torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Property.hpp
Go to the documentation of this file.
1 // Torc - Copyright 2011-2013 University of Southern California. All Rights Reserved.
2 // $HeadURL$
3 // $Id$
4 
5 // This program is free software: you can redistribute it and/or modify it under the terms of the
6 // GNU General Public License as published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
11 // the GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License along with this program. If
14 // not, see <http://www.gnu.org/licenses/>.
15 
16 #ifndef TORC_GENERIC_PROPERTY_HPP
17 #define TORC_GENERIC_PROPERTY_HPP
18 
20 
21 //BOOST
22 #ifdef GENOM_SERIALIZATION
23 #include <boost/serialization/access.hpp>
24 #endif //GENOM_SERIALIZATION
26 #include "torc/generic/Error.hpp"
31 #include "torc/generic/SymTab.hpp"
32 #include "torc/generic/Unit.hpp"
33 #include "torc/generic/Value.hpp"
37 #include <boost/shared_ptr.hpp>
38 
39 namespace torc { namespace generic { class BaseVisitor; } }
40 
41 namespace torc {
42 namespace generic {
43 
44 /**
45  * Represents an EDIF property. EDIF supports placing of simple and nested properties on different
46  * objects.
47  */
48 class Property : public Commentable, public Nameable, public Renamable, public SelfReferencing<
49  Property>, public Visitable {
50 #ifdef GENOM_SERIALIZATION
51  friend class boost::serialization::access;
52 #endif
53 
54  friend class FactoryType<Property> ;
55 
56 public:
57 
59 
60  /**
61  * Convenience class to create a property.
62  */
63  class Factory : public FactoryType<Property> {
64  public:
66  /**
67  * Create a property.
68  *
69  * @param[in] inName Name of the property to be created.
70  * @param[in] inContainer Pointer of the property container.
71  * @param[in] inValue Value of the property.
72  * @param[in] inUnit Unit of the property.
73  *
74  * @return Pointer to created property.
75  */
76  inline PropertySharedPtr virtual newPropertyPtr(const std::string& inName,
77  const PropertyContainerSharedPtr& inContainer, const Value& inValue,
78  const Unit& inUnit = eUnitUndefined,
79  const PropertySharedPtr& inParentProperty = PropertySharedPtr(),
80  const std::string& inOriginalName = std::string()) throw (Error);
81  };
82 
83  virtual void accept(BaseVisitor& inoutVisitor) throw (Error);
84 
85  virtual ~Property() throw ();
86 
87  /**
88  * Get the Property inSource.
89  *
90  * @return Value of property.
91  */
92  inline const Value getValue() const;
93 
94  /**
95  * Set the Property inSource.
96  *
97  * @param[in] inSource Value of property.
98  */
99  void setValue(const Value& inSource);
100 
101  /**
102  * Get the Unit inSource.
103  *
104  * @return Unit of property.
105  */
106  inline const Unit getUnit() const;
107 
108  /**
109  * Set the Property inSource unit.
110  *
111  * @param[in] inSource Unit of property.
112  */
113  void setUnit(const Unit& inSource);
114 
115  /**
116  * Get the owner/source of this property.
117  *
118  * @return string Owner of this property
119  */
120  inline const std::string getOwner() const;
121 
122  /**
123  * Set the owner/source of this property.
124  *
125  * @param[in] inSource Owner of this property
126  */
127  void setOwner(const std::string& inSource);
128 
129  /**
130  * Get the nested properties.
131  *
132  * @return map Containing properties
133  */
134  inline void getChildren(std::map<std::string, PropertySharedPtr>& outValues) const;
135 
136  /**
137  * Apply action on all children.
138  * @param[in] action Action to be applied
139  *
140  */
141  template <typename _Action> inline void applyOnAllChildren(const _Action& action) throw (Error);
142 
143  /**
144  * Set the nested properties.
145  *
146  * @param[in] inSource Map containing properties
147  */
148  void setChildren(const std::map<std::string, PropertySharedPtr>& inSource);
149 
150  /**
151  * Add a nested property to this property
152  *
153  * @param[in] inName Nested property name
154  * @param[in] inProperty Nested property
155  */
156  bool addChildProperty(const std::string& inName, const PropertySharedPtr& inProperty);
157 
158 protected:
159  Property();
160 
161 private:
162 #ifdef GENOM_SERIALIZATION
163  template <class Archive> void serialize(Archive& ar, unsigned int);
164 #endif //GENOM_SERIALIZATION
169 };
170 
171 /**
172  * Get the Property inSource.
173  *
174  * @return Value of property.
175  */
176 inline const Value Property::getValue() const {
177  return mValue;
178 }
179 
180 /**
181  * Get the Unit inSource.
182  *
183  * @return Unit of property.
184  */
185 inline const Unit Property::getUnit() const {
186  return mUnit;
187 }
188 
189 /**
190  * Get the owner/source of this property.
191  *
192  * @return string Owner of this property
193  */
194 inline const std::string Property::getOwner() const {
195  return mOwner;
196 }
197 
198 /**
199  * Get the nested properties.
200  *
201  * @param[out] outValues map Containing properties
202  */
203 inline void Property::getChildren(std::map<std::string, PropertySharedPtr>& outValues) const {
204  mChildren.getValueMap(outValues);
205 }
206 
207 template <typename _Action> inline void Property::applyOnAllChildren(const _Action& action)
208  throw (Error) {
209  try {
210  mChildren.applyOnAll(action);
211  } catch(Error& e) {
212  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
213  throw;
214  }
215 }
216 
217 /**
218  * Create a property.
219  *
220  * @param[in] inName Name of the property to be created.
221  * @param[in] inContainer Pointer of the property container.
222  * @param[in] inValue Value of the property.
223  * @param[in] inUnit Unit of the property.
224  *
225  * @return Pointer to created property.
226  */
227 
229  const PropertyContainerSharedPtr& inContainer, const Value& inValue, const Unit& inUnit,
230  const PropertySharedPtr& inParentProperty, const std::string& inOriginalName) throw (Error) {
231  try {
232  PropertySharedPtr newProperty;
233  create(newProperty);
234  newProperty->setName(inName);
235  newProperty->setValue(inValue);
236  newProperty->setUnit(inUnit);
237  newProperty->setOriginalName(inOriginalName);
238  if(inParentProperty) {
239  inParentProperty->addChildProperty(inName, newProperty);
240  } else {
241  inContainer->setProperty(inName, newProperty);
242  }
243  return newProperty;
244  } catch(Error& e) {
245  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
246  throw;
247  }
248 }
249 
250 } // namespace generic
251 } // namespace torc
252 
253 #endif // TORC_GENERIC_PROPERTY_HPP
An acyclic inoutVisitor implementation.
Definition: VisitorType.hpp:57
void applyOnAllChildren(const _Action &action)
Definition: Property.hpp:207
SymTab< std::string, PropertySharedPtr > mChildren
Definition: Property.hpp:168
boost::shared_ptr< PropertyContainer > PropertyContainerSharedPtr
Represents all classes that can hold user comments.
Definition: Commentable.hpp:36
virtual void accept(BaseVisitor &inoutVisitor)
Definition: Property.cpp:32
const Unit getUnit() const
Definition: Property.hpp:185
std::string string
bool addChildProperty(const std::string &inName, const PropertySharedPtr &inProperty)
Definition: Property.cpp:91
void getChildren(std::map< std::string, PropertySharedPtr > &outValues) const
Definition: Property.hpp:203
void setValue(const Value &inSource)
Definition: Property.cpp:50
The Error object thrown by different methods of EdifOM.
Definition: Error.hpp:41
A base class for Visitor.
Definition: VisitorType.hpp:31
void setChildren(const std::map< std::string, PropertySharedPtr > &inSource)
Definition: Property.cpp:77
void setOwner(const std::string &inSource)
Definition: Property.cpp:68
void getValueMap(UserMap &outMap) const
Definition: SymTab.hpp:171
const std::string getOwner() const
Definition: Property.hpp:194
void setUnit(const Unit &inSource)
Definition: Property.cpp:59
An object that has a name.
Definition: Nameable.hpp:34
boost::shared_ptr< Property > PropertySharedPtr
const Value getValue() const
Definition: Property.hpp:176
Represents objects that can be renamed.
VisitorType< Property > Visitor
Definition: Property.hpp:58
virtual PropertySharedPtr newPropertyPtr(const std::string &inName, const PropertyContainerSharedPtr &inContainer, const Value &inValue, const Unit &inUnit=eUnitUndefined, const PropertySharedPtr &inParentProperty=PropertySharedPtr(), const std::string &inOriginalName=std::string())
Definition: Property.hpp:228
A placeholder for a factory method.
Definition: FactoryType.hpp:35
An object that receives an inoutVisitor.
Definition: Visitable.hpp:38
void setCurrentLocation(const std::string &inFunction, const std::string &inFile, uint32_t inLine)
Definition: Error.cpp:73