MeVisLabToolboxReference
MeVisLab/Standard/Sources/ML/MLVesselGraph/Properties/mlPropertyManager.h
Go to the documentation of this file.
00001 // **InsertLicense** code
00002 //----------------------------------------------------------------------------------
00004 
00009 //----------------------------------------------------------------------------------
00010 
00011 #ifndef __MLPropertyManager_H
00012 #define __MLPropertyManager_H
00013 
00014 #include "mlVesselGraphSystem.h"
00015 
00016 #include "mlIntrusivePtrBase.h"
00017 #include "mlPropertyValue.h"
00018 
00019 #include <vector>
00020 #include <boost/intrusive_ptr.hpp>
00021 
00022 #include <mlBase.h>
00023 #include <mlTrace.h>
00024 
00025 ML_START_NAMESPACE
00026 
00027 class PropertyContainer;
00028 class SmartPropertyIndex;
00029 
00030 class VESSELGRAPH_EXPORT PropertyManager : public Base, public IntrusivePtrBase<PropertyManager> {
00031 public:
00032   typedef PropertyManager type;
00033   typedef boost::intrusive_ptr<type> Pointer;
00034   typedef boost::intrusive_ptr<const type> ConstPointer;
00035   typedef std::map<std::string, PropertyValue> PropertyMap;
00036 
00038   PropertyManager();
00040   PropertyManager(const PropertyManager& other);
00041   ~PropertyManager();
00042 
00043   template <typename T>
00044   static void logObjectConstructor(const T* object);
00045 
00046   template <typename T>
00047   static void logObjectDestructor(const T* object);
00048 
00049   static void allowDestruction(void* location);
00050 
00052   PropertyManager& operator=(const PropertyManager& item);
00053 
00055   bool operator==(const PropertyManager& other) const;
00056 
00058   void clearProperties();
00059 
00062   size_t createProperty(const std::string& name, const PropertyValue& defaultValue);
00063 
00065   const PropertyValue& getPropertyDefault(size_t propertyIndex)            const { return _defaultValues[propertyIndex]; }
00066   const PropertyValue& getPropertyDefault(const std::string& propertyName) const { return _defaultValues[getPropertyIndex(propertyName)]; }
00067 
00069   PropertyMap getPropertyDefaultMap() const;
00070 
00072   size_t getPropertyIndex(const std::string& propertyName) const;
00073 
00075   const std::string& getPropertyName(size_t index) const { return _names[index]; }
00076 
00078   const std::vector<std::string>& getPropertyNames() const { return _names; }
00079 
00081   template<typename T>
00082   std::vector<std::string> getPropertyNames() const;
00083 
00085   bool hasProperty(const std::string& name) const { return std::find(_names.begin(), _names.end(), name)!=_names.end(); }
00086 
00088   size_t numberOfProperties() const { return _names.size(); }
00089 
00091   void removeProperty(const std::string& name);
00092 
00094 
00095 
00096   virtual void addStateToTree(TreeNode* parent) const;
00097 
00099   ML_SET_ADDSTATE_VERSION(1);
00100 
00102   virtual void readStateFromTree(TreeNode* parent);
00104 
00105 private:
00106   friend class PropertyContainer;
00107   friend class SmartPropertyIndex;
00108   static std::set<const void*> destructableObjects;
00109 
00113   void _register(PropertyContainer* properties)   { _propertyContainers.insert(properties); }
00115   void _unregister(PropertyContainer* properties) { _propertyContainers.erase(properties); }
00116 
00118   void _register(SmartPropertyIndex* index)   { _smartIndices.insert(index); }
00120   void _unregister(SmartPropertyIndex* index) { _smartIndices.erase(index); }
00121 
00123   void _notifySmartIndices() const;
00124 
00126   std::vector<std::string> _names;
00127   
00129   std::vector<PropertyValue> _defaultValues;
00130 
00132   std::set<PropertyContainer*> _propertyContainers;
00134   std::set<SmartPropertyIndex*> _smartIndices;
00135 
00136   ML_CLASS_HEADER(PropertyManager);
00137 };
00138 
00139 template<typename T>
00140 std::vector<std::string> PropertyManager::getPropertyNames() const
00141 {
00142   BOOST_MPL_ASSERT(( boost::mpl::contains<PropertyValue::SupportedPropertyTypes, T> ));
00143   std::vector<std::string> names;
00144 
00145   T t;
00146   PropertyValue v(t);
00147   const size_t which = v.which();
00148   for(size_t i=0;i<_defaultValues.size(); ++i) {
00149     if (_defaultValues[i].which()==which) {
00150       names.push_back(_names[i]);
00151     }
00152   }
00153 
00154   return names;
00155 }
00156 
00157 #include <typeinfo>
00158 #include <iostream>
00159 #include <fstream>
00160 
00161 template <typename T>
00162 void PropertyManager::logObjectConstructor(const T* object)
00163 {
00164 /*  std::ofstream debugFile;
00165   debugFile.open("d:\\Tmp\\debug.log", std::ios::out | std::ios::app);
00166   debugFile << "Constructed object of type " << typeid(T).name() << " at memory location " << object << std::endl;
00167   debugFile.close();
00168 */
00169 }
00170 
00171 template <typename T>
00172 void PropertyManager::logObjectDestructor(const T* object)
00173 {
00174 /*
00175   std::ofstream debugFile;
00176   debugFile.open("d:\\Tmp\\debug.log", std::ios::out | std::ios::app);
00177   const void* location = object;
00178   if (destructableObjects.find(location)==destructableObjects.end()) {
00179     debugFile << "WARNING: The following object is destructed prematurely!!!" << std::endl;
00180     debugFile << "MLStackTrace:" << std::endl;
00181     for (MLuint16 i=0; i<MLGlobalTraceBuffer.getTraceStackTop(); ++i) {
00182       debugFile << MLGlobalTraceBuffer.getTraceStack()[i] << std::endl;
00183     }
00184   }
00185   debugFile << "Destructed object of type " << typeid(T).name() << " at memory location " << object << std::endl;
00186   debugFile.close();
00187 */
00188 }
00189 
00190 ML_END_NAMESPACE
00191 
00192 #endif // __MLPropertyManager_H