00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef _COMPWRAPSYSTEM_H_
00027 #define _COMPWRAPSYSTEM_H_
00028
00029 #include <stdlib.h>
00030 #include <vector>
00031
00032 #define WRAPABLE_DEF(func, ...) \
00033 { \
00034 mHandler-> func ## SetEnabled (this, false); \
00035 return mHandler-> func (__VA_ARGS__); \
00036 }
00037
00038 #define WRAPABLE_HND(num,itype,rtype, func, ...) \
00039 rtype func (__VA_ARGS__); \
00040 void func ## SetEnabled (itype *obj, bool enabled) \
00041 { \
00042 functionSetEnabled (obj, num, enabled); \
00043 } \
00044 unsigned int func ## GetCurrentIndex () \
00045 { \
00046 return mCurrFunction[num]; \
00047 } \
00048 void func ## SetCurrentIndex (unsigned int index) \
00049 { \
00050 mCurrFunction[num] = index; \
00051 }
00052
00053 #define WRAPABLE_HND_FUNC(num, func, ...) \
00054 { \
00055 unsigned int curr = mCurrFunction[num]; \
00056 while (mCurrFunction[num] < mInterface.size () && \
00057 !mInterface[mCurrFunction[num]].enabled[num]) \
00058 mCurrFunction[num]++; \
00059 if (mCurrFunction[num] < mInterface.size ()) \
00060 { \
00061 mInterface[mCurrFunction[num]++].obj-> func (__VA_ARGS__); \
00062 mCurrFunction[num] = curr; \
00063 return; \
00064 } \
00065 mCurrFunction[num] = curr; \
00066 }
00067
00068 #define WRAPABLE_HND_FUNC_RETURN(num, rtype, func, ...) \
00069 { \
00070 unsigned int curr = mCurrFunction[num]; \
00071 while (mCurrFunction[num] < mInterface.size () && \
00072 !mInterface[mCurrFunction[num]].enabled[num]) \
00073 mCurrFunction[num]++; \
00074 if (mCurrFunction[num] < mInterface.size ()) \
00075 { \
00076 rtype rv = mInterface[mCurrFunction[num]++].obj-> func (__VA_ARGS__); \
00077 mCurrFunction[num] = curr; \
00078 return rv; \
00079 } \
00080 mCurrFunction[num] = curr; \
00081 }
00082
00083 template <typename T, typename T2>
00084 class WrapableInterface {
00085 protected:
00086 WrapableInterface () : mHandler (0) {};
00087 virtual ~WrapableInterface ()
00088 {
00089 if (mHandler)
00090 mHandler->unregisterWrap (static_cast<T2*> (this));
00091 };
00092
00093 void setHandler (T *handler, bool enabled = true)
00094 {
00095 if (mHandler)
00096 mHandler->unregisterWrap (static_cast<T2*> (this));
00097 if (handler)
00098 handler->registerWrap (static_cast<T2*> (this), enabled);
00099 mHandler = handler;
00100 }
00101 T *mHandler;
00102 };
00103
00104 template <typename T, unsigned int N>
00105 class WrapableHandler : public T
00106 {
00107 public:
00108 void registerWrap (T *, bool);
00109 void unregisterWrap (T *);
00110
00111 unsigned int numWrapClients () { return mInterface.size (); };
00112
00113 protected:
00114
00115 class Interface
00116 {
00117 public:
00118 T *obj;
00119 bool *enabled;
00120 };
00121
00122 WrapableHandler () : mInterface ()
00123 {
00124 mCurrFunction = new unsigned int [N];
00125 if (!mCurrFunction)
00126 abort ();
00127 for (unsigned int i = 0; i < N; i++)
00128 mCurrFunction[i] = 0;
00129 };
00130
00131 ~WrapableHandler ()
00132 {
00133 typename std::vector<Interface>::iterator it;
00134 for (it = mInterface.begin (); it != mInterface.end (); it++)
00135 delete [] (*it).enabled;
00136 mInterface.clear ();
00137 delete [] mCurrFunction;
00138 };
00139
00140 void functionSetEnabled (T *, unsigned int, bool);
00141
00142 unsigned int *mCurrFunction;
00143 std::vector<Interface> mInterface;
00144 };
00145
00146 template <typename T, unsigned int N>
00147 void WrapableHandler<T,N>::registerWrap (T *obj, bool enabled)
00148 {
00149 typename WrapableHandler<T,N>::Interface in;
00150 in.obj = obj;
00151 in.enabled = new bool [N];
00152 if (!in.enabled)
00153 return;
00154 for (unsigned int i = 0; i < N; i++)
00155 in.enabled[i] = enabled;
00156 mInterface.insert (mInterface.begin (), in);
00157 };
00158
00159 template <typename T, unsigned int N>
00160 void WrapableHandler<T,N>::unregisterWrap (T *obj)
00161 {
00162 typename std::vector<Interface>::iterator it;
00163 for (it = mInterface.begin (); it != mInterface.end (); it++)
00164 if ((*it).obj == obj)
00165 {
00166 delete [] (*it).enabled;
00167 mInterface.erase (it);
00168 break;
00169 }
00170 }
00171
00172 template <typename T, unsigned int N>
00173 void WrapableHandler<T,N>::functionSetEnabled (T *obj, unsigned int num,
00174 bool enabled)
00175 {
00176 for (unsigned int i = 0; i < mInterface.size (); i++)
00177 if (mInterface[i].obj == obj)
00178 {
00179 mInterface[i].enabled[num] = enabled;
00180 break;
00181 }
00182 }
00183
00184 #endif