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