MeVisLab Toolbox Reference
mlPropertyAccessor.h
Go to the documentation of this file.
1 /*************************************************************************************
2 **
3 ** Copyright 2010, MeVis Medical Solutions AG
4 **
5 ** The user may use this file in accordance with the license agreement provided with
6 ** the Software or, alternatively, in accordance with the terms contained in a
7 ** written agreement between the user and MeVis Medical Solutions AG.
8 **
9 ** For further information use the contact form at https://www.mevislab.de/contact
10 **
11 **************************************************************************************/
12 
13 #ifndef ML_PROPERTY_ACCESSOR_H
14 #define ML_PROPERTY_ACCESSOR_H
15 
16 
18 
19 #include "mlVesselGraphSystem.h"
20 
21 #include "mlIntrusivePtrBase.h"
22 #include "mlPropertyContainer.h"
23 #include "mlPropertyIndex.h"
24 #include "mlPropertyManager.h"
25 
27 #include <boost/mem_fn.hpp>
28 #include <boost/type_traits/add_const.hpp>
29 #include <boost/type_traits/add_reference.hpp>
30 #include <boost/type_traits/remove_const.hpp>
32 
33 #ifdef _MSC_VER
34 #pragma warning( push )
35 #pragma warning( disable : 4344 )
36 #endif
37 
39 
40 template<typename T>
41 class DefaultPropertyAccessor : IntrusivePtrBase<DefaultPropertyAccessor<T> > {
42 public:
44  typedef boost::intrusive_ptr<type> PointerType;
45  typedef boost::intrusive_ptr<const type> ConstPointerType;
46 
47  DefaultPropertyAccessor(const PropertyManager::Pointer& manager, const std::string& propertyName, const T& defaultValue) :
48  _index(manager, propertyName), _defaultValue(defaultValue) {};
49 
50  T& operator()(PropertyContainer& container) const;
51  const T& operator()(const PropertyContainer& container) const;
52 
53  const T& get(const PropertyContainer& container) const { return operator()(container); }
54  void set(PropertyContainer& container, const T& value) const { operator()(container) = value; }
55 
56 private:
57  SmartPropertyIndex _index;
58  T _defaultValue;
59 };
60 
61 template<typename T>
63 {
64  if (_index.getManager()!=container.getPropertyManager()) {
65  throw ML_BAD_PARAMETER;
66  }
67  if (!_index.isValid()) {
68  container.getPropertyManager()->createProperty(_index.getName(), _defaultValue);
69  }
70  return container.property<T>(_index);
71 }
72 
73 template<typename T>
75 {
76  if (_index.getManager()!=container.getPropertyManager()) {
77  throw ML_BAD_PARAMETER;
78  }
79  if (_index.isValid()) {
80  return container.property<T>(_index);
81  }
82  else {
83  return _defaultValue;
84  }
85 }
86 
87 
89 
90 #ifdef _MSC_VER
91 #pragma warning( pop )
92 #endif
93 
94 #endif // __MLPropertyAccessor_H
@ T
Definition: SoKeyGrabber.h:71
Project global and OS specific declarations.
DefaultPropertyAccessor(const PropertyManager::Pointer &manager, const std::string &propertyName, const T &defaultValue)
DefaultPropertyAccessor< T > type
T & operator()(PropertyContainer &container) const
boost::intrusive_ptr< type > PointerType
boost::intrusive_ptr< const type > ConstPointerType
void set(PropertyContainer &container, const T &value) const
const T & get(const PropertyContainer &container) const
A base-class enabling the use of boost::intrusive_ptr in derived classes.
const PropertyManager * getPropertyManager() const
Get read-only access to the associated property manager.
const PropertyValue & property(size_t index) const
Get the property with index index as PropertyValue.
boost::intrusive_ptr< type > Pointer
size_t createProperty(const std::string &name, const PropertyValue &defaultValue)
Create property with name name and return its' index.
A self-updating index for efficient name-based access of properties.
#define ML_BAD_PARAMETER
A bad/invalid parameter (or even an inappropriate image) has been passed to a module or an algorithm;...
Definition: mlTypeDefs.h:925
#define VESSELGRAPH_END_NAMESPACE
#define VESSELGRAPH_BEGIN_NAMESPACE