embeddable.h

Go to the documentation of this file.
00001 // -*- Mode: C++; tab-width: 2; -*-
00002 // vi: set ts=2:
00003 //
00004 // $Id: embeddable.h,v 1.21 2005/12/23 17:01:40 amoll Exp $
00005 //
00006 
00007 #ifndef BALL_CONCEPT_EMBEDDABLE_H
00008 #define BALL_CONCEPT_EMBEDDABLE_H
00009 
00010 #ifndef BALL_COMMON_H
00011 # include <BALL/common.h>
00012 #endif
00013 
00014 #ifndef BALL_DATATYPE_LIST_H
00015 # include <BALL/DATATYPE/list.h>
00016 #endif
00017 
00018 #ifndef BALL_DATATYPE_STRINGHASHMAP_H
00019 # include <BALL/DATATYPE/stringHashMap.h>
00020 #endif
00021 
00022 #ifndef BALL_DATATYPE_HASHMAP_H
00023 # include <BALL/DATATYPE/hashMap.h>
00024 #endif
00025 
00026 #include <typeinfo>
00027 
00028 namespace BALL 
00029 {
00030 
00037   #define BALL_EMBEDDABLE(TYPE,BASE)\
00038     virtual void registerThis()  \
00039     { \
00040       if (typeid(*this) != typeid(TYPE))\
00041       {\
00042         Log.error() << "Warning: derived class " << typeid(*this).name() << " was derived from BALL::Embeddable, but the macro\n"\
00043                   << "BALL_EMBEDDABLE(...) was not specified in the class declaration!" << std::endl;\
00044       }\
00045       Embeddable::registerInstance_(typeid(TYPE), this);\
00046       Embeddable::registerInstance_(typeid(BASE), this);\
00047     }\
00048     \
00049     static TYPE* getInstance(Position index)  \
00050     { \
00051       Embeddable* ptr = Embeddable::getInstance_(typeid(TYPE), index);\
00052       if (ptr != 0)\
00053       {\
00054         return dynamic_cast<TYPE*>(ptr); \
00055       }\
00056       else\
00057       {\
00058         return 0;\
00059       }\
00060     }\
00061     static TYPE* getInstance(const String& identifier) \
00062     {\
00063       Embeddable* ptr = Embeddable::getInstance_(typeid(TYPE), identifier);\
00064       if (ptr != 0)\
00065       {\
00066         return dynamic_cast<TYPE*>(ptr); \
00067       }\
00068       else\
00069       {\
00070         return 0;\
00071       }\
00072     }\
00073     static Size countInstances()  { return (Embeddable::countInstances_(typeid(TYPE))); };
00074   
00087   class BALL_EXPORT Embeddable
00088   {
00089     public:
00090 
00094     typedef std::vector<Embeddable*> EmbeddableVector;
00096 
00100 
00103     Embeddable(const String& identifier = "<Embeddable>")
00104       ;
00105     
00108     Embeddable(const Embeddable& embeddable)
00109       ;
00110     
00113     virtual ~Embeddable()
00114       ;
00116 
00120 
00123     void setIdentifier(const String& identifier)
00124       ;
00125 
00128     const String& getIdentifier() const
00129       ;
00130 
00133     void unregisterThis()
00134       ;
00135 
00142     virtual void registerThis()
00143       ; 
00144 
00146     
00153     virtual void dump(std::ostream& s = std::cout, Size depth = 0) const
00154       ;
00155 
00156     protected:
00159     static void registerInstance_(const std::type_info& type, const Embeddable* instance)
00160       ;
00163     static void unregisterInstance_(const Embeddable* instance)
00164       ;
00165     
00168     static Size countInstances_(const std::type_info& type)
00169       ;
00170 
00175     static Embeddable* getInstance_(const std::type_info& type, Position index)
00176       ;
00177 
00181     static Embeddable* getInstance_(const std::type_info& type, const String& identifier)
00182       ;
00183 
00184 
00185     private:
00186     
00189     String  identifier_;
00190 
00193     static StringHashMap<EmbeddableVector>  instance_vectors_;
00194 
00197     static HashMap<Embeddable*, string>   instance_to_type_map_;
00198   };
00199 
00200 } // namespace BALL
00201 
00202 #endif // BALL_CONCEPT_EMBEDDABLE_H