C++ extension methods

2,758 views
2,533 views

Published on

A 5 minute (or just over) lightning talk I did at ACCU2013

Published in: Technology, Health & Medicine
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,758
On SlideShare
0
From Embeds
0
Number of Embeds
213
Actions
Shares
0
Downloads
19
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

C++ extension methods

  1. 1. C++ Ext e n s i o@phil_nash n Methods
  2. 2. class  A:  foo()  bar()  data
  3. 3. class  A:  foo() +  explode() ?  bar()  data
  4. 4. Dynamiclanguages?
  5. 5. setattr(cls,  func.__name__,                types.MethodType(func,  cls))
  6. 6. setattr(cls,  func.__name__,                types.MethodType(func,  cls))var  a  =  {};a.f1  =  function(){};
  7. 7. setattr(cls,  func.__name__,                types.MethodType(func,  cls))var  a  =  {};a.f1  =  function(){};struct  objc_method  myMethod;myMethod.method_name  =  sel_registerName("sayHello");myMethod.method_imp    =  sayHello;  struct  objc_method_list  *  myMethodList;myMethodList  =  malloc  (sizeof(struct  objc_method_list));myMethodList-­‐>method_count  =  1;myMethodList-­‐>method_list[0]  =  myMethod;    class_addMethods  (  [EmptyClass  class],  myMethodList  );
  8. 8. Staticlanguages?
  9. 9. namespace  ExtensionMethods  {        public  static  class  MyExtensions  {                public  static  int  WordCount(this  String  str)  {                        return  str.Split(new  char[]  {    ,  .,  ?  },                                                            StringSplitOptions.RemoveEmptyEntries).Length;                }        }      }
  10. 10. namespace  ExtensionMethods  {        public  static  class  MyExtensions  {                public  static  int  WordCount(this  String  str)  {                        return  str.Split(new  char[]  {    ,  .,  ?  },                                                            StringSplitOptions.RemoveEmptyEntries).Length;                }        }       }@implementation NSString (reverse) -(NSString *) reverseString { NSMutableString *reversedStr; int len = [self length]; reversedStr = [NSMutableString stringWithCapacity:len]; while (len > 0) [reversedStr appendString: [NSString stringWithFormat:@"%C", [self characterAtIndex:--len]]];   return reversedStr;}
  11. 11. C++ ?
  12. 12. TheC++ Interface principle
  13. 13. namespace  NS  {        class  T  {  };        void  f(  T  );}  int  main()  {        NS::T  object;        f(object);}
  14. 14. namespace  NS  {        class  T  {  };        void  f(  T  );}  int  main()  {        NS::T  object;        object.f();}
  15. 15. namespace  NS  {        class  T  {  };        void  f(  T  );}  int  main()  {        NS::T*  object  =  new  NS::T();        object-­‐>f();}
  16. 16. object-­‐>f()
  17. 17. object<-­‐f()
  18. 18. struct Widget : Extendible<Widget> { Widget( int size, int weight ) : size( size ), weight( weight ) {} int size; int weight;};
  19. 19. struct Widget : Extendible<Widget> { Widget( int size, int weight ) : size( size ), weight( weight ) {} int size; int weight;};struct print : ExtMethod<print> { void operator()( Widget& w ) { std::cout << "size: " << w.size << ", weight: " << w.weight << std::endl; }}; Widget w( 4, 10 ); w<-print();
  20. 20. struct Widget : Extendible<Widget> { Widget( int size, int weight ) : size( size ), weight( weight ) {} int size; int weight;};struct print : ExtMethod<print> { void operator()( Widget& w ) { std::cout << "size: " << w.size << ", weight: " << w.weight << std::endl; }};struct density : ExtMethod<density, float> { float operator()( Widget& w ) { return (float)(w.weight / w.size); }}; Widget w( 4, 10 ); w<-print(); float d = w<-density();
  21. 21. template<typename T, typename ReturnT=void>struct ExtMethod { ExtMethod& operator - () { return *this; } template<typename U> ReturnT operator()( U& obj ) { return static_cast<T*>(this)->operator()( obj ); }};template<typename Derived>struct Extendible{ template<typename T, typename ReturnT> ReturnT operator < ( ExtMethod<T, ReturnT>& extMethod ) { return extMethod( static_cast<Derived&>( *this ) ); }};
  22. 22. template<typename T, typename ReturnT=void>struct ExtMethod { ExtMethod& operator - () { return *this; } template<typename U> ReturnT operator()( U& obj ) { return static_cast<T*>(this)->operator()( obj ); }};template<typename Derived>struct Extendible{ template<typename T, typename ReturnT> ReturnT operator < ( ExtMethod<T, ReturnT>& extMethod ) { return extMethod( static_cast<Derived&>( *this ) ); }};
  23. 23. template<typename T, typename ReturnT=void> struct ExtMethod { e! ExtMethod& operator - () { } return *this; c od template<typename U> ion ct ReturnT operator()( U& obj ) { u return static_cast<T*>(this)->operator()( obj ); d ro } }; in p this do template<typename Derived> n ’t struct Extendible { o se d template<typename T, typename ReturnT> ea ReturnT operator < ( ExtMethod<T, ReturnT>& extMethod ) {Pl }; } return extMethod( static_cast<Derived&>( *this ) );

×