HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Property.h
Go to the documentation of this file.
1 //
2 // Copyright Contributors to the MaterialX Project
3 // SPDX-License-Identifier: Apache-2.0
4 //
5 
6 #ifndef MATERIALX_PROPERTY_H
7 #define MATERIALX_PROPERTY_H
8 
9 /// @file
10 /// Property element subclasses
11 
12 #include <MaterialXCore/Export.h>
13 
14 #include <MaterialXCore/Geom.h>
15 
17 
18 class Property;
19 class PropertyAssign;
20 class PropertySet;
21 class PropertySetAssign;
22 
23 /// A shared pointer to a Property
24 using PropertyPtr = shared_ptr<Property>;
25 /// A shared pointer to a const Property
26 using ConstPropertyPtr = shared_ptr<const Property>;
27 
28 /// A shared pointer to a PropertyAssign
29 using PropertyAssignPtr = shared_ptr<PropertyAssign>;
30 /// A shared pointer to a const PropertyAssign
31 using ConstPropertyAssignPtr = shared_ptr<const PropertyAssign>;
32 
33 /// A shared pointer to a PropertySet
34 using PropertySetPtr = shared_ptr<PropertySet>;
35 /// A shared pointer to a const PropertySet
36 using ConstPropertySetPtr = shared_ptr<const PropertySet>;
37 
38 /// A shared pointer to a PropertySetAssign
39 using PropertySetAssignPtr = shared_ptr<PropertySetAssign>;
40 /// A shared pointer to a const PropertySetAssign
41 using ConstPropertySetAssignPtr = shared_ptr<const PropertySetAssign>;
42 
43 /// @class Property
44 /// A property element within a PropertySet.
46 {
47  public:
48  Property(ElementPtr parent, const string& name) :
49  ValueElement(parent, CATEGORY, name)
50  {
51  }
52  virtual ~Property() { }
53 
54  public:
55  static const string CATEGORY;
56 };
57 
58 /// @class PropertyAssign
59 /// A property assignment element within a Look.
61 {
62  public:
63  PropertyAssign(ElementPtr parent, const string& name) :
64  ValueElement(parent, CATEGORY, name)
65  {
66  }
67  virtual ~PropertyAssign() { }
68 
69  /// @name Property
70  /// @{
71 
72  /// Set the property string of this element.
73  void setProperty(const string& property)
74  {
75  setAttribute(PROPERTY_ATTRIBUTE, property);
76  }
77 
78  /// Return true if this element has a property string.
79  bool hasProperty() const
80  {
81  return hasAttribute(PROPERTY_ATTRIBUTE);
82  }
83 
84  /// Return the property string of this element.
85  const string& getProperty() const
86  {
87  return getAttribute(PROPERTY_ATTRIBUTE);
88  }
89 
90  /// @}
91  /// @name Geometry
92  /// @{
93 
94  /// Set the geometry string of this element.
95  void setGeom(const string& geom)
96  {
97  setAttribute(GEOM_ATTRIBUTE, geom);
98  }
99 
100  /// Return true if this element has a geometry string.
101  bool hasGeom() const
102  {
103  return hasAttribute(GEOM_ATTRIBUTE);
104  }
105 
106  /// Return the geometry string of this element.
107  const string& getGeom() const
108  {
109  return getAttribute(GEOM_ATTRIBUTE);
110  }
111 
112  /// @}
113  /// @name Collection
114  /// @{
115 
116  /// Set the collection string of this element.
117  void setCollectionString(const string& collection)
118  {
119  setAttribute(COLLECTION_ATTRIBUTE, collection);
120  }
121 
122  /// Return true if this element has a collection string.
123  bool hasCollectionString() const
124  {
125  return hasAttribute(COLLECTION_ATTRIBUTE);
126  }
127 
128  /// Return the collection string of this element.
129  const string& getCollectionString() const
130  {
131  return getAttribute(COLLECTION_ATTRIBUTE);
132  }
133 
134  /// Assign a Collection to this element.
135  void setCollection(ConstCollectionPtr collection);
136 
137  /// Return the Collection that is assigned to this element.
138  CollectionPtr getCollection() const;
139 
140  /// @}
141 
142  public:
143  static const string CATEGORY;
144  static const string PROPERTY_ATTRIBUTE;
145  static const string GEOM_ATTRIBUTE;
146  static const string COLLECTION_ATTRIBUTE;
147 };
148 
149 /// @class PropertySet
150 /// A property set element within a Document.
152 {
153  public:
154  PropertySet(ElementPtr parent, const string& name) :
155  Element(parent, CATEGORY, name)
156  {
157  }
158  virtual ~PropertySet() { }
159 
160  /// @name Properties
161  /// @{
162 
163  /// Add a Property to the set.
164  /// @param name The name of the new Property.
165  /// If no name is specified, then a unique name will automatically be
166  /// generated.
167  /// @return A shared pointer to the new Property.
168  PropertyPtr addProperty(const string& name)
169  {
170  return addChild<Property>(name);
171  }
172 
173  /// Return the Property, if any, with the given name.
174  PropertyPtr getProperty(const string& name) const
175  {
176  return getChildOfType<Property>(name);
177  }
178 
179  /// Return a vector of all Property elements in the set.
180  vector<PropertyPtr> getProperties() const
181  {
182  return getChildrenOfType<Property>();
183  }
184 
185  /// Remove the Property with the given name, if present.
186  void removeProperty(const string& name)
187  {
188  removeChildOfType<Property>(name);
189  }
190 
191  /// @}
192  /// @name Values
193  /// @{
194 
195  /// Set the typed value of a property by its name, creating a child element
196  /// to hold the property if needed.
197  template <class T> PropertyPtr setPropertyValue(const string& name,
198  const T& value,
199  const string& type = EMPTY_STRING)
200  {
201  PropertyPtr property = getChildOfType<Property>(name);
202  if (!property)
203  property = addProperty(name);
204  property->setValue(value, type);
205  return property;
206  }
207 
208  /// Return the typed value of a property by its name.
209  /// @param name The name of the property to be evaluated.
210  /// @return If the given property is found, then a shared pointer to its
211  /// value is returned; otherwise, an empty shared pointer is returned.
212  ValuePtr getPropertyValue(const string& name) const
213  {
214  PropertyPtr property = getProperty(name);
215  return property ? property->getValue() : ValuePtr();
216  }
217 
218  /// @}
219 
220  public:
221  static const string CATEGORY;
222 };
223 
224 /// @class PropertySetAssign
225 /// A property set assignment element within a Look.
227 {
228  public:
229  PropertySetAssign(ElementPtr parent, const string& name) :
230  GeomElement(parent, CATEGORY, name)
231  {
232  }
233  virtual ~PropertySetAssign() { }
234 
235  /// @name Property Set
236  /// @{
237 
238  /// Set the property set string of this element.
239  void setPropertySetString(const string& propertySet)
240  {
241  setAttribute(PROPERTY_SET_ATTRIBUTE, propertySet);
242  }
243 
244  /// Return true if this element has a property set string.
245  bool hasPropertySetString() const
246  {
247  return hasAttribute(PROPERTY_SET_ATTRIBUTE);
248  }
249 
250  /// Return the property set string of this element.
251  const string& getPropertySetString() const
252  {
253  return getAttribute(PROPERTY_SET_ATTRIBUTE);
254  }
255 
256  /// Assign a property set to this element.
257  void setPropertySet(ConstPropertySetPtr propertySet);
258 
259  /// Return the property set that is assigned to this element.
260  PropertySetPtr getPropertySet() const;
261 
262  /// @}
263 
264  public:
265  static const string CATEGORY;
266  static const string PROPERTY_SET_ATTRIBUTE;
267 };
268 
270 
271 #endif
PropertyPtr getProperty(const string &name) const
Return the Property, if any, with the given name.
Definition: Property.h:174
shared_ptr< PropertySetAssign > PropertySetAssignPtr
A shared pointer to a PropertySetAssign.
Definition: Property.h:39
shared_ptr< Collection > CollectionPtr
A shared pointer to a Collection.
Definition: Geom.h:53
const string & getPropertySetString() const
Return the property set string of this element.
Definition: Property.h:251
virtual ~PropertySetAssign()
Definition: Property.h:233
static const string PROPERTY_SET_ATTRIBUTE
Definition: Property.h:266
shared_ptr< Property > PropertyPtr
A shared pointer to a Property.
Definition: Property.h:24
#define MATERIALX_NAMESPACE_BEGIN
Definition: Generated.h:25
shared_ptr< const PropertySet > ConstPropertySetPtr
A shared pointer to a const PropertySet.
Definition: Property.h:36
virtual ~Property()
Definition: Property.h:52
MATERIALX_NAMESPACE_BEGIN MX_CORE_API const string EMPTY_STRING
const string & getAttribute(const string &attrib) const
Definition: Element.h:504
const string & getCollectionString() const
Return the collection string of this element.
Definition: Property.h:129
static const string CATEGORY
Definition: Property.h:265
void removeProperty(const string &name)
Remove the Property with the given name, if present.
Definition: Property.h:186
void setProperty(const string &property)
Set the property string of this element.
Definition: Property.h:73
#define MX_CORE_API
Definition: Export.h:18
vector< PropertyPtr > getProperties() const
Return a vector of all Property elements in the set.
Definition: Property.h:180
PropertyAssign(ElementPtr parent, const string &name)
Definition: Property.h:63
shared_ptr< PropertySet > PropertySetPtr
A shared pointer to a PropertySet.
Definition: Property.h:34
static const string PROPERTY_ATTRIBUTE
Definition: Property.h:144
bool hasCollectionString() const
Return true if this element has a collection string.
Definition: Property.h:123
virtual ~PropertyAssign()
Definition: Property.h:67
static const string GEOM_ATTRIBUTE
Definition: Property.h:145
bool hasProperty() const
Return true if this element has a property string.
Definition: Property.h:79
shared_ptr< PropertyAssign > PropertyAssignPtr
A shared pointer to a PropertyAssign.
Definition: Property.h:29
const string & getProperty() const
Return the property string of this element.
Definition: Property.h:85
GLuint const GLchar * name
Definition: glcorearb.h:786
virtual ~PropertySet()
Definition: Property.h:158
shared_ptr< const PropertySetAssign > ConstPropertySetAssignPtr
A shared pointer to a const PropertySetAssign.
Definition: Property.h:41
void setPropertySetString(const string &propertySet)
Set the property set string of this element.
Definition: Property.h:239
PropertyPtr setPropertyValue(const string &name, const T &value, const string &type=EMPTY_STRING)
Definition: Property.h:197
bool hasGeom() const
Return true if this element has a geometry string.
Definition: Property.h:101
void setAttribute(const string &attrib, const string &value)
Set the value string of the given attribute.
void setGeom(const string &geom)
Set the geometry string of this element.
Definition: Property.h:95
PropertySetAssign(ElementPtr parent, const string &name)
Definition: Property.h:229
void setCollectionString(const string &collection)
Set the collection string of this element.
Definition: Property.h:117
bool hasAttribute(const string &attrib) const
Return true if the given attribute is present.
Definition: Element.h:497
static const string CATEGORY
Definition: Property.h:221
PropertySet(ElementPtr parent, const string &name)
Definition: Property.h:154
PropertyPtr addProperty(const string &name)
Definition: Property.h:168
Definition: core.h:1131
shared_ptr< Element > ElementPtr
A shared pointer to an Element.
Definition: Element.h:31
const string & getGeom() const
Return the geometry string of this element.
Definition: Property.h:107
shared_ptr< const Collection > ConstCollectionPtr
A shared pointer to a const Collection.
Definition: Geom.h:55
#define MATERIALX_NAMESPACE_END
Definition: Generated.h:26
shared_ptr< const PropertyAssign > ConstPropertyAssignPtr
A shared pointer to a const PropertyAssign.
Definition: Property.h:31
static const string COLLECTION_ATTRIBUTE
Definition: Property.h:146
Property(ElementPtr parent, const string &name)
Definition: Property.h:48
shared_ptr< Value > ValuePtr
A shared pointer to a Value.
Definition: Value.h:29
type
Definition: core.h:1059
ValuePtr getPropertyValue(const string &name) const
Definition: Property.h:212
shared_ptr< const Property > ConstPropertyPtr
A shared pointer to a const Property.
Definition: Property.h:26
bool hasPropertySetString() const
Return true if this element has a property set string.
Definition: Property.h:245
static const string CATEGORY
Definition: Property.h:143
static const string CATEGORY
Definition: Property.h:55