Assignment #1 Advanced Programming in C /2017

Slides:



Advertisements
Similar presentations
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Advertisements

Chapter 7: User-Defined Functions II
Intro to Generic Programming Templates and Vectors.
Programming Languages and Paradigms Object-Oriented Programming.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
C# Fundamentals An Introduction. Before we begin How to get started writing C# – Quick tour of the dev. Environment – The current C# version is 5.0 –
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Operator Overloading Introduction
Motivation for Generic Programming in C++
C++ Lesson 1.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Procedural and Object-Oriented Programming
C# for C++ Programmers 1.
Test 2 Review Outline.
Programming with ANSI C ++
Chapter 13: Overloading and Templates
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
11 Chapter Structured Data
C++ Templates.
Objectives In this chapter, you will:
Templates.
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
7. Inheritance and Polymorphism
Introduction to C++ Systems Programming.
Type Traits By Reggie Meisler.
Programming Languages Dan Grossman 2013
Review: Two Programming Paradigms
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Object Lifetime and Dynamic Objects
Chapter 15: Overloading and Templates
Generics, Lambdas, Reflections
Indexer AKEEL AHMED.
Java Review: Reference Types
Templates.
This pointer, Dynamic memory allocation, Constructors and Destructor
The dirty secrets of objects
Introduction to Classes
CS212: Object Oriented Analysis and Design
Java Programming Language
Built-In (a.k.a. Native) Types in C++
Constructors and Other Tools
Dr. Bhargavi Dept of CS CHRIST
Polymorphism Polymorphism
Classes and Objects.
CISC/CMPE320 - Prof. McLeod
Introduction C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell.
Prof. Bhushan Trivedi Director GLS Institute of Computer Technology
UNIT I OBJECT ORIENTED PROGRAMMING FUNDAMENTALS
CISC/CMPE320 - Prof. McLeod
Focus of the Course Object-Oriented Software Development
9-10 Classes: A Deeper Look.
Method of Classes Chapter 7, page 155 Lecture /4/6.
VIRTUAL FUNCTIONS RITIKA SHARMA.
CIS 199 Final Review.
Submitted By : Veenu Saini Lecturer (IT)
Standard Version of Starting Out with C++, 4th Edition
CMPE212 – Reminders Assignment 2 due next Friday.
9-10 Classes: A Deeper Look.
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Object Oriented Programming
Chengyu Sun California State University, Los Angeles
CMSC 202 Constructors Version 9/10.
Presentation transcript:

Assignment #1 Advanced Programming in C++ 2016/2017 NPRG051 Advanced programming in C++ - Assignment #1

Assignment #1 – serialize deadline: Wed 15 Mar 14:00 NPRG051 Advanced programming in C++ - Assignment #1

Assignment #1 - serialize class testB { public: std::string attrB1; enum E { E_X, E_Y, E_Z } attrB2; }; testB data = { “One”, E_X }; std::ofstream ofs( “data.json”); serialize::dump(ofs, data); testB data2; std::ifstream ifs( “data.json”); data2 = serialize::load(ifs); Implement two generic functions dump the contents of an object into a file load the contents of an object from a file Use json format: {"attrB1"="One","attrB2"=“E_X"} Problem: This “ideal” interface can not work in C++ the dump function cannot enumerate the data members of the object the load function cannot determine the type it should return NPRG051 Advanced programming in C++ - Assignment #1

Assignment #1 - serialize In C++, we have (almost) no introspection Run-time introspection in C++: typeid(obj) returns a type descriptor (std::type_info) the type descriptor allows the following operations: compare two types for identity generate some implementation-defined unique name of the type it may be used as a key in associative containers typeid(obj) works as true run-time identification only if the dynamic (run-time) type of obj is inherited from a base class B the static (compile-time) type of obj is (a reference to) B B contains at least one virtual function in other cases, typeid(obj) just returns the static type of obj The type descriptor is not able to enumerate members of the type There is no such information at run-time (nor for virtual functions) NPRG051 Advanced programming in C++ - Assignment #1

Assignment #1 - serialize Compile-time introspection in C++ It is possible to determine some properties of a type Example: the constant std::is_array_v< T> is true if T is an array the constant std::extent_v<T> returns the size of the array It is possible to determine whether a class contains a member of given name Example: the constant std::is_callable_v< F(A1,...,An)> is true if an object of type F may act as a function with arguments of types A1,...,An For class F, it effectively checks whether it contains a function named “operator()” which can be called with the specified argument types It requires extremely dirty tricks (combining SFINAE and decltype()) Essentially, the SFINAE trick allows checking whether a particular syntactically-correct expression is also type-correct, without causing error messages if it is not It can work only if the C++ source code contains the name of the member being checked The interface of a generic function to determine existence of T::name would be ugly: decltype( contains_member< T>([](auto && v){ v.name(); }))::value There is no way to enumerate the members of a class The compiler has the information but there is no interface in the language NPRG051 Advanced programming in C++ - Assignment #1

Assignment #1 - serialize For problems like generic “serialize”, the user must cooperate The user must supply a “list” of members for each class involved In this assignment, we require non-intrusive form of support It is required to work over existing classes without their modification The “list” must be specified outside the class When calling the dump/load function, the user shall supply the “list” The “list” shall be handled at compile-time, therefore it must be a type The resulting syntax shall be: serialize::dump<list>(ofs, data); data2 = serialize::load<list>(ifs); For the load function, the “list” must also deliver the ability to create the object being returned NPRG051 Advanced programming in C++ - Assignment #1

Assignment #1 - serialize testB data, data2; serialize::dump<listB>(ofs, data); data2 = serialize::load<listB>(ifs); listB shall be a class/struct which somehow describes the class testB It will not be instantiated – only the following members may be useful: nested classes or type definitions static constants static functions Such class/struct is usually called policy class we will use the name structpolicyB instead of listB in the examples we will use another kind of policy classes (e.g., enumpolicyE) to describe enumerations In our case, the policy class allows to: pass through all data members in an user-defined order create an object of type testB NPRG051 Advanced programming in C++ - Assignment #1

Assignment #1 - serialize class testB { public: std::string attrB1; enum E { E_X, E_Y, E_Z } attrB2; }; In our cases, we want to: pass through all data members in an user-defined order create an object of type testB For creation, we need a function: struct structpolicyB { static auto create() { return testB{}; } // ... data2 = serialize::load<structpolicyB>(ifs); NPRG051 Advanced programming in C++ - Assignment #1

Assignment #1 - serialize class testB { public: std::string attrB1; enum E { E_X, E_Y, E_Z } attrB2; }; For enumeration of attributes, we may theoretically use a type: struct structpolicyB { using attributes = std::tuple< something_about_attrB1, something_about_attrB2>; // ... In this code, std::tuple is merely a way to pack a list of types Describing attrB1 using only a type may be done using another policy class: struct something_about_attrB1 { static auto & f( testB & v) { return v.attrB1; } This is long and difficult to use – we prefer a different way... NPRG051 Advanced programming in C++ - Assignment #1

Assignment #1 - serialize class testB { public: std::string attrB1; enum E { E_X, E_Y, E_Z } attrB2; }; The simpler way for enumeration of attributes is a function struct structpolicyB { static void make(/*...*/) { something_about_attrB1(/*...*/); something_about_attrB2(/*...*/); } What arguments we need? Something representing the json parser/generator A reference to an object of type testB NPRG051 Advanced programming in C++ - Assignment #1

Assignment #1 - serialize class testB { public: std::string attrB1; enum E { E_X, E_Y, E_Z } attrB2; }; What arguments we need? Something representing the json parser/generator The users would not like writing two such functions We need a template to handle both cases at once A reference to an object of type testB struct structpolicyB { template< typename X> static void make(X && x, testB & v) { something_about_attrB1(x, v); something_about_attrB2(x, v); } NPRG051 Advanced programming in C++ - Assignment #1

Assignment #1 - serialize class testB { public: std::string attrB1; enum E { E_X, E_Y, E_Z } attrB2; }; How do we access the attribute? Passing a reference to the library code: template< typename X> static void make(X && x, testB & v) { string_attribute(x, v.attrB1); enum_attribute(x, v.attrB2); } It works as long as the references are accessible... NPRG051 Advanced programming in C++ - Assignment #1

Assignment #1 - serialize class testA { public: int get_A1() const { return attrA1; } void set_A1( int x) { attrA1 = x; } private: int attrA1; }; What if the attribute is accessible only through get/set? Passing a reference to the library code: template< typename X> static void make(X && x, testA & v) { int_attribute(x, v.get_A1(), v.set_A1(/*???*/)); } Completely wrong! What is the argument of set? It always calls set even if we want only to get NPRG051 Advanced programming in C++ - Assignment #1

Assignment #1 - serialize class testA { public: int get_A1() const { return attrA1; } void set_A1( int x) { attrA1 = x; } private: int attrA1; }; What if the attribute is accessible only through get/set? template< typename X> static void make(X && x, testA & v) { int_attribute(x, [&v]() { return v.get_A1(); }, [&v](int x) { v.set_A1(x); }); } int_attribute receives two functors and calls one of them, depending on the type X The use of lambdas allows to invoke only one of the get/set functions but it does not solve all problems... NPRG051 Advanced programming in C++ - Assignment #1

Assignment #1 - serialize template< typename X> static void make(X && x, testA & v) { int_attribute(x, [&v]() { return v.get_A1(); }, [&v](int x) { v.set_A1(x); }); } Problem: we need writable access to testA even if we only want to read Solution: static void make(X && x) { int_attribute(x, [](const testA & v) { return v.get_A1(); }, [](testA & v, int n) { v.set_A1(n); }); Note: x now shall represent both the json file and the object being loaded/dumped, i.e., x shall be a structure containing two pointers or references NPRG051 Advanced programming in C++ - Assignment #1

Assignment #1 - serialize Shorter implementation using auto template< typename X> static void make(X && x) { int_attribute(x, [](auto && v) { return v.get_A1(); }, [](auto && v, auto && x) { v.set_A1(x); }); } Even shorter (and safer) using pointers: [](auto v) { return v->get_A1(); }, [](auto v, auto && x) { v->set_A1(x); }); NPRG051 Advanced programming in C++ - Assignment #1

Assignment #1 - serialize template< typename X> static void make(X && x) { int_attribute(x, [](auto && v) { return v.get_A1(); }, [](auto && v, auto && x) { v.set_A1(x); }); } Implementing int_attribute We need to do different things (dump/load), depending on the type of x This is a compile-time switch - function overloading can do the trick The two functions still need to be templates to accomodate the functor arguments template< typename SetF, typename GetF> void int_attribute(const dump_pair & x, SetF && sf, GetF && gF); void int_attribute(const load_pair & x, SetF && sf, GetF && gF); But there is still a problem... NPRG051 Advanced programming in C++ - Assignment #1

Assignment #1 - serialize template< typename X> static void make(X && x) { int_attribute(x, [](auto && v) { return v.get_A1(); }, [](auto && v, auto && x) { v.set_A1(x); }); } Implementing int_attribute The dump_pair/load_pair shall contain a pointer/reference to the dumper/loader stream a pointer/reference to the object being dumped/accessed The code shall work for any type of object – it requires another template argument template< typename TObj, typename SetF, typename GetF> void int_attribute(const dump_pair< TObj> & x, SetF && sf, GetF && gF); void int_attribute(const load_pair< TObj> & x, SetF && sf, GetF && gF); NPRG051 Advanced programming in C++ - Assignment #1

Assignment #1 - serialize class testB { /*...*/ }; class testA { public: enum E { E_X, E_Y, E_Z } attrA2; std::list< testB> attrA3; }; Handling non-trivial data members The functions shall receive a policy class describing the type of the attribute struct structpolicyA { template< typename X> static void make(X && x) { enum_attribute< enumpolicyE>(x, [](auto && v) -> auto && { return v.attrA2; }); struct_sequence_attribute< structpolicyB>(x, } NPRG051 Advanced programming in C++ - Assignment #1

Assignment #1 - serialize class testA { public: enum E { E_X, E_Y, E_Z } attrA2; }; Describing enumerations We need a list of binary-string pairs (template enumeration_list) struct enumpolicyE { static auto create() return testB::E{}; } static enumeration_list< testB::E> enumeration() return { { testB::E_X, "X" }, { testB::E_Y, "Y" }, { testB::E_Z, "Z" } }; NPRG051 Advanced programming in C++ - Assignment #1

Assignment #1 - serialize The assignment #1 You will receive three files: du1example.hpp – a sample data structure – do not touch! du1examplepolicy.hpp – a version of the corresponding policy classes You may change the policy classes (not their names) if you want/need du1test.cpp – a test based on the sample structures and policy classes – do not touch! You have to return the following files (via SIS): du1serialize.hpp – your implementation of dump/load the dump/load functions must be callable as used in du1test.cpp du1serialize.cpp – your implementation (non-template non-inline functions) du1examplepolicy.hpp – the policy classes corresponding to the original du1example.hpp and your du1serialize.hpp NPRG051 Advanced programming in C++ - Assignment #1

Assignment #1 - serialize You have to return the following files (via SIS): du1serialize.hpp – your implementation of dump/load the dump/load functions must be callable as used in du1test.cpp testA xA = /*...*/; std::ofstream ofs{ fname }; serialize::dump< structpolicyA>(ofs, (const testA &)xA); std::ifstream ifs{ fname }; auto xAcopy = serialize::load< structpolicyA>(ifs); du1serialize.cpp – your implementation (non-template non-inline functions) du1examplepolicy.hpp – the policy classes corresponding to the original du1example.hpp and your du1serialize.hpp NPRG051 Advanced programming in C++ - Assignment #1

Assignment #1 - serialize Your system shall support data members of the following data types: int std::string enumeration types (with policy class) sequential containers of struct/class types For all types, the system shall support access by reference, i.e. directly accessible public data members private data members indirectly accessible via a member function returning reference In addition, for data members of type int, the system shall support access by a pair of get/set methods NPRG051 Advanced programming in C++ - Assignment #1