HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PDG_AttributePrimitive.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  * COMMENTS:
7  */
8 
9 #ifndef __PDG_ATTRIBUTE_PRIMITIVE_H__
10 #define __PDG_ATTRIBUTE_PRIMITIVE_H__
11 
12 #include "PDG_API.h"
13 
14 #include "PDG_AttributeArray.h"
15 #include "PDG_AttributeData.h"
16 #include "PDG_AttributeTypes.h"
17 
18 #include <UT/UT_JSONValue.h>
19 #include <UT/UT_JSONWriter.h>
20 #include <UT/UT_Options.h>
21 
22 #include <UT/UT_StringArray.h>
23 #include <UT/UT_StringHolder.h>
24 #include <UT/UT_ValArray.h>
25 
26 /**
27  * Template for primitive value array attributes that can serialize themselves
28  * directly to JSON, without the need for custom value handling.
29  */
31  typename DataType,
33  typename JSONType = ArrayType>
35  AttribType,
36  DataType,
37  ArrayType>
38 {
39 public:
43 
44  using Base::Base;
46 
47  /// Returns the total memory usage of the underlying array data
48  int64 getMemoryUsage(bool inclusive) const override
49  {
50  int64 mem = inclusive ? sizeof(*this) : 0;
51  mem += getDataMemory(Base::myData);
52  return mem;
53  }
54 
55  /// Constructs a new attribue that's copied from this one
56  PDG_AttributeData* clone() const override
57  {
58  return new Self(Base::myData);
59  }
60 
61  /// Saves the array data to the specified json writer
62  bool saveData(UT_JSONWriter& writer) const override
63  {
64  return writer.jsonValue(Base::myData);
65  }
66 
67  /// Saves the array data to the specified options
68  void saveData(
69  const UT_StringHolder& key,
70  UT_OptionsHolder& dict) const override
71  {
72  dict.update([&] (UT_Options& edit)
73  {
74  saveOption(key, Base::myData, edit);
75  });
76  }
77 
78  /// Reads array data from the json value
79  bool loadData(const UT_JSONValue& value) override
80  {
81  JSONType json_type;
82  if (!value.import(json_type))
83  return false;
84  for (auto&& entry: json_type)
85  Base::myData.append(entry);
86  return true;
87  }
88 
89  /// Reads array data from a dict
90  bool loadData(
91  const UT_StringHolder& key,
92  const UT_OptionsHolder& dict) override
93  {
94  return dict->importOption(key, Base::myData);
95  }
96 
97 private:
98  /// Utility function to return the memory usage of a string array, which
99  /// counts the contents of the array values themselves
100  static int64 getDataMemory(const UT_StringArray& data)
101  {
102  return UTarrayDeepMemoryUsage(data, false);
103  }
104 
105  /// Utility function to return the memory usage of a value array
106  template <typename T>
107  static int64 getDataMemory(const UT_ValArray<T>& data)
108  {
109  return data.getMemoryUsage(false);
110  }
111 
112  /// Utilities for saving data to a UT_Options
113  static void saveOption(
114  const UT_StringHolder& key,
115  const UT_ValArray<fpreal>& data,
116  UT_Options& dict)
117  {
118  dict.setOptionFArray(key, data.data(), data.size());
119  }
120 
121  static void saveOption(
122  const UT_StringHolder& key,
123  const UT_ValArray<exint>& data,
124  UT_Options& dict)
125  {
126  dict.setOptionIArray(key, data);
127  }
128 
129  static void saveOption(
130  const UT_StringHolder& key,
131  const UT_StringArray& data,
132  UT_Options& dict)
133  {
134  dict.setOptionSArray(key, data);
135  }
136 };
137 
138 /**
139  * Primitive array attribute type specializations for float, integer and
140  * string attributes
141  */
144  fpreal,
149  exint,
156 
157 #endif
bool jsonValue(bool value)
int64 getMemoryUsage(bool inclusive) const override
Returns the total memory usage of the underlying array data.
int64 exint
Definition: SYS_Types.h:125
int64 getMemoryUsage(bool inclusive=false) const
Definition: UT_Array.h:657
bool loadData(const UT_JSONValue &value) override
Reads array data from the json value.
Class which writes ASCII or binary JSON streams.
Definition: UT_JSONWriter.h:37
bool loadData(const UT_StringHolder &key, const UT_OptionsHolder &dict) override
Reads array data from a dict.
exint size() const
Definition: UT_Array.h:646
UT_Options & setOptionIArray(const UT_StringHolder &name, const int32 *values, size_t size)
UT_Options & setOptionSArray(const UT_StringHolder &name, const UT_StringHolder *values, size_t size)
Set string array options.
bool saveData(UT_JSONWriter &writer) const override
Saves the array data to the specified json writer.
void update(const OP &op)
Definition: UT_Options.h:1151
#define PDG_API_TMPL
Definition: PDG_API.h:24
long long int64
Definition: SYS_Types.h:116
PDG_AttributeType
Enumeration of possible attribute types.
An array of UT_StringHolder values.
void saveData(const UT_StringHolder &key, UT_OptionsHolder &dict) const override
Saves the array data to the specified options.
An array of fpreal values.
exint append()
Definition: UT_Array.h:142
uint8_t ArrayType
Definition: NanoVDB.h:5566
A map of string to various well defined value types.
Definition: UT_Options.h:84
SYS_FORCE_INLINE bool import(bool &result) const
Extract a bool (returns false if type is invalid)
Definition: UT_JSONValue.h:246
fpreal64 fpreal
Definition: SYS_Types.h:277
T * data()
Definition: UT_Array.h:842
PDG_AttributeData * clone() const override
Constructs a new attribue that's copied from this one.
Class to store JSON objects as C++ objects.
Definition: UT_JSONValue.h:99
PDG_AttributeArray< AttribType, DataType, ArrayType > Self
An array of int values.
Definition: core.h:1131
UT_Options & setOptionFArray(const UT_StringHolder &name, const fpreal32 *values, size_t size)
Definition: format.h:895