BALL  1.4.1
embeddable.h
Go to the documentation of this file.
00001 // -*- Mode: C++; tab-width: 2; -*-
00002 // vi: set ts=2:
00003 //
00004 
00005 #ifndef BALL_CONCEPT_EMBEDDABLE_H
00006 #define BALL_CONCEPT_EMBEDDABLE_H
00007 
00008 #ifndef BALL_COMMON_H
00009 # include <BALL/common.h>
00010 #endif
00011 
00012 #ifndef BALL_DATATYPE_STRINGHASHMAP_H
00013 # include <BALL/DATATYPE/stringHashMap.h>
00014 #endif
00015 
00016 #ifndef BALL_DATATYPE_HASHMAP_H
00017 # include <BALL/DATATYPE/hashMap.h>
00018 #endif
00019 
00020 #include <typeinfo>
00021 
00022 namespace BALL 
00023 {
00024 
00031   #define BALL_EMBEDDABLE(TYPE,BASE)\
00032     virtual void registerThis()  \
00033     { \
00034       if (typeid(*this) != typeid(TYPE))\
00035       {\
00036         Log.error() << "Warning: derived class " << typeid(*this).name() << " was derived from BALL::Embeddable, but the macro\n"\
00037                   << "BALL_EMBEDDABLE(...) was not specified in the class declaration!" << std::endl;\
00038       }\
00039       Embeddable::registerInstance_(typeid(TYPE), this);\
00040       Embeddable::registerInstance_(typeid(BASE), this);\
00041     }\
00042     \
00043     static TYPE* getInstance(Position index)  \
00044     { \
00045       Embeddable* ptr = Embeddable::getInstance_(typeid(TYPE), index);\
00046       if (ptr != 0)\
00047       {\
00048         return dynamic_cast<TYPE*>(ptr); \
00049       }\
00050       else\
00051       {\
00052         return 0;\
00053       }\
00054     }\
00055     static TYPE* getInstance(const String& identifier) \
00056     {\
00057       Embeddable* ptr = Embeddable::getInstance_(typeid(TYPE), identifier);\
00058       if (ptr != 0)\
00059       {\
00060         return dynamic_cast<TYPE*>(ptr); \
00061       }\
00062       else\
00063       {\
00064         return 0;\
00065       }\
00066     }\
00067     static Size countInstances()  { return (Embeddable::countInstances_(typeid(TYPE))); };
00068   
00081   class BALL_EXPORT Embeddable
00082   {
00083     public:
00084 
00088     typedef std::vector<Embeddable*> EmbeddableVector;
00090 
00094 
00097     Embeddable(const String& identifier = "<Embeddable>")
00098       ;
00099     
00102     Embeddable(const Embeddable& embeddable)
00103       ;
00104     
00107     virtual ~Embeddable()
00108       ;
00110 
00114 
00117     void setIdentifier(const String& identifier)
00118       ;
00119 
00122     const String& getIdentifier() const
00123       ;
00124 
00127     void unregisterThis()
00128       ;
00129 
00136     virtual void registerThis()
00137       ; 
00138 
00140     
00147     virtual void dump(std::ostream& s = std::cout, Size depth = 0) const
00148       ;
00149 
00150     protected:
00153     static void registerInstance_(const std::type_info& type, const Embeddable* instance)
00154       ;
00157     static void unregisterInstance_(const Embeddable* instance)
00158       ;
00159     
00162     static Size countInstances_(const std::type_info& type)
00163       ;
00164 
00169     static Embeddable* getInstance_(const std::type_info& type, Position index)
00170       ;
00171 
00175     static Embeddable* getInstance_(const std::type_info& type, const String& identifier)
00176       ;
00177 
00178 
00179     private:
00180     
00183     String  identifier_;
00184 
00187     static StringHashMap<EmbeddableVector>  instance_vectors_;
00188 
00191     static HashMap<Embeddable*, string>   instance_to_type_map_;
00192   };
00193 
00194 } // namespace BALL
00195 
00196 #endif // BALL_CONCEPT_EMBEDDABLE_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines