| 1 | #ifndef VFACADE_HPP_
 | 
|---|
| 2 | #define VFACADE_HPP_
 | 
|---|
| 3 | 
 | 
|---|
| 4 | #include <iostream>
 | 
|---|
| 5 | #include <typeinfo>
 | 
|---|
| 6 | 
 | 
|---|
| 7 | #include <assert.h>
 | 
|---|
| 8 | #include <memory.h>
 | 
|---|
| 9 | 
 | 
|---|
| 10 | /**
 | 
|---|
| 11 |  * TODO: Doc
 | 
|---|
| 12 |  *
 | 
|---|
| 13 |  * @author Sebastian Mies <mies@tm.uka.de>
 | 
|---|
| 14 |  */
 | 
|---|
| 15 | /// the virtual object adaptor class
 | 
|---|
| 16 | template<class NonVirtual, class Virtual, class Adaptor>
 | 
|---|
| 17 | class vobject_hull : public Virtual {
 | 
|---|
| 18 | };
 | 
|---|
| 19 | 
 | 
|---|
| 20 | /**
 | 
|---|
| 21 |  * TODO: Doc
 | 
|---|
| 22 |  *
 | 
|---|
| 23 |  * @author Sebastian Mies <mies@tm.uka.de>
 | 
|---|
| 24 |  */
 | 
|---|
| 25 | /// the vfacade adaptor type
 | 
|---|
| 26 | struct vfacade_adaptor_type {
 | 
|---|
| 27 |         template<class NonVirtual>
 | 
|---|
| 28 |         class adaptor_type {
 | 
|---|
| 29 |         private:
 | 
|---|
| 30 |                 NonVirtual* obj_;
 | 
|---|
| 31 |         public:
 | 
|---|
| 32 |                 explicit inline adaptor_type() :
 | 
|---|
| 33 |                         obj_() {
 | 
|---|
| 34 |                 }
 | 
|---|
| 35 |                 explicit inline adaptor_type(NonVirtual& obj) :
 | 
|---|
| 36 |                         obj_(&obj) {
 | 
|---|
| 37 |                 }
 | 
|---|
| 38 |                 explicit inline adaptor_type(const NonVirtual& obj) :
 | 
|---|
| 39 |                         obj_(&obj) {
 | 
|---|
| 40 |                 }
 | 
|---|
| 41 |                 inline void set(NonVirtual& obj) {
 | 
|---|
| 42 |                         obj_ = &obj;
 | 
|---|
| 43 |                 }
 | 
|---|
| 44 |                 inline NonVirtual* operator->() {
 | 
|---|
| 45 |                         return obj_;
 | 
|---|
| 46 |                 }
 | 
|---|
| 47 |                 inline const NonVirtual* operator->() const {
 | 
|---|
| 48 |                         return obj_;
 | 
|---|
| 49 |                 }
 | 
|---|
| 50 |                 inline const NonVirtual& operator*() const {
 | 
|---|
| 51 |                         return *obj_;
 | 
|---|
| 52 |                 }
 | 
|---|
| 53 |                 inline NonVirtual& operator*() {
 | 
|---|
| 54 |                         return *obj_;
 | 
|---|
| 55 |                 }
 | 
|---|
| 56 |         };
 | 
|---|
| 57 | };
 | 
|---|
| 58 | 
 | 
|---|
| 59 | /**
 | 
|---|
| 60 |  * TODO: Doc
 | 
|---|
| 61 |  *
 | 
|---|
| 62 |  * @author Sebastian Mies <mies@tm.uka.de>
 | 
|---|
| 63 |  */
 | 
|---|
| 64 | /// the virtual capsule adaptor type
 | 
|---|
| 65 | struct vcapsule_adaptor_type {
 | 
|---|
| 66 |         template<class NonVirtual>
 | 
|---|
| 67 |         class adaptor_type {
 | 
|---|
| 68 |         private:
 | 
|---|
| 69 |                 NonVirtual obj_;
 | 
|---|
| 70 |         public:
 | 
|---|
| 71 |                 inline adaptor_type() :
 | 
|---|
| 72 |                         obj_() {
 | 
|---|
| 73 |                 }
 | 
|---|
| 74 |                 inline adaptor_type(const NonVirtual& obj) :
 | 
|---|
| 75 |                         obj_(obj) {
 | 
|---|
| 76 |                 }
 | 
|---|
| 77 |                 inline void assign(const NonVirtual& obj) {
 | 
|---|
| 78 |                         obj_ = obj;
 | 
|---|
| 79 |                 }
 | 
|---|
| 80 |                 inline NonVirtual* operator->() {
 | 
|---|
| 81 |                         return &obj_;
 | 
|---|
| 82 |                 }
 | 
|---|
| 83 |                 inline const NonVirtual* operator->() const {
 | 
|---|
| 84 |                         return &obj_;
 | 
|---|
| 85 |                 }
 | 
|---|
| 86 |                 inline const NonVirtual& operator*() const {
 | 
|---|
| 87 |                         return obj_;
 | 
|---|
| 88 |                 }
 | 
|---|
| 89 |                 inline NonVirtual& operator*() {
 | 
|---|
| 90 |                         return obj_;
 | 
|---|
| 91 |                 }
 | 
|---|
| 92 |         };
 | 
|---|
| 93 | };
 | 
|---|
| 94 | 
 | 
|---|
| 95 | /**
 | 
|---|
| 96 |  * TODO: Doc
 | 
|---|
| 97 |  *
 | 
|---|
| 98 |  * @author Sebastian Mies <mies@tm.uka.de>
 | 
|---|
| 99 |  */
 | 
|---|
| 100 | /// placeholder
 | 
|---|
| 101 | class vfacade_no_class {};
 | 
|---|
| 102 | 
 | 
|---|
| 103 | /**
 | 
|---|
| 104 |  * TODO: Doc
 | 
|---|
| 105 |  *
 | 
|---|
| 106 |  * @author Sebastian Mies <mies@tm.uka.de>
 | 
|---|
| 107 |  */
 | 
|---|
| 108 | /// a virtual fascade implementation
 | 
|---|
| 109 | template<class Virtual, class Extension = vfacade_no_class>
 | 
|---|
| 110 | class vfacade : public Extension {
 | 
|---|
| 111 | public:
 | 
|---|
| 112 |         typedef vfacade<Virtual> self;
 | 
|---|
| 113 | 
 | 
|---|
| 114 | private:
 | 
|---|
| 115 |         void* vtable;
 | 
|---|
| 116 |         void* vadaptor;
 | 
|---|
| 117 | 
 | 
|---|
| 118 |         template<class T>
 | 
|---|
| 119 |         void assign(T& obj) {
 | 
|---|
| 120 |                 typedef vobject_hull<T, Virtual, vfacade_adaptor_type> adaptor_type;
 | 
|---|
| 121 |                 adaptor_type adaptor(obj);
 | 
|---|
| 122 |                 assert( sizeof(adaptor_type) == sizeof(vfacade) );
 | 
|---|
| 123 |                 memcpy((void*) this, (void*) &adaptor, sizeof(vfacade));
 | 
|---|
| 124 |         }
 | 
|---|
| 125 | 
 | 
|---|
| 126 |         template<class T>
 | 
|---|
| 127 |         void assign(const T& obj) {
 | 
|---|
| 128 |                 T& obj_ = *const_cast<T*>(&obj);
 | 
|---|
| 129 |                 typedef vobject_hull<T, Virtual, vfacade_adaptor_type> adaptor_type;
 | 
|---|
| 130 |                 adaptor_type adaptor(obj_);
 | 
|---|
| 131 |                 assert( sizeof(adaptor_type) == sizeof(vfacade) );
 | 
|---|
| 132 |                 memcpy((void*) this, (void*) &adaptor, sizeof(vfacade));
 | 
|---|
| 133 |         }
 | 
|---|
| 134 | 
 | 
|---|
| 135 |         void assign(self& copy) {
 | 
|---|
| 136 |                 this->vtable = copy.vtable;
 | 
|---|
| 137 |                 this->vadaptor = copy.vadaptor;
 | 
|---|
| 138 |         }
 | 
|---|
| 139 | 
 | 
|---|
| 140 |         void assign(const self& copy) {
 | 
|---|
| 141 |                 this->vtable = copy.vtable;
 | 
|---|
| 142 |                 this->vadaptor = copy.vadaptor;
 | 
|---|
| 143 |         }
 | 
|---|
| 144 | 
 | 
|---|
| 145 | public:
 | 
|---|
| 146 |         /// constructs an undefined virtual facade
 | 
|---|
| 147 |         inline vfacade() {
 | 
|---|
| 148 |                 this->vtable = NULL;
 | 
|---|
| 149 |                 this->vadaptor = NULL;
 | 
|---|
| 150 |         }
 | 
|---|
| 151 | 
 | 
|---|
| 152 |         /// constructs an initialized virtual facade
 | 
|---|
| 153 |         template<class T>
 | 
|---|
| 154 |         inline vfacade(T& obj) {
 | 
|---|
| 155 |                 assign(*const_cast<T*>(&obj));
 | 
|---|
| 156 |         }
 | 
|---|
| 157 | 
 | 
|---|
| 158 |         /// assigns a new object to this facade
 | 
|---|
| 159 |         template<class T>
 | 
|---|
| 160 |         inline self& operator=(T& obj) {
 | 
|---|
| 161 |                 assign(obj);
 | 
|---|
| 162 |                 return *this;
 | 
|---|
| 163 |         }
 | 
|---|
| 164 | 
 | 
|---|
| 165 |         /// returns true, if the facade is unassigned
 | 
|---|
| 166 |         inline bool is_null() const {
 | 
|---|
| 167 |                 return this->vtable == NULL;
 | 
|---|
| 168 |         }
 | 
|---|
| 169 | 
 | 
|---|
| 170 |         inline operator Virtual* () {
 | 
|---|
| 171 |                 assert(vtable!=NULL);
 | 
|---|
| 172 |                 return (Virtual*) this;
 | 
|---|
| 173 |         }
 | 
|---|
| 174 | 
 | 
|---|
| 175 |         inline operator const Virtual* () const {
 | 
|---|
| 176 |                 assert(vtable!=NULL);
 | 
|---|
| 177 |                 return (const Virtual*) this;
 | 
|---|
| 178 |         }
 | 
|---|
| 179 | 
 | 
|---|
| 180 |         inline operator Virtual& () {
 | 
|---|
| 181 |                 assert(vtable!=NULL);
 | 
|---|
| 182 |                 return *(Virtual*) this;
 | 
|---|
| 183 |         }
 | 
|---|
| 184 | 
 | 
|---|
| 185 |         inline operator const Virtual& () const {
 | 
|---|
| 186 |                 assert(vtable!=NULL);
 | 
|---|
| 187 |                 return *(const Virtual*) this;
 | 
|---|
| 188 |         }
 | 
|---|
| 189 | 
 | 
|---|
| 190 |         /// returns the virtual object of the facade
 | 
|---|
| 191 |         inline Virtual* operator->() {
 | 
|---|
| 192 |                 assert(vtable!=NULL);
 | 
|---|
| 193 |                 return (Virtual*) this;
 | 
|---|
| 194 |         }
 | 
|---|
| 195 | 
 | 
|---|
| 196 |         /// returns a virtual object reference
 | 
|---|
| 197 |         inline Virtual& operator*() {
 | 
|---|
| 198 |                 assert(vtable!=NULL);
 | 
|---|
| 199 |                 return *((Virtual*) this);
 | 
|---|
| 200 |         }
 | 
|---|
| 201 | 
 | 
|---|
| 202 |         /// returns a virtual object reference
 | 
|---|
| 203 |         inline const Virtual& operator*() const {
 | 
|---|
| 204 |                 assert(vtable!=NULL);
 | 
|---|
| 205 |                 return *((const Virtual*) this);
 | 
|---|
| 206 |         }
 | 
|---|
| 207 | 
 | 
|---|
| 208 |         /// returns a constant virtual object of the facade
 | 
|---|
| 209 |         inline const Virtual* operator->() const {
 | 
|---|
| 210 |                 assert(vtable!=NULL);
 | 
|---|
| 211 |                 return (const Virtual*) this;
 | 
|---|
| 212 |         }
 | 
|---|
| 213 | };
 | 
|---|
| 214 | 
 | 
|---|
| 215 | /**
 | 
|---|
| 216 |  * TODO: Doc
 | 
|---|
| 217 |  *
 | 
|---|
| 218 |  * @author Sebastian Mies <mies@tm.uka.de>
 | 
|---|
| 219 |  */
 | 
|---|
| 220 | /// creates a virtual capsule for an object
 | 
|---|
| 221 | template<class Virtual, class NonVirtual>
 | 
|---|
| 222 | Virtual* vcapsule(const NonVirtual& obj) {
 | 
|---|
| 223 |         return new vobject_hull<NonVirtual, Virtual, vcapsule_adaptor_type> (obj);
 | 
|---|
| 224 | }
 | 
|---|
| 225 | 
 | 
|---|
| 226 | /**
 | 
|---|
| 227 |  * TODO: Doc
 | 
|---|
| 228 |  *
 | 
|---|
| 229 |  * @author Sebastian Mies <mies@tm.uka.de>
 | 
|---|
| 230 |  */
 | 
|---|
| 231 | /// returns the size in bytes of a virtual capsule
 | 
|---|
| 232 | template<class Virtual, class NonVirtual>
 | 
|---|
| 233 | size_t vcapsule_size(const NonVirtual& obj) {
 | 
|---|
| 234 |         return sizeof(vobject_hull<NonVirtual, Virtual, vcapsule_adaptor_type> (obj));
 | 
|---|
| 235 | }
 | 
|---|
| 236 | 
 | 
|---|
| 237 | #endif /* VFACADE_HPP_ */
 | 
|---|