SCIRun  5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PropertyManager.h
Go to the documentation of this file.
1 /*
2  For more information, please see: http://software.sci.utah.edu
3 
4  The MIT License
5 
6  Copyright (c) 2009 Scientific Computing and Imaging Institute,
7  University of Utah.
8 
9 
10  Permission is hereby granted, free of charge, to any person obtaining a
11  copy of this software and associated documentation files (the "Software"),
12  to deal in the Software without restriction, including without limitation
13  the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  and/or sell copies of the Software, and to permit persons to whom the
15  Software is furnished to do so, subject to the following conditions:
16 
17  The above copyright notice and this permission notice shall be included
18  in all copies or substantial portions of the Software.
19 
20  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  DEALINGS IN THE SOFTWARE.
27 */
28 
29 
30 ///
31 ///@file PropertyManager.h
32 ///
33 ///@author
34 /// Yarden Livnat
35 /// Department of Computer Science
36 /// University of Utah
37 ///@date March 2001
38 ///
39 ///@brief Manage dynamic properties of persistent objects.
40 ///
41 
42 #ifndef CORE_DATATYPES_PROPERTYMANAGER_H
43 #define CORE_DATATYPES_PROPERTYMANAGER_H 1
44 
49 #include <Core/Thread/Mutex.h>
50 #include <Core/Containers/Array1.h>
51 
52 #include <iostream>
53 #include <map>
54 
55 
57 
58 namespace SCIRun {
59 
61 {
62 public:
63  explicit PropertyBase(bool trans) : transient_(trans) {}
64  virtual PropertyBase* clone() const {
65  ASSERTFAIL("PropertyBase clone called");
66  }
67 
68  virtual void io(Piostream &) {}
70  virtual std::string dynamic_type_name() const;
71 
72  bool transient() const { return transient_; }
73  void set_transient(bool t) { transient_ = t; }
74 
75  virtual bool operator==(PropertyBase &/*pb*/) const {
76  ASSERTFAIL( "Not defined."); }
77  virtual bool operator!=(PropertyBase &/*pb*/) const {
78  ASSERTFAIL( "Not defined."); }
79 
80 protected:
81  /// Transient properties are deleted when the PropertyManager that this
82  /// Property belongs to is thawed.
83  bool transient_;
84  static Persistent *maker();
85 };
86 
87 
89 
90 template<class T>
91 class Property : public PropertyBase {
92 public:
93  friend class PropertyManager;
94 
95  Property(const T &o, bool trans) : PropertyBase(trans), obj_(o)
96  {
97  }
98  virtual ~Property() {}
99  virtual PropertyBase *clone() const
100  { return new Property(obj_, transient()); }
101 
102  static const std::string type_name(int n = -1);
103  virtual void io(Piostream &stream);
105  virtual std::string dynamic_type_name() const { return type_id.type; }
106 
107  virtual bool operator==(PropertyBase &pb) const {
108  const Property<T> *prop = dynamic_cast<Property<T> *>(&pb);
109 
110  if (prop && obj_ == prop->obj_ )
111  return true;
112 
113  return false;
114  }
115 
116  virtual bool operator!=(PropertyBase &pb) const {
117  const Property<T> *prop = dynamic_cast<Property<T> *>(&pb);
118 
119  if (prop && obj_ == prop->obj_ )
120  return false;
121 
122  return true;
123  }
124 
125 protected:
126  // Only Pio should use this constructor.
127  // Default is for objects read in to be non-transient.
128  Property() : PropertyBase(false) {}
129 
130 private:
131  T obj_;
132 
133  static Persistent *maker();
134 };
135 
136 
137 
138 /*
139  * Persistent Io
140  */
141 
142 const int PROPERTY_VERSION = 2;
143 
144 /*
145  * Property<T>
146  */
147 
148 template<class T>
149 const std::string Property<T>::type_name(int n)
150 {
151  if ( n == -1 ) {
152  const std::string name = TypeNameGenerator::make_template_id(type_name(0), type_name(1));
153  return name;
154  }
155  else if ( n == 0 ) {
156  const std::string nm("Property");
157  return nm;
158  }
159  else
160  return find_type_name( static_cast<T*>(0));
161 }
162 
163 template <class T>
165 Property<T>::type_id(type_name(-1), "PropertyBase", maker);
166 
167 template <class T>
168 Persistent*
170 {
171  // Properties in a file start out to be non-transient.
172  return new Property<T>();
173 }
174 
175 template<class T>
176 void
178 {
179  const int version = stream.begin_class( type_name(-1), PROPERTY_VERSION);
180  if (version > 1)
181  {
182  Pio(stream, transient_);
183  }
184  Pio(stream, obj_);
185  stream.end_class();
186 }
187 
188 
189 /*
190  * PropertyManager
191  */
192 
194 {
195 public:
196  PropertyManager();
197  PropertyManager(const PropertyManager &copy);
198  virtual ~PropertyManager();
199 
201  void copy_properties(const PropertyManager* src);
202 
203  bool operator==(const PropertyManager &pm);
204  bool operator!=(const PropertyManager &pm);
205 
206  template<class T> void set_property(const std::string &, const T &,
207  bool is_transient);
208  template<class T> bool get_property( const std::string &, T &);
209  bool is_property( const std::string & );
210  std::string get_property_name( size_t index );
211 
212  /// -- mutability --
213 
214  /// Transient data may only be stored in a frozen PropertyManager.
215  virtual void freeze();
216 
217  /// thaw will remove all transient properties from the PropertyManager.
218  virtual void thaw();
219 
220  /// query frozen state of a PropertyManager.
221  bool is_frozen() const { return frozen_; }
222 
223  void remove_property( const std::string & );
224  size_t nproperties() const { return properties_.size(); }
225  const std::map<std::string, PropertyBase *>& properties() const { return properties_; }
226 
227  void io(Piostream &stream);
229  virtual std::string dynamic_type_name() const;
230 
231  void set_name(const std::string& name)
232  {
233  set_property("name", name, false);
234  }
235  std::string get_name()
236  {
237  std::string name;
238  return get_property("name", name) ? name : std::string();
239  }
240 
241 private:
242 
243  typedef std::map<std::string, PropertyBase *> map_type;
244  map_type properties_;
245 
246 protected:
247 /// A frozen PropertyManager may store transient data.
248  void clear_transient();
249 
250  bool frozen_;
251 
253 };
254 
255 
256 template<class T>
257 void
258 PropertyManager::set_property(const std::string &name, const T& obj,
259  bool is_transient)
260 {
261  if (is_transient && (! is_frozen())) {
262  std::cerr << "WARNING::PropertyManager must be frozen to store transient data"
263  << " freezing now!" << std::endl;
264  freeze();
265  }
267  map_type::iterator loc = properties_.find(name);
268  if (loc != properties_.end())
269  {
270  delete loc->second;
271  }
272  properties_[name] = new Property<T>(obj, is_transient);
273 }
274 
275 
276 template<class T>
277 bool
278 PropertyManager::get_property(const std::string &name, T &ref)
279 {
281 
282  bool ans = false;
283  map_type::iterator loc = properties_.find(name);
284  if (loc != properties_.end()) {
285  const Property<T> *prop = dynamic_cast<const Property<T> *>(loc->second);
286  if (prop)
287  {
288  ref = prop->obj_;
289  ans = true;
290  }
291  }
292  return ans;
293 }
294 
295 
296 } // namespace SCIRun
297 
298 #endif // SCI_project_PropertyManager_h
virtual bool operator==(PropertyBase &) const
Definition: PropertyManager.h:75
specializations of template&lt;class T&gt; find_type_name() function for build-in and simple types not deri...
static PersistentTypeID type_id
Definition: PropertyManager.h:228
bool is_frozen() const
query frozen state of a PropertyManager.
Definition: PropertyManager.h:221
std::string get_property_name(size_t index)
Definition: PropertyManager.cc:230
Interface to dynamic 1D array class.
Definition: Persistent.h:89
const std::map< std::string, PropertyBase * > & properties() const
Definition: PropertyManager.h:225
virtual std::string dynamic_type_name() const
Definition: PropertyManager.cc:70
void set_property(const std::string &, const T &, bool is_transient)
Definition: PropertyManager.h:258
Definition: Persistent.h:187
const int PROPERTY_VERSION
Definition: PropertyManager.h:142
virtual void io(Piostream &)
Definition: PropertyManager.h:68
boost::mutex & get()
Definition: Mutex.h:49
#define SCISHARE
Definition: share.h:39
Utility for specifying data invariants (Assertions)
Definition: Mutex.h:43
PropertyBase(bool trans)
Definition: PropertyManager.h:63
void remove_property(const std::string &)
Definition: PropertyManager.cc:253
virtual PropertyBase * clone() const
Definition: PropertyManager.h:99
bool operator==(const PropertyManager &pm)
Definition: PropertyManager.cc:140
virtual int begin_class(const std::string &name, int current_version)
Definition: Persistent.cc:143
bool get_property(const std::string &, T &)
Definition: PropertyManager.h:278
const string find_type_name(float *)
Definition: TypeName.cc:63
void copy_properties(const PropertyManager *src)
Definition: PropertyManager.cc:115
virtual void thaw()
thaw will remove all transient properties from the PropertyManager.
Definition: PropertyManager.cc:189
static PersistentTypeID type_id
Definition: PropertyManager.h:104
Core::Thread::Mutex lock
Definition: PropertyManager.h:252
void set_name(const std::string &name)
Definition: PropertyManager.h:231
const char * name[]
Definition: BoostGraphExampleTests.cc:87
size_t nproperties() const
Definition: PropertyManager.h:224
static PersistentTypeID type_id
Definition: PropertyManager.h:69
PropertyManager & operator=(const PropertyManager &pm)
Definition: PropertyManager.cc:104
bool transient_
Definition: PropertyManager.h:83
Definition: PropertyManager.h:91
Persistent i/o for STL containers.
std::string type
Definition: Persistent.h:72
static const std::string type_name(int n=-1)
Definition: PropertyManager.h:149
virtual void freeze()
– mutability –
Definition: PropertyManager.cc:207
virtual std::string dynamic_type_name() const
Definition: PropertyManager.h:105
Definition: PropertyManager.h:60
static const std::string make_template_id(const std::string &templateName, const std::string &templateParam)
Definition: TypeName.h:62
bool operator!=(const PropertyManager &pm)
Definition: PropertyManager.cc:174
Property(const T &o, bool trans)
Definition: PropertyManager.h:95
#define ASSERTFAIL(string)
Definition: Assert.h:52
void Pio(Piostream &stream, Array1< T > &array)
Definition: Array1.h:65
Property()
Definition: PropertyManager.h:128
virtual void end_class()
Definition: Persistent.cc:178
virtual ~Property()
Definition: PropertyManager.h:98
std::string get_name()
Definition: PropertyManager.h:235
virtual bool operator==(PropertyBase &pb) const
Definition: PropertyManager.h:107
virtual bool operator!=(PropertyBase &pb) const
Definition: PropertyManager.h:116
bool frozen_
Definition: PropertyManager.h:250
Definition: Persistent.h:64
int n
Definition: eab.py:9
boost::lock_guard< boost::mutex > Guard
Definition: Mutex.h:55
void clear_transient()
A frozen PropertyManager may store transient data.
Definition: PropertyManager.cc:266
virtual PropertyBase * clone() const
Definition: PropertyManager.h:64
bool is_property(const std::string &)
Definition: PropertyManager.cc:216
virtual void io(Piostream &stream)
Definition: PropertyManager.h:177
Definition: PropertyManager.h:193
virtual bool operator!=(PropertyBase &) const
Definition: PropertyManager.h:77
virtual ~PropertyManager()
Definition: PropertyManager.cc:180
void set_transient(bool t)
Definition: PropertyManager.h:73
void io(Piostream &stream)
Definition: PropertyManager.cc:290
PropertyManager()
Definition: PropertyManager.cc:73