HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GA_AttributeType.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: GA_AttributeType.h ( GA Library, C++)
7  *
8  * COMMENTS: Class-factory for GA_Attribute objects
9  *
10  * Each attribute type should have a unique name.
11  *
12  * The constructor will register the attribute type and assign it a unique
13  * integer identifier. The unique integer identifier may change from run
14  * to run depending on construction order, so it should only be used in a
15  * single session (and you should *not* assume the value will be the same
16  * on the next session).
17  *
18  */
19 
20 #pragma once
21 
22 #ifndef __GA_AttributeType__
23 #define __GA_AttributeType__
24 
25 #include "GA_API.h"
26 #include "GA_Types.h"
27 
28 #include <UT/UT_Array.h>
29 #include <UT/UT_StringHolder.h>
30 
31 class GA_IndexMap;
32 class GA_Attribute;
33 class GA_AIFBase;
34 class GA_AIFEditDelta;
36 class GA_ReuseStrategy;
37 class UT_Options;
38 class UT_JSONParser;
39 
40 template <typename AIF_TYPE> class GA_AIFDefinition;
41 
43 {
44 public:
46  virtual ~GA_AttributeType();
47 
48  static const GA_AttributeType *findType(const UT_StringRef &name);
49 
50  /// Method to create and initialize a new attribute. The pure virtual
51  /// alloc() method is used to allocate the attribute.
52  GA_Attribute *create(const GA_IndexMap &index_map,
53  GA_AttributeScope scope,
54  const UT_StringHolder &name,
55  const UT_Options *creation_options,
56  const GA_AttributeOptions *attribute_options) const;
57 
58  /// There may already be an attribute with the same name when we attempt
59  /// to add a new attribute. Whenever possible we want to try to recycle
60  /// the existing attribute, updating it for our current needs, instead of
61  /// allocating a new attribute and copying any compatible values.
62  ///
63  /// This method returns false whenever it was not possible to update the
64  /// supplied attribute to meet the new requirements. The attribute may
65  /// be partially modified even on failure.
66  /// @note Always fails if findType(requested_type) != this, regardless of
67  /// strategy.
68  bool recycleAttribute(GA_Attribute &attrib,
70  const GA_AttributeType &requested_type,
71  const UT_Options *creation_options,
72  const GA_AttributeOptions *attribute_options) const;
73 
74  /// Return information about the type
76  const UT_StringHolder &getTypeName() const { return myTypeName; }
78  int getTypeId() const { return myTypeId; }
79 
80  bool isFactoryType() const { return myFactoryFlag; }
81  bool isStandardType() const { return myStandardFlag; }
82  bool isTopologyType() const
83  { return myTypeId == ourTopologyTypeId; }
84 
85  /// This method is used to parse the GA_AIFEditDelta type arguments saved
86  /// to a JSON stream in order to allocate the appropriate subclass. The
87  /// type arguments should be stored as the next object.
88  /// TODO: Maybe we should parse a UT_Options object externally and pass
89  /// that in?
90  virtual bool jsonLoadDeltaDefinition(UT_JSONParser &p,
91  GA_AIFEditDelta *&delta) const;
92 
93  /// This method is used to register a new AIF type. The returned object
94  /// can be used to bind custom implementations to specific types and get
95  /// the implementation bound to a given type. A default implementation
96  /// can be specified during the registration that will be used for those
97  /// types without an explicitly bound custom implementation.
98  template <typename AIF_TYPE> static
99  GA_AIFDefinition<AIF_TYPE> registerAIF(AIF_TYPE *default_impl)
100  {
102  allocAIF(default_impl));
103  }
104 
105 protected:
106  /// Allocate the attribute
107  virtual GA_Attribute *alloc(const GA_IndexMap &index_map,
108  GA_AttributeScope scope,
109  const UT_StringHolder &name,
110  const UT_Options *creation_options)
111  const = 0;
112 
113  /// This method is the type-specific implementation of recycleAttribute().
114  ///
115  /// @pre &attrib.getType() == this
116  virtual bool recycle(GA_Attribute &attrib,
117  const GA_ReuseStrategy &strategy,
118  const UT_Options *creation_options)
119  const = 0;
120 
121 private:
122  // Register built-in attribute types during static initialization of GA.
123  // We don't want it to happen when code tries to create details in parallel.
124  static void createFactoryTypes();
125  class Creator;
126  static Creator theCreator;
127 
128  static int allocAIF(GA_AIFBase *default_aif);
129  static void bindAIF(const UT_StringHolder &type, int id, GA_AIFBase *aif);
130  const GA_AIFBase *getAIF(int id) const;
131 
132  static int ourTopologyTypeId;
133 
134 
135  UT_StringHolder myTypeName;
136  int myTypeId;
137  bool myFactoryFlag;
138  bool myStandardFlag;
139 
140  UT_Array<GA_AIFBase *> myAIFs;
141 
142  // Ideally we'd only declare specific methods of GA_AIFDefinition friends,
143  // but the GA_AIFDefinition template must be declared after us.
144  template <typename AIF_TYPE> friend class GA_AIFDefinition;
145 #if 0
146  template <typename AIF_TYPE> friend
147  void GA_AIFDefinition<AIF_TYPE>::bind(const UT_StringHolder &, AIF_TYPE *) const;
148  template <typename AIF_TYPE> friend
149  const AIF_TYPE *GA_AIFDefinition<AIF_TYPE>::get(
150  const GA_AttributeType &) const;
151 #endif
152 };
153 
154 /// A common base class for all client-registered AIF classes.
156 {
157 public:
158  virtual ~GA_AIFBase() {}
159 };
160 
161 /// @brief Class to allow custom AIF interfaces to be built
162 ///
163 /// GA_AIFDefinition is used by client-code to manage client-registered
164 /// AIF_TYPE implementations. AIF_TYPE must be derived from GA_AIFBase.
165 template <typename AIF_TYPE>
167 {
168 public:
169  /// Bind an implementation to the specified attribute type. This method
170  /// can be called even before the specified attribute type is registered.
171  void bind(const UT_StringHolder &attrib_type, AIF_TYPE *aif) const
172  {
173  GA_AttributeType::bindAIF(attrib_type, myId, aif);
174  }
175  /// Query the implementation for the specified attribute type. Default
176  /// implementation specified during registration, if any, is returned if
177  /// no implementation has been explicitly bound to this type.
178  const AIF_TYPE *get(const GA_AttributeType &type) const
179  {
180  return static_cast<const AIF_TYPE *>(
181  type.getAIF(myId));
182  }
183 private:
184  explicit GA_AIFDefinition(int id) : myId(id) {}
185 
186  int myId;
187 
188 #if defined(WIN32) || defined(__clang__)
189  template <typename T>
191 #else
192  friend GA_AIFDefinition<AIF_TYPE> GA_AttributeType::registerAIF<AIF_TYPE>(AIF_TYPE *);
193 #endif
194 };
195 
196 
197 #endif
A class to manage an ordered array which has fixed offset handles.
Definition: GA_IndexMap.h:63
Definition of a geometry attribute.
Definition: GA_Attribute.h:189
Specify when and how to reuse an existing attribute.
const AIF_TYPE * get(const GA_AttributeType &type) const
bool isTopologyType() const
JSON reader class which handles parsing of JSON or bJSON files.
Definition: UT_JSONParser.h:72
#define GA_API
Definition: GA_API.h:12
bool isFactoryType() const
SYS_FORCE_INLINE int getTypeId() const
void bind(const UT_StringHolder &attrib_type, AIF_TYPE *aif) const
GA_AttributeScope
Definition: GA_Types.h:121
A common base class for all client-registered AIF classes.
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
int strategy
Definition: png.h:2009
virtual ~GA_AIFBase()
GLuint const GLchar * name
Definition: glcorearb.h:785
A map of string to various well defined value types.
Definition: UT_Options.h:42
bool isStandardType() const
SYS_FORCE_INLINE const UT_StringHolder & getTypeName() const
Return information about the type.
Class to allow custom AIF interfaces to be built.
Base class to store recorder data.
Definition: GA_AIFEdit.h:217
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:107
static GA_AIFDefinition< AIF_TYPE > registerAIF(AIF_TYPE *default_impl)