bbfunction.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #ifndef BB_FUNCTION_H
  2. #define BB_FUNCTION_H
  3. #include "bbtypes.h"
  4. #include "bbdebug.h"
  5. namespace bbGC{
  6. void *malloc( size_t size );
  7. void free( void *p );
  8. }
  9. template<class T> class bbFunction;
  10. template<class R,class...A> struct bbFunction<R(A...)>{
  11. typedef R(*F)(A...);
  12. struct FunctionRep;
  13. struct SequenceRep;
  14. template<class C> struct MethodRep;
  15. static R castErr( A... ){
  16. puts( "Null Function Error" );
  17. exit( -1 );
  18. return R();
  19. }
  20. struct Rep{
  21. int refs=0;
  22. virtual ~Rep(){
  23. }
  24. virtual R invoke( A... ){
  25. return R();
  26. }
  27. virtual bool equals( Rep *rep ){
  28. return rep==this;
  29. }
  30. virtual int compare( Rep *rhs ){
  31. if( this<rhs ) return -1;
  32. if( this>rhs ) return 1;
  33. return 0;
  34. }
  35. virtual Rep *remove( Rep *rep ){
  36. if( equals( rep ) ) return &_nullRep;
  37. return this;
  38. }
  39. virtual void gcMark(){
  40. }
  41. void *operator new( size_t size ){
  42. return bbGC::malloc( size );
  43. }
  44. void operator delete( void *p ){
  45. bbGC::free( p );
  46. }
  47. };
  48. struct FunctionRep : public Rep{
  49. F p;
  50. FunctionRep( F p ):p( p ){
  51. }
  52. virtual R invoke( A...a ){
  53. return p( a... );
  54. }
  55. virtual bool equals( Rep *rhs ){
  56. FunctionRep *t=dynamic_cast<FunctionRep*>( rhs );
  57. return t && p==t->p;
  58. }
  59. virtual int compare( Rep *rhs ){
  60. FunctionRep *t=dynamic_cast<FunctionRep*>( rhs );
  61. if( t && p==t->p ) return 0;
  62. return Rep::compare( rhs );
  63. }
  64. virtual F Cast(){
  65. return p;
  66. }
  67. };
  68. template<class C> struct MethodRep : public Rep{
  69. typedef R(C::*T)(A...);
  70. C *c;
  71. T p;
  72. MethodRep( C *c,T p ):c(c),p(p){
  73. }
  74. virtual R invoke( A...a ){
  75. return (c->*p)( a... );
  76. }
  77. virtual bool equals( Rep *rhs ){
  78. MethodRep *t=dynamic_cast<MethodRep*>( rhs );
  79. return t && c==t->c && p==t->p;
  80. }
  81. virtual int compare( Rep *rhs ){
  82. MethodRep *t=dynamic_cast<MethodRep*>( rhs );
  83. if( t && c==t->c && p==t->p ) return 0;
  84. return Rep::compare( rhs );
  85. }
  86. virtual void gcMark(){
  87. bbGCMark( c );
  88. }
  89. };
  90. struct SequenceRep : public Rep{
  91. bbFunction lhs,rhs;
  92. SequenceRep( const bbFunction &lhs,const bbFunction &rhs ):lhs( lhs ),rhs( rhs ){
  93. }
  94. virtual R invoke( A...a ){
  95. lhs( a... );
  96. return rhs( a... );
  97. }
  98. virtual Rep *remove( Rep *rep ){
  99. if( rep==this ) return &_nullRep;
  100. Rep *lhs2=lhs._rep->remove( rep );
  101. Rep *rhs2=rhs._rep->remove( rep );
  102. if( lhs2==lhs._rep && rhs2==rhs._rep ) return this;
  103. if( lhs2!=&_nullRep && rhs2 !=&_nullRep ) return new SequenceRep( lhs2,rhs2 );
  104. if( lhs2!=&_nullRep ) return lhs2;
  105. if( rhs2!=&_nullRep ) return rhs2;
  106. return &_nullRep;
  107. }
  108. virtual void gcMark(){
  109. lhs._rep->gcMark();
  110. rhs._rep->gcMark();
  111. }
  112. };
  113. Rep *_rep;
  114. static Rep _nullRep;
  115. void retain()const{
  116. ++_rep->refs;
  117. }
  118. void release(){
  119. if( !--_rep->refs && _rep!=&_nullRep ) delete _rep;
  120. }
  121. bbFunction( Rep *rep ):_rep( rep ){
  122. retain();
  123. }
  124. public:
  125. bbFunction():_rep( &_nullRep ){
  126. }
  127. bbFunction( const bbFunction &p ):_rep( p._rep ){
  128. retain();
  129. }
  130. template<class C> bbFunction( C *c,typename MethodRep<C>::T p ):_rep( new MethodRep<C>(c,p) ){
  131. retain();
  132. }
  133. bbFunction( F p ):_rep( new FunctionRep( p ) ){
  134. retain();
  135. }
  136. ~bbFunction(){
  137. release();
  138. }
  139. void discard(){
  140. if( _rep==&_nullRep ) return;
  141. delete _rep;
  142. _rep=&_nullRep;
  143. }
  144. bbFunction &operator=( const bbFunction &p ){
  145. p.retain();
  146. release();
  147. _rep=p._rep;
  148. return *this;
  149. }
  150. bbFunction operator+( const bbFunction &rhs )const{
  151. if( _rep==&_nullRep ) return rhs;
  152. if( rhs._rep==&_nullRep ) return *this;
  153. return new SequenceRep( *this,rhs );
  154. }
  155. bbFunction operator-( const bbFunction &rhs )const{
  156. return _rep->remove( rhs._rep );
  157. }
  158. bbFunction &operator+=( const bbFunction &rhs ){
  159. *this=*this+rhs;
  160. return *this;
  161. }
  162. bbFunction &operator-=( const bbFunction &rhs ){
  163. *this=*this-rhs;
  164. return *this;
  165. }
  166. bbBool operator==( const bbFunction &rhs )const{
  167. return _rep->equals( rhs._rep );
  168. }
  169. bbBool operator!=( const bbFunction &rhs )const{
  170. return !_rep->equals( rhs._rep );
  171. }
  172. operator bbBool()const{
  173. return _rep==&_nullRep;
  174. }
  175. R operator()( A...a )const{
  176. return _rep->invoke( a... );
  177. }
  178. //cast to simple static function ptr
  179. //
  180. operator F()const{
  181. FunctionRep *t=dynamic_cast<FunctionRep*>( _rep );
  182. if( t ) return t->p;
  183. return castErr;
  184. }
  185. };
  186. template<class R,class...A> typename bbFunction<R(A...)>::Rep bbFunction<R(A...)>::_nullRep;
  187. template<class C,class R,class...A> bbFunction<R(A...)> bbMethod( C *c,R(C::*p)(A...) ){
  188. return bbFunction<R(A...)>( c,p );
  189. }
  190. template<class C,class R,class...A> bbFunction<R(A...)> bbMethod( const bbGCVar<C> &c,R(C::*p)(A...) ){
  191. return bbFunction<R(A...)>( c.get(),p );
  192. }
  193. template<class R,class...A> bbFunction<R(A...)> bbMakefunc( R(*p)(A...) ){
  194. return bbFunction<R(A...)>( p );
  195. }
  196. template<class R,class...A> void bbGCMark( const bbFunction<R(A...)> &t ){
  197. t._rep->gcMark();
  198. }
  199. template<class R,class...A> int bbCompare( const bbFunction<R(A...)> &x,const bbFunction<R(A...)> &y ){
  200. return x._rep->compare( y._rep );
  201. }
  202. template<class R,class...A> bbString bbDBType( bbFunction<R(A...)> *p ){
  203. return bbDBType<R>()+"()";
  204. }
  205. template<class R,class...A> bbString bbDBValue( bbFunction<R(A...)> *p ){
  206. return "function?????";
  207. }
  208. #endif