HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GA_GroupTable.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_GroupTable.h (GA Library, C++)
7  *
8  * COMMENTS: A table for managing GA_Group objects.
9  *
10  */
11 
12 #pragma once
13 
14 #ifndef __GA_GroupTable_h__
15 #define __GA_GroupTable_h__
16 
17 #include "GA_API.h"
18 
19 #include "GA_Group.h"
20 #include "GA_Types.h"
21 
22 #include <UT/UT_ArrayStringMap.h>
23 #include <UT/UT_OrderedIterator.h>
24 #include <UT/UT_String.h>
25 #include <UT/UT_StringHolder.h>
26 #include <UT/UT_UniquePtr.h>
27 
28 #include <SYS/SYS_Types.h>
29 
30 
31 class GA_Detail;
32 class GA_LoadMap;
33 class GA_SaveMap;
34 class GA_Stat;
35 
36 class UT_JSONParser;
37 class UT_JSONWriter;
38 class UT_MemoryCounter;
39 class UT_StringArray;
40 class UT_WorkBuffer;
41 template <typename T> class UT_Array;
42 
44 {
45 public:
47 
48  GA_GroupTable(GA_Detail &detail);
49  virtual ~GA_GroupTable();
50 
51  /// Report memory usage (includes all shared memory)
52  virtual int64 getMemoryUsage(bool inclusive) const;
53 
54  /// Count memory usage using a UT_MemoryCounter in order to count
55  /// shared memory correctly.
56  /// If inclusive is true, the size of this object is counted,
57  /// else only memory owned by this object is counted.
58  /// If this is pointed to by the calling object, inclusive should be true.
59  /// If this is contained in the calling object, inclusive should be false.
60  /// (Its memory was already counted in the size of the calling object.)
61  virtual void countMemory(UT_MemoryCounter &counter, bool inclusive) const;
62 
63  bool isEmpty() const { return myEntryMap.empty(); }
64  // Number of groups, not including internal groups.
65  exint getPersistentCount() const;
66  // Raw number of groups, including internal groups.
67  exint entries() const { return myEntryMap.size(); }
68 
69  GA_Group *find(const UT_StringRef &name) const;
70 
71  GA_Group *newGroup(const UT_StringHolder &name);
72  GA_Group *newInternalGroup();
73 
74  /// TODO: Deprecate internal groups with user-specified names.
75  GA_Group *newGroup(const UT_StringHolder &name, bool internal);
76 
77  /// A detached group is not owned by anyone but the caller, so
78  /// will not track new points and must be deleted by the caller.
79  /// @{
80  GA_GroupUPtr createDetachedGroup() const;
81 
82  SYS_DEPRECATED_REPLACE(20.0, "createDetachedGroup")
83  GA_Group *newDetachedGroup() const
84  { return createDetachedGroup().release(); }
85  /// @}
86 
87  bool destroy(GA_Group *group);
88  bool destroy(const UT_StringRef &name);
89 
90  class GA_API Filter
91  {
92  public:
93  virtual ~Filter() {}
94 
95  virtual bool match(const GA_Group *group) const = 0;
96  };
97 
98  GA_Size destroyGroups(const Filter &filter);
99 
100  void clear();
101 
102  bool renameGroup(const UT_StringRef &fromname, const UT_StringHolder &toname);
103 
104  /// Rename a group to an unused name. Typically used to make from_name
105  /// available for another group.
106  ///
107  /// Returns new name on success, UT_StringHolder() on failure.
108  UT_StringHolder renameGroupUnspecified(const UT_StringRef &from_name);
109 
110  /// Save array of groups to a JSON stream
111  /// @section JSON-GA_GroupTable JSON Schema: GA_GroupTable
112  /// Stores an array of groups.
113  /// @code
114  /// {
115  /// "name" : "GA_GroupTable",
116  /// "description" : "A list of groups. All items should be same type",
117  /// "type" : "array",
118  /// "items" : { "$ref" : "GA_Group" },
119  /// }
120  /// @endcode
121  /// @see @ref JSON_FileFormat, GA_Group
122  bool jsonSave(UT_JSONWriter &w, const GA_SaveMap &map,
123  const char *type) const;
124 
125  /// Load the groups from the JSON stream
126  bool jsonLoad(UT_JSONParser &p, const GA_LoadMap &map,
127  const char *type_default, GA_AttributeOwner owner);
128 
129  /// Returns @c true if there are any groups to be saved out.
130  bool jsonShouldSave(const GA_SaveMap &map) const;
131 
132  /// Stat the group from the JSON stream
133  static bool jsonStat(UT_JSONParser &p, GA_Stat &sbuf, GA_GroupType owner);
134 
135  /// @{
136  /// Get information about groups
137  void stat(UT_StringArray &info, uint level);
138  void stat(GA_Stat &stat, uint level) const;
139  /// @}
140 
141  /// @note Derived classes are responsible for ensuring that this iterator
142  /// template is only instantiated for the appropriate GROUP_TYPE, if any.
143  template <typename GROUP_TYPE>
144  class iterator
145  {
146  public:
148  iterator() : myIt(), myEnd() {}
151  : myIt(it)
152  , myEnd(end)
153  {}
154 
157  { return myIt->second->getName(); }
159  GROUP_TYPE *group()
160  { return static_cast<GROUP_TYPE *>(myIt->second); }
162  GROUP_TYPE *operator*() { return group(); }
163 
165  bool operator==(const iterator &cmp) const
166  { return (myIt == cmp.myIt); }
168  bool operator!=(const iterator &cmp) const
169  { return !(*this == cmp); }
171  bool atEnd() const
172  { return myIt == myEnd; }
173 
176  {
177  ++myIt;
178  return *this;
179  }
180  private:
183  };
184 
185  /// Traverse the group table in creation order.
186  ///
187  /// @note Derived classes will add their own specialized versions
188  /// of begin() and end() and call them beginTraverse() and endTraverse().
189  /// These were named begin/endTraverse() to avoid any method hiding as
190  /// the return types are different.
192  { return begin<GA_Group>(); }
194  { return end<GA_Group>(); }
195 
196  /// @{
197  /// Traverse the group table in alpabetic order.
200  static int compareAlpha(GA_Group *const*a, GA_Group *const*b)
201  { return compareGroupName(**a, **b); }
203  {
204  GA_GroupTable::iterator<GA_Group> it = beginTraverse();
205  return ordered_iterator(it, compareAlpha);
206  }
208  { return ordered_iterator(); }
209  /// @}
210 
211 protected:
212  /// Convenience function for ordered iteration (in subclasses)
213  /// @{
214  static int compareGroupName(const GA_Group &a, const GA_Group &b)
215  {
217  }
218  /// @}
219 
220  template <typename T> iterator<T> begin() const
221  { return iterator<T>(myEntryMap.begin(),myEntryMap.end()); }
222  template <typename T> iterator<T> end() const
223  { return iterator<T>(myEntryMap.end(),myEntryMap.end()); }
224 
225  /// @note Derived classes are responsible for ensuring that this template
226  /// method is only instantiated for the appropriate GROUP_TYPE, if any.
227  template <typename GROUP_TYPE>
229  {
231 
232  for (iter = begin<GROUP_TYPE>(); !iter.atEnd(); ++iter)
233  {
234  GROUP_TYPE *group = iter.group();
235  if (group)
236  list.append(group);
237  }
238  }
239 
240  GA_Detail &getDetail() { return myDetail; }
241  const GA_Detail &getDetail() const { return myDetail; }
242 
243  /// Method called to create a group during loading. The sub-class should
244  /// verify that the owner matches the type information.
245  virtual GA_Group *jsonCreateGroup(UT_JSONParser &p,
246  const char *type,
247  const char *name,
248  bool internal,
249  GA_AttributeOwner owner) = 0;
250 
251  virtual GA_Group *virtualCreateGroup(
252  GA_Detail &detail,
253  const UT_StringHolder &name,
254  bool internal) const = 0;
255  virtual GA_Group *virtualCreateDetachedGroup(
256  const GA_Detail &detail) const = 0;
257 
258  // This variation of newGroup does not check to see if the
259  // name is already used.
260  GA_Group *newGroupWithNoConflict(const UT_StringHolder &name, bool internal);
261 
263  {
264  if (myEntryMap.size() + n > myEntryMap.bucket_count())
265  myEntryMap.reserve(myEntryMap.size() + n);
266  }
267 
268  friend class GA_AttributeDict;
269 
270  /// Used only by GA_AttributeDict
271  const MapType &getMap() const
272  {
273  return myEntryMap;
274  }
275 private:
276  UT_StringRef makeUniqueName(UT_WorkBuffer &name);
277 
278  /// Bumps the corresponding attribute set, if it exists, version
279  /// parms. Requires the group as that is the only way to locate
280  /// the group type.
281  void bumpVersionParms(const GA_Group *group);
282 
283  GA_Detail &myDetail;
284  MapType myEntryMap;
285 };
286 
287 #endif
SYS_FORCE_INLINE iterator()
SYS_FORCE_INLINE const UT_StringHolder & name()
GA_GroupTable::iterator< GA_Group > beginTraverse() const
SYS_FORCE_INLINE iterator(const MapType::const_iterator &it, const MapType::const_iterator &end)
Used to pass options and map offset values during saving.
Definition: GA_SaveMap.h:48
SYS_FORCE_INLINE GROUP_TYPE * group()
void getListT(UT_Array< GROUP_TYPE * > &list) const
UT_ArrayStringMap< GA_Group * > MapType
Definition: GA_GroupTable.h:46
int64 exint
Definition: SYS_Types.h:125
GLint level
Definition: glcorearb.h:108
exint entries() const
Definition: GA_GroupTable.h:67
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
ordered_iterator obegin() const
JSON reader class which handles parsing of JSON or bJSON files.
Definition: UT_JSONParser.h:87
#define GA_API
Definition: GA_API.h:14
const GA_Detail & getDetail() const
Class which writes ASCII or binary JSON streams.
Definition: UT_JSONWriter.h:37
static int compareAlpha(GA_Group *const *a, GA_Group *const *b)
UT_ArrayStringMap< GA_AttributeProxy * > MapType
exint GA_Size
Defines the bit width for index and offset types in GA.
Definition: GA_Types.h:236
IMATH_HOSTDEVICE constexpr int cmp(T a, T b) IMATH_NOEXCEPT
Definition: ImathFun.h:84
A string map of attributes to ease backward compatibility In the GB/GEO/GU library code would often p...
#define SYS_DEPRECATED_REPLACE(__V__, __R__)
void reserveNewSymbolTableSpace(exint n)
GA_GroupTable::iterator< GA_Group > endTraverse() const
Parent::const_iterator const_iterator
SYS_FORCE_INLINE GROUP_TYPE * operator*()
GLdouble n
Definition: glcorearb.h:2008
bool isEmpty() const
Definition: GA_GroupTable.h:63
ordered_iterator oend() const
GLuint GLuint end
Definition: glcorearb.h:475
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
long long int64
Definition: SYS_Types.h:116
Options during loading.
Definition: GA_LoadMap.h:42
SYS_FORCE_INLINE const char * c_str() const
GLuint const GLchar * name
Definition: glcorearb.h:786
const UT_StringHolder & getName() const
Definition: GA_Group.h:42
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
exint append()
Definition: UT_Array.h:142
iterator< T > end() const
const MapType & getMap() const
Used only by GA_AttributeDict.
SYS_FORCE_INLINE iterator & operator++()
SYS_FORCE_INLINE bool operator==(const iterator &cmp) const
static int compareNumberedString(const char *s1, const char *s2, bool case_sensitive=true, bool allow_negatives=false)
GA_AttributeOwner
Definition: GA_Types.h:35
Class to return information about a GA_Detail.
Definition: GA_Stat.h:50
static int compareGroupName(const GA_Group &a, const GA_Group &b)
UT_UniquePtr< GA_Group > GA_GroupUPtr
Definition: GA_Group.h:199
GA_GroupType
An ordinal enum for the different types of groups in GA.
Definition: GA_Types.h:161
Container class for all geometry.
Definition: GA_Detail.h:96
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:857
SYS_FORCE_INLINE bool operator!=(const iterator &cmp) const
iterator< T > begin() const
type
Definition: core.h:1059
UT_OrderedIterator< GA_Group *, GA_GroupTable::iterator< GA_Group > > ordered_iterator
unsigned int uint
Definition: SYS_Types.h:45
SYS_FORCE_INLINE bool atEnd() const
GA_Detail & getDetail()
FMT_CONSTEXPR auto find(Ptr first, Ptr last, T value, Ptr &out) -> bool
Definition: core.h:2089
GLint GLint GLint GLint GLint GLint GLint GLbitfield GLenum filter
Definition: glcorearb.h:1297