![]() |
User Manual, Developers Guide and API Documentation |
![]() |
#include <Chamaeleon.hpp>


Public Types | |
| typedef VALUETYPE | ValueType |
| Public access to VALUETYPE as ValueType. | |
Public Member Functions | |
| template<typename U> | |
| Chamaeleon (const Chamaeleon< U > &other) throw () | |
| Constructor to support conversion for upcasts. | |
| Chamaeleon (ValueType data) throw () | |
| Takes an instance of ValueType. | |
| template<typename U> | |
| Chamaeleon & | operator= (const Chamaeleon< U > &other) throw () |
| Assignment operator. | |
| ValueType | unHide () const throw () |
| Get back the "chamaelonized" data. | |
| virtual | ~Chamaeleon () throw () |
| Destructor. | |
Private Attributes | |
| ValueType | data_ |
| Data that is "chamaelonized". | |
Friends | |
| class | Chamaeleon |
| Chamaeleon<T> is friend of any Chamaeleon<U>. | |
Before you use this template always think twice. Maybe there is a better way. Probably there is a better way ;)
| VALUETYPE | The type you want chamaelonize |
// Classic classes for testing. They all have the same "test" // method which returns a different integer (depending on the // type). Furthermore A and C have a common base type (A). class A { public: virtual int test() { return 1; } virtual ~A() { } }; class B { public: virtual int test() { return 2; } virtual ~B() { } }; class C : public A { public: virtual int test() { return 3; } virtual ~C() { } };
Example:
// This example shows how objects without a common base class can be // stored in one container with the help of Chamaeleons Chamaeleon<A*>* a = new Chamaeleon<A*>(new A); Chamaeleon<B*>* b = new Chamaeleon<B*>(new B); // Create a vector that takes ChamaeleonBase* std::vector<ChamaeleonBase*> v; // Now we can put the Chamaeleons in the vector (even though the real // objects didn't have a common base class!) v.push_back(a); v.push_back(b); // to get them back we need to remember the position of the Chamaeleons Chamaeleon<A*>* a2 = v[0]->downCast<A*>(); Chamaeleon<B*>* b2 = v[1]->downCast<B*>(); // This should throw a wns::ChamaeleonBase::BadCast (since the type is wrong) ASSERT_THROW(v[1]->downCast<A*>(), wns::ChamaeleonBase::BadCast); // Finally check if unhiding works: ASSERT_EQUAL( 1, a2->unHide()->test() ); ASSERT_EQUAL( 2, b2->unHide()->test() );
Definition at line 139 of file Chamaeleon.hpp.
| typedef VALUETYPE wns::Chamaeleon< VALUETYPE >::ValueType |
Definition at line 150 of file Chamaeleon.hpp.
| wns::Chamaeleon< VALUETYPE >::Chamaeleon | ( | ValueType | data | ) | throw () [inline, explicit] |
| data | the data you want to "chamaelonize" |
// The Chamaeleon takes a pointer Chamaeleon<A*> a(new A);
Definition at line 161 of file Chamaeleon.hpp.
| wns::Chamaeleon< VALUETYPE >::Chamaeleon | ( | const Chamaeleon< U > & | other | ) | throw () [inline, explicit] |
Definition at line 174 of file Chamaeleon.hpp.
| virtual wns::Chamaeleon< VALUETYPE >::~Chamaeleon | ( | ) | throw () [inline, virtual] |
Definition at line 184 of file Chamaeleon.hpp.
| Chamaeleon& wns::Chamaeleon< VALUETYPE >::operator= | ( | const Chamaeleon< U > & | other | ) | throw () [inline] |
Definition at line 207 of file Chamaeleon.hpp.
| ValueType wns::Chamaeleon< VALUETYPE >::unHide | ( | ) | const throw () [inline] |
// Create three Chamaeleons Chamaeleon<B*> b(new B); Chamaeleon<C*> c(new C); Chamaeleon<A*> c_a(new C); // unHide the real objects B* bPtr = b.unHide(); C* cPtr = c.unHide(); A* c_aPtr = c_a.unHide(); // Check if everything works ASSERT_EQUAL( 2, bPtr->test() ); ASSERT_EQUAL( 3, cPtr->test() ); ASSERT_EQUAL( 3, c_aPtr->test() );
Definition at line 197 of file Chamaeleon.hpp.
friend class Chamaeleon [friend] |
Definition at line 145 of file Chamaeleon.hpp.
ValueType wns::Chamaeleon< VALUETYPE >::data_ [private] |
Definition at line 217 of file Chamaeleon.hpp.
1.5.5