scoop: a static c++ object-oriented paradigmmoulard/seminar.pdf · generic programming with c++...
TRANSCRIPT
Introduction Common C++ paradigms SCOOP’s approach Conclusion
SCOOP: a static C++ object-oriented paradigm
Thomas Moulard
LRDE – EPITA Research and Development Laboratory
January 9, 2008
Thomas Moulard SCOOP: a static C++ object-oriented paradigm 1 / 27
Introduction Common C++ paradigms SCOOP’s approach Conclusion
Outline
1 Introduction
2 Common C++ paradigms
3 SCOOP’s approach
4 Conclusion
Thomas Moulard SCOOP: a static C++ object-oriented paradigm 2 / 27
Introduction Common C++ paradigms SCOOP’s approach Conclusion
Outline
1 Introduction
2 Common C++ paradigms
3 SCOOP’s approach
4 Conclusion
Thomas Moulard SCOOP: a static C++ object-oriented paradigm 3 / 27
Introduction Common C++ paradigms SCOOP’s approach Conclusion
Introduction
What is not SCOOP?
Not a library SCOOP is more than a tool as it changes how toengine a library.
Not a framework SCOOP can be integrated to existing projects.
Not a language extension SCOOP uses “pure” C++ 2003 (noextra-preprocessing, nor extension).
Thomas Moulard SCOOP: a static C++ object-oriented paradigm 4 / 27
Introduction Common C++ paradigms SCOOP’s approach Conclusion
Introduction
What is not SCOOP?
Not a library SCOOP is more than a tool as it changes how toengine a library.
Not a framework SCOOP can be integrated to existing projects.
Not a language extension SCOOP uses “pure” C++ 2003 (noextra-preprocessing, nor extension).
Thomas Moulard SCOOP: a static C++ object-oriented paradigm 4 / 27
Introduction Common C++ paradigms SCOOP’s approach Conclusion
Introduction
What is not SCOOP?
Not a library SCOOP is more than a tool as it changes how toengine a library.
Not a framework SCOOP can be integrated to existing projects.
Not a language extension SCOOP uses “pure” C++ 2003 (noextra-preprocessing, nor extension).
Thomas Moulard SCOOP: a static C++ object-oriented paradigm 4 / 27
Introduction Common C++ paradigms SCOOP’s approach Conclusion
Introduction
What is SCOOP?
SCOOP is a paradigm, i.e. a thought pattern [Wikipedia, 2008].Designed during Olena’s development in order to write genericlibraries [Geraud, 2006].
Consequences
Impact software design (class hierarchies).
Introduce a new C++ “dialect”.
Bring new features in current C++ through a different way ofwriting pieces of software.
Thomas Moulard SCOOP: a static C++ object-oriented paradigm 5 / 27
Introduction Common C++ paradigms SCOOP’s approach Conclusion
Outline
1 Introduction
2 Common C++ paradigms
3 SCOOP’s approach
4 Conclusion
Thomas Moulard SCOOP: a static C++ object-oriented paradigm 6 / 27
Introduction Common C++ paradigms SCOOP’s approach Conclusion
Object-Oriented programming with C++ 2003
Imagefill(Color)
Image2d
fill(Color)
Image3d
fill(Color)
Specialized versions of the fill algorithm.
Figure: Object-Orientedprogramming
Features
Classes hierarchies Define abstract baseclasses and specialize themvia sub-classing.
Dynamic typing Dynamic typing allows avariable of type X to containa value of type Y whereY <: X (read “Y is asub-type of X”).
Virtual methods Virtual methods allowimplementation refinement.
Thomas Moulard SCOOP: a static C++ object-oriented paradigm 8 / 27
Introduction Common C++ paradigms SCOOP’s approach Conclusion
Object-Oriented programming with C++ 2003Common problems
ImagegetPixel(Point)
Image2d
getPixel(Point2d)
Image3d
getPixel(Point3d)
Figure: Object-Orientedprogramming and covariancearguments.
Argument covariance
Arguments are not covariant in C++: iffill takes an argument of type Point, aspecialized version of fill can not take anargument of type Point2d (wherePoint2d <: Point).
Thomas Moulard SCOOP: a static C++ object-oriented paradigm 9 / 27
Introduction Common C++ paradigms SCOOP’s approach Conclusion
Object-Oriented programming with C++ 2003Common problems
ImagegetPixel(Point)
Image2d
getPixel(Point2d)
Image3d
getPixel(Point3d)
Impossible to express aspecialized version of getPixel
Figure: Object-Orientedprogramming and covariancearguments.
Argument covariance
Arguments are not covariant in C++: iffill takes an argument of type Point, aspecialized version of fill can not take anargument of type Point2d (wherePoint2d <: Point).
Thomas Moulard SCOOP: a static C++ object-oriented paradigm 9 / 27
Introduction Common C++ paradigms SCOOP’s approach Conclusion
Object-Oriented programming with C++ 2003Common problems
Image
Image2d Image3d
Figure: Object-Orientedprogramming and multi-methods.
Multi-methods
Virtual methods realizes a dispatchdepending on the first argument of themethod (this).With multi-methods, the dispatch dependson every argument.
How to write a generic copy algorithm?
Impossible in “classic” C++ withoutrun-time overhead.
Thomas Moulard SCOOP: a static C++ object-oriented paradigm 10 / 27
Introduction Common C++ paradigms SCOOP’s approach Conclusion
Generic programming with C++ 2003
Vector
push_back(T)sort()
T
Vector<int>
push_back(int)sort()
Vector<float>
push_back(float)sort()
Meta-class
Classes
No inheritance betweenmeta-class and classes.
Figure: Generic programming.
Features
Meta-class use Define meta-class (i.e.templated classes) in order togenerate a class which suitsspecific needs.
Static typing No class hierarchies (nosub-typing): type checkingdone at compile-time.
Easy genericity No need to write plenty ofclasses, just templating byanother type.
Thomas Moulard SCOOP: a static C++ object-oriented paradigm 11 / 27
Introduction Common C++ paradigms SCOOP’s approach Conclusion
Generic programming with C++ 2003
Vector
push_back(T)sort()
T
Vector<Point1d>
push_back(Point1d)sort()
Vector<Point2d>
push_back(Point2d)sort()
Meta-class
Classes
Requires that operator<is defined in T (documentation only!)
OK
Error
Figure: The “sort” algorithm andgeneric programming.
Lack of constraints on parameters
Extremely easy to make mistakes asrequirements on types can not be expressedin the source code.It is impossible to restrict the parametersof the meta-class to the only classes whichhave the features you need (i.e. theattributes, methods. . . of the parameterused in the meta-class).
Thomas Moulard SCOOP: a static C++ object-oriented paradigm 12 / 27
Introduction Common C++ paradigms SCOOP’s approach Conclusion
Generic programming with C++-0X
LessThanComparable
bool operator<(T, U)
T, U
Point
Point1d
bool operator<(Point1d)
Point2d
Figure: C++-0X concepts.
C++-0X
C++-0X is the next C++ standard. Itshould be finished in one or two years. Oneof the major changement is theintroduction of Concepts.
Concepts
The concept proposition[D. Gregor, 2006]solves the problem of the lack ofconstraints. A structural matching is madein order to decide if a type is valid for aspecific template parameter.
Thomas Moulard SCOOP: a static C++ object-oriented paradigm 13 / 27
Introduction Common C++ paradigms SCOOP’s approach Conclusion
Comparing paradigms
XXXXXXXXXXXFeatureParadigm
OOP GP
Maintainability Easy Hard
Genericity Medium Medium
Speed Slow Fast
Table: Paradigms characteristics.
Main problem
How to obtain a paradigm which mixes the advantages of bothOriented-Object programming and Generic programming?
Thomas Moulard SCOOP: a static C++ object-oriented paradigm 14 / 27
Introduction Common C++ paradigms SCOOP’s approach Conclusion
Comparing paradigms
XXXXXXXXXXXFeatureParadigm
OOP GP SCOOP
Maintainability Easy Hard Medium
Genericity Medium Medium Easy
Speed Slow Fast Fast
Table: Paradigms characteristics.
SCOOP’s features
Static virtual methodsCovariant argumentsStatic multi-methodsFunctions from type to type (Morphers).
Thomas Moulard SCOOP: a static C++ object-oriented paradigm 14 / 27
Introduction Common C++ paradigms SCOOP’s approach Conclusion
Outline
1 Introduction
2 Common C++ paradigms
3 SCOOP’s approach
4 Conclusion
Thomas Moulard SCOOP: a static C++ object-oriented paradigm 15 / 27
Introduction Common C++ paradigms SCOOP’s approach Conclusion
SCOOP core mechanism
Anyexact():E&
E
SubClassexact
FinalClass
Figure: A simpleSCOOP hierarchy.
Curiously recurring template pattern [Coplien, 1995]
The dynamic type is a class parameter. Abstract, non-finalclasses are parameterized by the “exact” (dynamic) type, afinal (concrete) class is not parameterized (its exact type isitself).
Benefits and drawbacks
The dynamic type is known at compile time.Easy to dispatch depending on the dynamic type (forvirtual methods, argument covariance andmulti-methods).Less flexible than Oriented Object programming(impossible to change the dynamic type depending on anon-static condition).
Thomas Moulard SCOOP: a static C++ object-oriented paradigm 16 / 27
Introduction Common C++ paradigms SCOOP’s approach Conclusion
The Pimpl design pattern [Sutter, 2000]
Anyexact():E&
E
Emousse
myVirtualMethod():void
exact
Interface
FinalClass
impl_myVirtualMethod():void
Implementation.
Figure: Pimpl design pattern.
Description
A public method is divided twomethods functions:
A public method(myVirtualMethod here).Another function which reallyimplements the function.
Interest
Separate interface fromimplementation.Avoid changing the classinterface directly.
Thomas Moulard SCOOP: a static C++ object-oriented paradigm 17 / 27
Introduction Common C++ paradigms SCOOP’s approach Conclusion
Static virtual methods
Anyexact():E&
E
SubClass
myVirtualMethod():voidimpl_myVirtualMethod():void
E
exact ().impl_myVirtualNote ();
Default implementation.
FinalClass
impl_myVirtualMethod():void Method implementation.
Figure: Virtual methods in SCOOP.
Principle
The dispatch is done manuallyby the public method.
Interest
No “virtual” keyword: norun-time overhead.Easy to implement and tounderstand.Use language overloadingmechanism.
Thomas Moulard SCOOP: a static C++ object-oriented paradigm 18 / 27
Introduction Common C++ paradigms SCOOP’s approach Conclusion
Argument covariance
Anyexact():E&
E
Animal
eat(Food& f):void
exact
exact ().impl_eat (f.exact ());
Cow
impl_eat(Grass&):void
Ape
impl_eat(Banana&):void
Figure: Argument covariance in SCOOP.
Principle
Dispatch done manually bythe public method: theargument’s dynamic type canbe retrieved throughSCOOP’s mechanisms.
Interest
No run-time overhead.Easy to implement: usesC++ classic overloading.
Thomas Moulard SCOOP: a static C++ object-oriented paradigm 19 / 27
Introduction Common C++ paradigms SCOOP’s approach Conclusion
Multi-methods
Image
Image2d Image3d
Image copy (Image i1, Image i2)return exact ().impl_copy (i1.exact (), i2.exact ( ));
Image2d impl_copy (Image2d i1, Image2d i2)
Image3d impl_copy (Image3d i1, Image3d i2)
Image3d impl_copy (Image2d i1, Image3d i2)
Image2d impl_copy (Image3d i1, Image2d i2)
Figure: Multi-methods in SCOOP.
Principle
Dispatch done manually by theinterface function: the arguments’dynamic types can be retrievedthrough SCOOP’s mechanisms.
Interest
No run-time overhead.Easy to implement: uses C++classic overloading.
Thomas Moulard SCOOP: a static C++ object-oriented paradigm 20 / 27
Introduction Common C++ paradigms SCOOP’s approach Conclusion
Virtual types
Imagepoint: typegetPixel(point)
Image2d
point: type = Point2d
Figure: Virtual types in SCOOP.
Principle
Traits are used to contain typesassociated to classes. The retrievealgorithm of SCOOP allows its typeto be refined in subclasses.
Thomas Moulard SCOOP: a static C++ object-oriented paradigm 21 / 27
Introduction Common C++ paradigms SCOOP’s approach Conclusion
Property based inheritance [Thierry Geraud, 2007]
Anyexact():exact
exact
Forwardexact
RandomAccessexact
MetaBridge
Vector List
Figure: Property based inheritance.
Principle
Three parts:Concepts.Meta-bridge (internalmechanism).Implementation classes.
Thomas Moulard SCOOP: a static C++ object-oriented paradigm 22 / 27
Introduction Common C++ paradigms SCOOP’s approach Conclusion
Morphers
Anyexact():exact
exact
Forwardbegin (): iterend (): iter
exactRandomAccess
exact
MetaBridge
value_type: typeforward: typeiter: type
Vector
value_type: type = Tforward: type = Trueiter: type = BiDirectionalIter
Logger
delegatee: type = T
T T
Figure: Morphers in SCOOP.
Delegatee type
Delegatee is a special virtualtype, it delegates the virtualtype definition to anotherclass.
Principle
Morphers provides type totype functions through thedelegatee type. It satisfiesconcepts of the destinationtype, takes an instance of thesource type as parameter andbridges the two types.
Thomas Moulard SCOOP: a static C++ object-oriented paradigm 23 / 27
Introduction Common C++ paradigms SCOOP’s approach Conclusion
Outline
1 Introduction
2 Common C++ paradigms
3 SCOOP’s approach
4 Conclusion
Thomas Moulard SCOOP: a static C++ object-oriented paradigm 24 / 27
Introduction Common C++ paradigms SCOOP’s approach Conclusion
Conclusion
SCOOP’s road-map
Enhance SCOOP’s libraries (Metalic and Static) to makethem more user-friendly.
Prove that the paradigm can scale through the developmentof Olena 1.0.
Enhance code readability through a new mini-language(SCOOL, successor of Metagene [Maes, 2004]) or through aC++ language extension.
Thomas Moulard SCOOP: a static C++ object-oriented paradigm 25 / 27
Introduction Common C++ paradigms SCOOP’s approach Conclusion
Bibliography I
Coplien, J. O. (1995).Curiously recurring template patterns.C++ Rep., 7(2):24–27.
D. Gregor, B. S. (2006).Concepts (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/2006-06.htm).Technical report, JTC1/SC22/WG21 C++ StandardsCommittee.
Geraud, Th. (2006).Advanced static object-oriented programming features: Asequel to SCOOP.http://www.lrde.epita.fr/people/theo/pub/olena/olena-06-jan.pdf.
Thomas Moulard SCOOP: a static C++ object-oriented paradigm 26 / 27
Introduction Common C++ paradigms SCOOP’s approach Conclusion
Bibliography II
Maes, F. (2004).Metagene, a C++ meta-program generation tool.In Proceedings of the Workshop on Multiple Paradigm withOO Languages (MPOOL; in conjunction with ECOOP), Oslo,Norway.
Sutter, H. (2000).Pimplesbeauty marks you can depend on.pages 407–416.
Thierry Geraud, R. L. (2007).Property-based genericity.
Wikipedia (2008).Paradigm — wikipedia, the free encyclopedia.[Online; accessed 3-January-2008].
Thomas Moulard SCOOP: a static C++ object-oriented paradigm 27 / 27