Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 An anomaly of subtype relations at component refinement and a generative solution in C++ Zoltán Porkoláb, István Zólyomi {gsd | Eötvös.

Similar presentations


Presentation on theme: "1 An anomaly of subtype relations at component refinement and a generative solution in C++ Zoltán Porkoláb, István Zólyomi {gsd | Eötvös."— Presentation transcript:

1 1 An anomaly of subtype relations at component refinement and a generative solution in C++ Zoltán Porkoláb, István Zólyomi {gsd | scamel}@elte.hu Eötvös Loránd University, Budapest, Hungary

2 2 OpEval PlusCheckPlusEval Operator Plus OpCheckOpDisplay PlusDisplay Example (Harold Ossher)

3 3 Possible solutions Virtual inheritance Intrusive: specified in base classes More concerns implies exponential number of virtual bases Traits No subtype relationship using hierarchies in traits AOP are extensions to standard C++ Signature Promising, but non-standard, available only for Gnu compiler Eiffel: ADOPT CSet: subtype relationship implemented with metaprogramming Non-intrusive solution based on the C++ standard

4 4 C++ Templates Macro-like type parameters –Strong type checking Templates alone form a Turing-complete functional language inside C++ –Based on template specializations Metaprograms –Executed during compilation –A program can observe its state –May make decisions depending on state –Compile-time adaptation

5 5 CSET_3(OpDisplay, OpEval, OpCheck) op;

6 6

7 7 Source code of Cset construction template struct CSet; template struct CSet > : public Head, public CSet { // --- Type name shortcuts typedef CSet Rest; typedef CSet > MyType; // --- Copy constructor CSet(const MyType& mt) : Head(mt), Rest(mt) {} // --- "Recursive" constructor CSet(const Head& head, const Rest& rest): Head(head), Rest(rest) {} }; template struct CSet > : public Head { // --- All in one constructor CSet(const Head& head) : Head(head) {} };

8 8 Conversions between CSets Initialize the head class for each recursion step Template constructors provide conversion steps (the same for operator= ) CSET_3(PlusEval,PlusDisplay, PlusCheck) sum; CSET_2(OpEval,OpCheck) calculate; calculate = sum; Example of usage: template template CSet > :: CSet(const FromType& f): Head(f), CSet (f) {}

9 9 Advantages Type safe: based on builtin language conversions Efficient: no temporal CSets or objects are used, objects are initialized directly Open: not restricted to CSet only, any user object can be converted without explicit conversion call. struct Minus: public MinusEval, public MinusDisplay, public MinusCheck {}; Minus subtract; calculate = subtract;

10 10 Smart pointers Problems: Conversion copies objects by value (slicing) No dynamic binding Solution: smart pointers Implementation and usage similar to those of CSet CSET_3(PlusDisplay, PlusEval, PlusCheck) sum; CSETPTR_2(OpDisplay, OpEval) opPtr(sum); // --- Function call with explicit cast static_cast (opPtr)->show(); // --- In longer form with implicit cast OpDisplay *displayPtr = opPtr; displayPtr->show();

11 11 CSETPTR_2(OpDisplay,OpEval) OpDisplay *head CSETPTR_1(OpEval) OpEval *head PlusDisplayPlusCheckPlusEval points to CSETPTR_2(OpDisplay, OpEval) opPtr; head: OpDisplay* head: OpEval*

12 12 Similar to CSetPtr Store references instead of pointers Consequences: A CSetRef object must be initialized Initializes by reference but copies by value during assignment CSetRef CSET_3(PlusDisplay, PlusEval, PlusCheck) sum; // --- Initializes by reference CSETREF_3(OpDisplay, OpEval, OpCheck) exprRef(sum); // --- Copies by value exprRef = sum;

13 13 #include #include cset.h" struct Shape { virtual void f() { std::cout << "Shape"; } }; struct Circle : public Shape { void f() { std::cout << "Circle"; } }; struct Colored {}; struct Filled {}; void main() { CSET_3(Circle, Colored, Filled) extCircle; CSET_3(Colored, Filled, Shape) extShape(extCircle); CSET_2(Filled, Colored) extensions(extCircle); extensions = extShape = extCircle; CSETPTR_3(Colored, Filled, Shape) extShapePtr(extCircle); Shape* shapePtr = extShapePtr; shapePtr->f(); // --- Prints Circle CSETPTR_3(Filled, Colored, Circle) extCirclePtr(extCircle); extShapePtr = extCircle;// --- Object -> Pointer extShapePtr = extCirclePtr; // --- Der. Pointer -> Base Pointer extShape = extShapePtr;// --- Pointer -> Object (by value) }

14 14 Summary Support for collaboration based design Non-intrusive solution Easy to understand and manage No extension to standard C++ Bad experience with compilers vs standards Features: Composition of concerns in a single class (CSet) Conversion between related CSets Dynamic binding with smart pointers and references

15 15 Zoltán Porkoláb István Zólyomi Eötvös Loránd University, Budapest, Hungary gsd@elte.hu scamel@elte.hu Download source from http://gsd.web.elte.huhttp://gsd.web.elte.hu


Download ppt "1 An anomaly of subtype relations at component refinement and a generative solution in C++ Zoltán Porkoláb, István Zólyomi {gsd | Eötvös."

Similar presentations


Ads by Google