HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PDG_AttributePyObject.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_PY_OBJECT_H__
10 #define __PDG_ATTRIBUTE_PY_OBJECT_H__
11 
12 #include "PDG_API.h"
13 
14 #include "PDG_AttributeData.h"
15 
16 #include <UT/UT_StringMap.h>
17 
18 class PDG_AttributeQuery;
19 class PY_PyObject;
20 class UT_JSONValue;
21 class UT_JSONWriter;
22 class UT_OptionsHolder;
23 class UT_StringHolder;
24 
25 /**
26  * Attribute that contains a single PY_PyObject*, mostly opaque in C++ code but
27  * accessible from the Python API.
28  */
30 {
31 public:
33 
34  using Data = PY_PyObject*;
37 
38 public:
40  explicit PDG_AttributePyObject(PY_PyObject* object);
41  ~PDG_AttributePyObject() override;
42 
43  int64 getMemoryUsage(bool inclusive) const override;
44  bool hash(
46  exint index,
47  UT_WorkBuffer& errors) const override;
48  PDG_AttributeData* clone() const override;
49 
50  bool saveData(UT_JSONWriter& writer) const override;
51  void saveData(
52  const UT_StringHolder& key,
53  UT_OptionsHolder& dict) const override;
54 
55  bool loadData(const UT_JSONValue& value) override;
56  bool loadData(
57  const UT_StringHolder& key,
58  const UT_OptionsHolder& dict) override;
59 
60  bool compare(
61  const PDG_AttributeData* other
62  ) const override;
64  PDG_AttributeMergeOp merge_op,
65  int stride) override;
66 
68  fpreal& value,
70  int component) const;
72  fpreal& value,
73  const UT_StringHolder& str,
74  int component) const;
75 
79  int component) const;
82  const UT_StringHolder& str,
83  int component) const;
84 
85  PY_PyObject* query(const UT_StringHolder& str) const;
86 
87  bool setObjectFromString(const UT_StringHolder& repr);
88  bool setObjectFromLiteral(
89  const UT_StringHolder& literal,
90  UT_WorkBuffer& errors);
91 
92  PY_PyObject* object() const;
93  void setObject(PY_PyObject* object);
94 
95  inline bool hasData() const
96  {
97  return (myObject != nullptr);
98  }
99 
100  inline int size() const
101  {
102  return 1;
103  }
104 
105  inline bool valid(int component) const
106  {
107  return (component == 0);
108  }
109 
110  inline bool value(PY_PyObject*& object, int component) const
111  {
112  if (component != 0)
113  return false;
114 
115  object = myObject;
116  return true;
117  }
118 
119  inline void setValue(PY_PyObject* object, int index)
120  {
121  setObject(object);
122  }
123 
124  bool repr(UT_WorkBuffer& str) const;
125  bool repr(UT_StringHolder& str) const;
126  bool str(UT_StringHolder& str) const;
127 
128  inline void swap(PDG_AttributePyObject* other)
129  {
130  std::swap(myObject, other->myObject);
131  }
132 
133  void copy(
134  const PDG_AttributePyObject* other,
135  PDG_AttributeCopyOp copy_op);
136 
137 private:
138  void serializationRepr(
139  UT_StringHolder& object_repr) const;
140  PY_PyObject* queryInternal(
141  const PDG_AttributeQuery& query,
142  int component) const;
143  static PY_PyObject* getItem(PY_PyObject* source,
144  const UT_StringHolder& key);
145  static PY_PyObject* getItem(PY_PyObject* source, int key);
146 
147  static PDG_AttributeCast
148  castValue(fpreal& value, PY_PyObject* object);
149  static PDG_AttributeCast
150  castValue(UT_WorkBuffer& value,
151  PY_PyObject* object);
152 private:
153  PY_PyObject* myObject;
154 };
155 
156 #endif
GLenum query
Definition: glad.h:2772
void swap(UT::ArraySet< Key, MULTI, MAX_LOAD_FACTOR_256, Clearer, Hash, KeyEqual > &a, UT::ArraySet< Key, MULTI, MAX_LOAD_FACTOR_256, Clearer, Hash, KeyEqual > &b)
Definition: UT_ArraySet.h:1699
OIIO_UTIL_API bool copy(string_view from, string_view to, std::string &err)
GLsizei const GLfloat * value
Definition: glcorearb.h:824
virtual bool compare(const PDG_AttributeData *) const =0
Compares this data object with another data object.
#define PDG_API
Definition: PDG_API.h:23
int64 exint
Definition: SYS_Types.h:125
std::size_t SYS_HashType
Define the type for hash values.
Definition: SYS_Hash.h:19
Class which writes ASCII or binary JSON streams.
Definition: UT_JSONWriter.h:37
bool valid(int component) const
void setValue(PY_PyObject *object, int index)
virtual bool loadData(const UT_JSONValue &)=0
Loads data from a JSON value.
virtual PDG_AttributeMergeError merge(PDG_AttributeMergeOp merge_op, int stride)=0
Applies a merge op to the data in this attribute.
PDG_AttributeCast
Enumeration of attribute cast results.
PY_PyObject
GLsizei GLsizei GLchar * source
Definition: glcorearb.h:803
GLint GLenum GLboolean GLsizei stride
Definition: glcorearb.h:872
virtual bool saveData(UT_JSONWriter &) const =0
Serializes the data to a JSON writer.
long long int64
Definition: SYS_Types.h:116
PDG_AttributeType
Enumeration of possible attribute types.
PDG_AttributeMergeOp
Enumeration of different ways that attributes can be combined.
virtual bool hash(SYS_HashType &hash_value, exint index, UT_WorkBuffer &errors) const =0
Return a hash value derived from the attribute data.
virtual int64 getMemoryUsage(bool inclusive) const =0
Returns the memory usage of the attribute and its data.
fpreal64 fpreal
Definition: SYS_Types.h:278
GLuint index
Definition: glcorearb.h:786
Class to store JSON objects as C++ objects.
Definition: UT_JSONValue.h:99
A single, opaque PyObject.
PDG_AttributeMergeError
Enumerations of errors that can occur when merging attributes.
void swap(PDG_AttributePyObject *other)
bool value(PY_PyObject *&object, int component) const
size_t hash_value(const CH_ChannelRef &ref)
virtual PDG_AttributeData * clone() const =0
Deep copies this attribute.
PDG_AttributeCopyOp
Enumeration of attribute copy operations.