HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_PropertyTable.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: (UT_PropertyTable.h, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef _UT_PropertyTable_
12 #define _UT_PropertyTable_
13 
14 #include "UT_API.h"
15 #include "UT_JSONValue.h"
16 #include "UT_JSONValueMap.h"
17 #include "UT_Map.h"
18 #include "UT_NonCopyable.h"
19 #include "UT_Options.h"
20 #include "UT_Set.h"
21 #include "UT_SharedPtr.h"
22 #include "UT_SymbolTable.h"
23 
24 /// This class represents a table of (mantra) properties. A property consists
25 /// of a name (the name used by mantra), a parm name (the name of the houdini
26 /// parameter that corresponds to the mantra property), the data type of the
27 /// property, and the default value of the property.
28 ///
29 /// This information is somewhat scattered right now, with the parameter
30 /// definition in the $HH/properties/soho/IFDmantra.ds file, the translation
31 /// from houdini to mantra names in $HH/soho/base/IFDmantra.py, and the
32 /// defaults as mantra understands them (which should but may not match
33 /// the defaults in the parameter definitions) in $RAY/RAY_Settings.C.
34 /// At some point all references to render properties should be through
35 /// this class.
36 ///
37 /// The property table is loaded from a JSON array where each item in the array
38 /// is a dictionary defining a property. The items to define the property are:
39 /// - "string class" (required): Specifies the rendering class of the
40 /// property (i.e. "renderer", "object" or "option")
41 /// - "string name" (required): The renderer name for the property
42 /// - "string hname" (required): The name of the Houdini parameter
43 /// - "<type> default" (required): The default value. This may be one of:
44 /// - bool
45 /// - number
46 /// - string
47 /// - array of numbers
48 /// - array of strings
49 /// - "string label" (optional): The label of the Houdini parameter
50 /// - "float min" (optional): Suggested minimum value for numeric types
51 /// - "float max" (optional): Suggested maximum value for numeric types
52 ///
54 {
55 public:
57 
58  UT_PropertyTable(const char *table_name,
59  bool verbose=false);
61  const char *filename=nullptr,
62  bool verbose=false);
63 
64  /// Return the load status of the table in the c-tor.
65  bool loadStatus() const { return myLoadStatus; }
66 
67  SYS_DEPRECATED(16.0) // Use size()
68  int getNumProps() const { return size(); }
69  SYS_DEPRECATED(16.0) // Use findRName()
70  int getPropIndex(const UT_StringRef &name)
71  { return findRName(name); }
72 
73  SYS_DEPRECATED(16.0)
74  const UT_StringHolder &getPropName(int i) const { return rname(i); }
75  SYS_DEPRECATED(16.0)
76  const UT_StringHolder &getPropLabel(int i) const { return label(i); }
77  SYS_DEPRECATED(16.0)
78  const UT_StringHolder &getPropParm(int i) const { return hname(i); }
79  SYS_DEPRECATED(16.0)
80  OptionEntryPtr getPropDefault(int i) const { return value(i);}
81  SYS_DEPRECATED(16.0)
82  fpreal64 getMin(int i) const { return minValue(i); }
83  SYS_DEPRECATED(16.0)
84  fpreal64 getMax(int i) const { return maxValue(i); }
85 
86  /// Find a property by the renderer property name. This will find names
87  /// like "global:lod", but also "lod". The first usage (with class
88  /// specifier) avoids name aliasing between different rendering classes.
89  /// The method returns @c -1 if the property isn't found.
90  int findRName(const UT_StringRef &name) const;
91  /// Find property by the Houdini parameter name
92  /// The method returns @c -1 if the parameter isn't found.
93  int findHName(const UT_StringRef &name) const;
94 
95  /// Number of properties stored in the table
96  exint size() const { return myProperties.size(); }
97  /// Return the class of the rendering property (i.e. "object")
98  const UT_StringHolder &rclass(int i) const;
99  /// Return the name of the rendering property (i.e. "coving")
100  const UT_StringHolder &rname(int i) const;
101  /// Return the name of the houdini parameter (i.e. "vm_coving")
102  const UT_StringHolder &hname(int i) const;
103  /// Return the label of the houdini parameter (i.e. "Coving")
104  const UT_StringHolder &label(int i) const;
105  /// Return the default value of the houdini parameter (i.e. 1)
106  OptionEntryPtr value(int i) const;
107  /// @{
108  /// For numeric values, return the expected range of the values. These is
109  /// typically a suggested range.
110  fpreal64 minValue(int i) const;
111  fpreal64 maxValue(int i) const;
112  /// @}
113 
114  /// Return a raw JSONValueMap representing the property
115  const UT_JSONValueMap &jsonMap(int i) const
116  { return myProperties[i].jsonMap(); }
117 
118  /// @{
119  /// Import a value from a property
120  template <typename T> SYS_FORCE_INLINE bool
121  import(int idx, const UT_StringRef &name, T &val) const
122  { return myProperties[idx].import(name, val); }
123 
124  template <typename T> SYS_FORCE_INLINE bool
125  import(int idx, const UT_StringRef &name, T *val, int size) const
126  { return myProperties[idx].import(name, val, size); }
127  /// @}
128 
129  /// Debug the settings
130  bool save(UT_JSONWriter &w, const UT_IntArray *select=nullptr) const;
131  void dump(const UT_IntArray *select=nullptr) const;
132 
133  /// Relatively lightweight representation of a subset of a UT_PropertyList.
135  {
136  public:
138  : myTable(table)
139  {
140  }
141 
142  /// @{
143  /// Interface to properties
144  exint size() const { return myProperties.size(); }
145  const UT_StringHolder &rclass(int i) const
146  { return myTable.rclass(map(i)); }
147  const UT_StringHolder &rname(int i) const
148  { return myTable.rname(map(i)); }
149  const UT_StringHolder &hname(int i) const
150  { return myTable.hname(map(i)); }
151  const UT_StringHolder &label(int i) const
152  { return myTable.label(map(i)); }
153  OptionEntryPtr value(int i) const
154  { return myTable.value(map(i)); }
155  fpreal64 minValue(int i) const
156  { return myTable.minValue(map(i)); }
157  fpreal64 maxValue(int i) const
158  { return myTable.maxValue(map(i)); }
159 
160  bool save(UT_JSONWriter &w) const
161  { return myTable.save(w, &myProperties); }
162  void dump() const
163  { return myTable.dump(&myProperties); }
164  /// @}
165 
166  /// Get the index in the parent table
167  exint parentIndex(int i) const { return map(i); }
168 
169  /// Add a property using the index from the base table
170  void appendProperty(int id) { myProperties.append(id); }
171 
172  private:
173  int map(int i) const
174  {
175  return i >= 0 && i < myProperties.size() ? myProperties(i) : -1;
176  }
177  const UT_PropertyTable &myTable;
178  UT_IntArray myProperties;
179  };
180 
182  { return getSubset(&name, 1); }
183  const PropertySubsetList &getSubset(const UT_StringRef names[],
184  exint arraylen);
185 
186  /// Function to extract a list of properties as an array of UT_JSONValueMaps
187  /// The SELECT method takes a const reference to the UT_JSONValueMap and
188  /// should return true if the map should be added. For example, to find
189  /// all the karma global rendering properties, you can do something like:
190  /// @code
191  /// UT_Array<const UT_JSONValueMap *> globals;
192  /// ptable.select(globals, [](const UT_JSONValueMap &prop)
193  /// {
194  /// UT_StringHolder _renderer;
195  /// UT_StringHolder _class;
196  /// return prop.import("renderer", _renderer)
197  /// && prop.import("class", _class)
198  /// && renderer == "karma"
199  /// && _class == "global";
200  /// });
201  /// @endcode
202  template <typename SELECT> SYS_FORCE_INLINE void
203  select(UT_Array<const UT_JSONValueMap *> &matched, SELECT &predicate)
204  {
205  for (auto &&pdef : myProperties)
206  {
207  if (predicate(pdef.jsonMap()))
208  matched.append(&pdef.jsonMap());
209  }
210  }
211  /// Run an operation on a selection of properties from the table.
212  /// The SELECT method takes a const reference to the UT_JSONValueMap and
213  /// should return true if the functor should be called.
214  template <typename FUNC, typename SELECT> SYS_FORCE_INLINE void
215  filter(FUNC &func, SELECT &predicate)
216  {
217  for (auto &&pdef : myProperties)
218  {
219  if (predicate(pdef.jsonMap()))
220  FUNC(pdef.jsonMap());
221  }
222  }
223 
224 private:
225  struct IncludeGuards
226  {
227  IncludeGuards(bool v=false)
228  : myVerbose(v)
229  {
230  }
231 
232  // Verbose if the flag is set and we haven't included any files
233  bool verbose() const { return myVerbose && mySet.size() == 0; }
234  void insert(const UT_StringRef &r) { mySet.insert(r); }
235  void erase(const UT_StringRef &r) { mySet.erase(r); }
236  size_t count(const UT_StringRef &r) const { return mySet.count(r); }
237  size_t size() const { return mySet.size(); }
238  UT_Set<UT_StringRef> mySet;
239  bool myVerbose;
240  };
241  bool doInclude(const UT_StringRef &filename,
242  IncludeGuards &guards);
243  bool load(UT_IStream &is, IncludeGuards &guards);
244  bool load(UT_JSONParser &p,
245  const char *filename,
246  IncludeGuards &guards);
247  const PropertySubsetList &buildSubset(const UT_StringRef names[],
248  exint arraylen,
249  const UT_StringHolder &key);
250 
251  using PropertySubsetListMap = UT_Map<UT_StringHolder, PropertySubsetList>;
252 
253  class PropDef
254  {
255  public:
256  PropDef(UT_JSONValueMap *map);
257  ~PropDef();
258 
259  UT_NON_COPYABLE(PropDef)
260 
261  /// Saves out a dictionary for the property
262  bool save(UT_JSONWriter &w) const;
263 
264  const UT_StringHolder &rclass() const { return myRClass; }
265  const UT_StringHolder &rname() const { return myRName; }
266  const UT_StringHolder &hname() const { return myHName; }
267  const UT_StringHolder &label() const { return myLabel; }
268  const OptionEntryPtr &value() const { return myValue; }
269  fpreal64 minValue() const { return myMin; }
270  fpreal64 maxValue() const { return myMax; }
271  const UT_JSONValueMap &jsonMap() const { return *myJSONMap; }
272  const UT_JSONValue *getItem(const UT_StringRef &name)
273  { return (*myJSONMap)(name); }
274 
275  template <typename T> SYS_FORCE_INLINE bool
276  import(const UT_StringRef &name, T &v)
277  {
278  const UT_JSONValue *item = (*myJSONMap)(name);
279  return item && item->import(v);
280  }
281  template <typename T> SYS_FORCE_INLINE bool
282  import(const UT_StringRef &name, T *v, int size)
283  {
284  const UT_JSONValue *item = (*myJSONMap)(name);
285  return item && item->import(v, size);
286  }
287  private:
288  UT_JSONValueMap *myJSONMap;
289  UT_StringHolder myRClass;
290  UT_StringHolder myRName;
291  UT_StringHolder myHName;
292  UT_StringHolder myLabel;
293  OptionEntryPtr myValue;
294  fpreal64 myMin;
295  fpreal64 myMax;
296  };
297  void addProperty(const UT_JSONValueMap &map);
298 
299  UT_Array<PropDef> myProperties;
300  UT_SymbolMap<int> myRNameMap;
301  UT_SymbolMap<int> myHNameMap;
302  PropertySubsetListMap mySubsets;
303  bool myLoadStatus;
304 };
305 
306 #endif
Relatively lightweight representation of a subset of a UT_PropertyList.
typedef int(APIENTRYP RE_PFNGLXSWAPINTERVALSGIPROC)(int)
GT_API const UT_StringHolder filename
UT_JSONValueMap stores a map/dictionary of UT_JSONValue objects.
GLuint GLsizei const GLchar * label
Definition: glcorearb.h:2545
#define SYS_DEPRECATED(__V__)
const UT_StringHolder & rclass(int i) const
UT_SharedPtr< UT_OptionEntry > OptionEntryPtr
const GLdouble * v
Definition: glcorearb.h:837
GLsizei const GLfloat * value
Definition: glcorearb.h:824
bool loadStatus() const
Return the load status of the table in the c-tor.
PropertySubsetList(const UT_PropertyTable &table)
int64 exint
Definition: SYS_Types.h:125
bool save(UT_JSONWriter &w) const
const UT_StringHolder & hname(int i) const
JSON reader class which handles parsing of JSON or bJSON files.
Definition: UT_JSONParser.h:87
#define UT_API
Definition: UT_API.h:14
Class which writes ASCII or binary JSON streams.
Definition: UT_JSONWriter.h:37
OIIO_FORCEINLINE vbool4 insert(const vbool4 &a, bool val)
Helper: substitute val for a[i].
Definition: simd.h:3436
double fpreal64
Definition: SYS_Types.h:201
exint parentIndex(int i) const
Get the index in the parent table.
vint4 select(const vbool4 &mask, const vint4 &a, const vint4 &b)
Definition: simd.h:4816
std::shared_ptr< T > UT_SharedPtr
Wrapper around std::shared_ptr.
Definition: UT_SharedPtr.h:36
__hostdev__ bool getMax() const
Definition: NanoVDB.h:5747
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
const UT_JSONValueMap & jsonMap(int i) const
Return a raw JSONValueMap representing the property.
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
exint size() const
Number of properties stored in the table.
const UT_StringHolder & label(int i) const
GLuint const GLchar * name
Definition: glcorearb.h:786
OptionEntryPtr value(int i) const
GLenum GLenum GLsizei void * table
Definition: glad.h:5129
exint append()
Definition: UT_Array.h:142
void appendProperty(int id)
Add a property using the index from the base table.
const UT_StringHolder & rname(int i) const
GLsizeiptr size
Definition: glcorearb.h:664
GLenum func
Definition: glcorearb.h:783
SYS_FORCE_INLINE bool import(bool &result) const
Extract a bool (returns false if type is invalid)
Definition: UT_JSONValue.h:246
__hostdev__ bool getMin() const
Definition: NanoVDB.h:5746
GLuint GLfloat * val
Definition: glcorearb.h:1608
Class to store JSON objects as C++ objects.
Definition: UT_JSONValue.h:99
SYS_FORCE_INLINE void filter(FUNC &func, SELECT &predicate)
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:857
const PropertySubsetList & getSubset(const UT_StringRef &name)
GLboolean r
Definition: glcorearb.h:1222
SYS_FORCE_INLINE void select(UT_Array< const UT_JSONValueMap * > &matched, SELECT &predicate)