MeVisLabToolboxReference
MeVisLab/Standard/Sources/ML/MLVesselGraph/Properties/mlPropertyAccessor.h
Go to the documentation of this file.
00001 // **InsertLicense** code
00002 //----------------------------------------------------------------------------------
00004 
00009 //----------------------------------------------------------------------------------
00010 
00011 #ifndef __MLPropertyAccessor_H
00012 #define __MLPropertyAccessor_H
00013 
00014 #include "mlVesselGraphSystem.h"
00015 
00016 #include "mlIntrusivePtrBase.h"
00017 #include "mlPropertyContainer.h"
00018 #include "mlPropertyIndex.h"
00019 #include "mlPropertyManager.h"
00020 
00021 #include "mlSystemWarningsDisable.h"
00022 #include <boost/mem_fn.hpp>
00023 #include <boost/type_traits/add_const.hpp>
00024 #include <boost/type_traits/add_reference.hpp>
00025 #include <boost/type_traits/remove_const.hpp>
00026 #include "mlSystemWarningsRestore.h"
00027 
00028 
00029 ML_START_NAMESPACE
00030 
00031 template<typename T>
00032 class DefaultPropertyAccessor : IntrusivePtrBase<DefaultPropertyAccessor<T> > {
00033 public:
00034   typedef DefaultPropertyAccessor<T> type;
00035   typedef boost::intrusive_ptr<type> PointerType;
00036   typedef boost::intrusive_ptr<const type> ConstPointerType;
00037 
00038   DefaultPropertyAccessor(const PropertyManager::Pointer& manager, const std::string& propertyName, const T& defaultValue) :
00039       _index(manager, propertyName), _defaultValue(defaultValue) {};
00040 
00041   T& operator()(PropertyContainer& container) const;
00042   const T& operator()(const PropertyContainer& container) const;
00043 
00044   const T& get(const PropertyContainer& container) const { return operator()(container); }
00045   void set(PropertyContainer& container, const T& value) const { operator()(container) = value; }
00046 
00047 private:
00048   SmartPropertyIndex _index;
00049   T _defaultValue;
00050 };
00051 
00052 template<typename T>
00053 T& DefaultPropertyAccessor<T>::operator()(PropertyContainer& container) const
00054 {
00055   if (_index.getManager()!=container.getPropertyManager()) {
00056     throw ML_BAD_PARAMETER;
00057   }
00058   if (!_index.isValid()) {
00059     container.getPropertyManager()->createProperty(_index.getName(), _defaultValue);
00060   }
00061   return container.property<T>(_index);
00062 }
00063 
00064 template<typename T>
00065 const T& DefaultPropertyAccessor<T>::operator()(const PropertyContainer& container) const
00066 {
00067   if (_index.getManager()!=container.getPropertyManager()) {
00068     throw ML_BAD_PARAMETER;
00069   }
00070   if (_index.isValid()) {
00071     return container.property<T>(_index);
00072   }
00073   else {
00074     return _defaultValue;
00075   }
00076 }
00077 
00078 
00079 ML_END_NAMESPACE
00080 
00081 #endif // __MLPropertyAccessor_H
00082 
00083