User Manual, Developers Guide and API Documentation

wns::Chamaeleon< VALUETYPE > Class Template Reference

Helps to put objects of different type into one container. More...

#include <Chamaeleon.hpp>

Inheritance diagram for wns::Chamaeleon< VALUETYPE >:

Inheritance graph
[legend]
Collaboration diagram for wns::Chamaeleon< VALUETYPE >:

Collaboration graph
[legend]

List of all members.

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>
Chamaeleonoperator= (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>.


Detailed Description

template<typename VALUETYPE>
class wns::Chamaeleon< VALUETYPE >

Author:
Marc Schinnenburg <marc@schinnenburg.net>
The idea is to use this template to give any type the same base class (ChamaeleonBase). This can be done without changing the definition of the type itself.

Note:
Regarding storage the Chamaeleon behaves like a STL-Container -> References are not delete, value types are copied around

Before you use this template always think twice. Maybe there is a better way. Probably there is a better way ;)

Parameters:
VALUETYPE The type you want chamaelonize
The classes A, B and C used in the examples here are defined as follows:
// 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.


Member Typedef Documentation

template<typename VALUETYPE>
typedef VALUETYPE wns::Chamaeleon< VALUETYPE >::ValueType

Definition at line 150 of file Chamaeleon.hpp.


Constructor & Destructor Documentation

template<typename VALUETYPE>
wns::Chamaeleon< VALUETYPE >::Chamaeleon ( ValueType  data  )  throw () [inline, explicit]

Parameters:
data the data you want to "chamaelonize"
Example:
// The Chamaeleon takes a pointer
Chamaeleon<A*> a(new A);

Definition at line 161 of file Chamaeleon.hpp.

template<typename VALUETYPE>
template<typename U>
wns::Chamaeleon< VALUETYPE >::Chamaeleon ( const Chamaeleon< U > &  other  )  throw () [inline, explicit]

Definition at line 174 of file Chamaeleon.hpp.

template<typename VALUETYPE>
virtual wns::Chamaeleon< VALUETYPE >::~Chamaeleon (  )  throw () [inline, virtual]

Definition at line 184 of file Chamaeleon.hpp.


Member Function Documentation

template<typename VALUETYPE>
template<typename U>
Chamaeleon& wns::Chamaeleon< VALUETYPE >::operator= ( const Chamaeleon< U > &  other  )  throw () [inline]

Definition at line 207 of file Chamaeleon.hpp.

template<typename VALUETYPE>
ValueType wns::Chamaeleon< VALUETYPE >::unHide (  )  const throw () [inline]

Returns:
data that is "chamaelonized"
Example:
// 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.


Friends And Related Function Documentation

template<typename VALUETYPE>
friend class Chamaeleon [friend]

Definition at line 145 of file Chamaeleon.hpp.


Member Data Documentation

template<typename VALUETYPE>
ValueType wns::Chamaeleon< VALUETYPE >::data_ [private]

Definition at line 217 of file Chamaeleon.hpp.


The documentation for this class was generated from the following file:

Generated on Sat May 26 03:32:50 2012 for openWNS by  doxygen 1.5.5