HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UN_CustomData.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: UN_NodeData.h ( UN Library, C++)
7  *
8  * COMMENTS:
9  *
10  */
11 
12 #ifndef __UN_CustomData_h__
13 #define __UN_CustomData_h__
14 
15 #include "UN_API.h"
16 #include "UN_DataBuffer.h"
17 #include "UN_DataIndexMap.h"
18 #include <UT/UT_UniquePtr.h>
19 
20 class UN_Data;
21 class UN_DataMergeInfo;
22 class UN_CustomData;
24 
25 
26 // ============================================================================
27 /// Container for custom data in UN.
28 /// It is intended to hold vectorized buffers indexed by data index value.
29 /// However, the UN API functions and methods usually operate on data IDs,
30 /// so this class provides a mapping from an ID to an INDEX.
32 {
33 public:
34  /// Constructor and destructor.
35  UN_CustomData() = default;
36  virtual ~UN_CustomData() = default;
37 
38  /// Returns an index map used for mapping IDs to indices.
39  /// Used by sub-classes to map an ID to an INDEX.
40  const UN_DataIndexMap * indexMap() const
41  { return myIndexMap; }
42 
43 private:
44  /// Returns a clone copy of this custom data object.
45  virtual UN_CustomDataPtr clone() const = 0;
46 
47  /// Sets the map from an ID to an INDEX of a slot in custom data buffers.
48  // Only UN_Data should need to set the index map.
49  friend UN_Data;
50  void setIndexMap( const UN_DataIndexMap *map )
51  { myIndexMap = map; }
52 
53 private:
54  /// A reference pointer to an index map.
55  /// This is a mapping from a data ID to an index of a slot in the
56  /// buffer array of custom data elements .
57  const UN_DataIndexMap * myIndexMap = nullptr;
58 };
59 
60 // ============================================================================
61 /// The interface for custom data containters, templetized for convenience
62 /// of using spcific IDs (eg, UN_NodeID, UN_PortID, etc),
63 /// rather than generic UN_DataID.
64 /// Note, this would normally be part of UN_CustomData, but templates
65 /// can complicate things, like forward declarations, so using a concrete
66 /// base class for that.
67 template <typename ID, typename INDEX>
69 {
70 public:
71  /// Increases the capacity of the data buffer arrays to the given minimum,
72  /// unless the capacity is already greater or equal to that minimum,
73  /// in which case the capacity remains unchanged.
74  /// This is a hint to the custom data, in case it uses data buffer arrays.
75  virtual void setCapacityIfNeeded(UN_DataSize min_capacity) = 0;
76 
77  /// Merge the other custom data into this one.
78  /// The merge info includes ID remapping as well as buffer spans
79  /// that can be copied in bulk.
80  /// The spans don't contain root data, (applicable only to node and subnet
81  /// data) so this method should merge any desired root data separately.
82  virtual void merge( const UN_CustomData &other,
83  const UN_DataMergeInfo &data_merge_info ) = 0;
84 
85  /// Adds a new data slot to accommodate a new piece of data
86  // for an element (eg, node) of a given ID, which corresponds
87  // to the given index (either can be used).
88  virtual void addData( ID id, INDEX index ) = 0;
89 
90  /// Clears the data slot of a given ID and at the corresponding index
91  /// as if it was newly created.
92  virtual void clearData( ID id, INDEX index ) = 0;
93 
94  /// Frees the data slot occupied by the item given by the id and
95  /// a corresponding index (either can be used), clearing it if necessary.
96  virtual void removeData( ID id, INDEX index ) = 0;
97 
98  /// Frees all the the data slots occupied by all the items, clearing
99  /// them if necessary.
100  virtual void removeAllData() = 0;
101 
102  /// Returns true if the given ID refers to a valid data entry
103  /// in the data buffers. Ie, such a piece of data exists and is valid.
104  bool isValid( ID id ) const
105  {
106  return indexMap() && indexMap()->isValid( id );
107  }
108 
109  /// Returns the data index into the data buffer arrays given its ID.
110  INDEX dataIndex( ID id ) const
111  {
112  return indexMap()
113  ? INDEX( indexMap()->indexFromID( id ))
114  : INDEX();
115  }
116 
117  /// Returns the size of the data buffers that use indexing based
118  /// on the `dataIndex()` lookup call that maps IDs to indices.
119  /// The size is lagest index issued plus one.
121  {
122  return indexMap()
123  ? indexMap()->dataBufferSize()
124  : UN_DataSize(0);
125  }
126 protected:
127  /// Helper method to copy-assign the value to an entry in the data buffer.
128  template<typename V, typename B>
129  inline void
130  setData( B &buffer, ID id, const V &value )
131  {
132  UN_DataBufferUtils::setData( buffer, dataIndex(id), value );
133  }
134 
135  /// Helper method to move-assign the value to an entry in the data buffer.
136  template<typename V, typename B>
137  inline void
138  setData( B &buffer, ID id, V &&value )
139  {
140  UN_DataBufferUtils::setData( buffer, dataIndex(id),
141  std::forward<V>(value));
142  }
143 
144  /// Helper method to return a const ref to an entry in the data buffer.
145  template<typename V, typename B>
146  inline const V &
147  getDataRef( const B &buffer, ID id, const V &default_value ) const
148  {
149  return UN_DataBufferUtils::getDataRef( buffer, dataIndex(id),
150  default_value );
151  }
152 
153  /// Returns a value constructed from an entry in the data buffer.
154  /// This is useful when the type returned here differs from
155  /// the underlying entry type.
156  /// In particular, this is needed for UN_BoolDataBuffer whose underlying
157  /// entries are bits (for which we can't return a const reference)
158  /// while here we are returning bool (byte).
159  template<typename V, typename B>
160  inline V
161  getDataVal( const B &buffer, ID id, const V &default_value ) const
162  {
163  return UN_DataBufferUtils::getDataVal( buffer, dataIndex(id),
164  default_value );
165  }
166 
167  /// Helper method to return a value moved from an entry in the data buffer.
168  template<typename V, typename B>
169  inline V
170  stealData( B &buffer, ID id, const V &default_value )
171  {
172  return UN_DataBufferUtils::stealData( buffer, dataIndex(id),
173  default_value );
174  }
175 
176  /// Helper method to update an entry value in the data buffer.
177  /// It updates a data value in-place in the data buffer,
178  /// which for complex data values (eg, dictionaries or arrays) is faster
179  /// than getting and setting the value (although stealing and setting
180  /// may be comparable). This is a convenient and useful idiom, that
181  /// tries to avoid the buffer data aliasing issues.
182  /// Intended usage:
183  /// updateData(buff, id, [&key, &value](auto &x) { x[key] = value; });
184  template<typename OP, typename B>
185  inline void
186  updateData( B &buffer, ID id, const OP &op )
187  {
188  UN_DataBufferUtils::updateData( buffer, dataIndex(id), op );
189  }
190 };
191 
192 #endif
193 
V getDataVal(const B &buffer, UN_DataIndex index, const V &default_value)
void setData(B &buffer, ID id, V &&value)
Helper method to move-assign the value to an entry in the data buffer.
V getDataVal(const B &buffer, ID id, const V &default_value) const
V stealData(B &buffer, ID id, const V &default_value)
Helper method to return a value moved from an entry in the data buffer.
GLsizei const GLfloat * value
Definition: glcorearb.h:824
UN_DataSize dataBufferSize() const
void updateData(B &buffer, ID id, const OP &op)
GLuint buffer
Definition: glcorearb.h:660
virtual void setCapacityIfNeeded(UN_DataSize min_capacity)=0
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:39
UN_DataSize dataBufferSize() const
virtual void removeData(ID id, INDEX index)=0
UN_CustomData()=default
Constructor and destructor.
bool isValid(ID id) const
void setData(B &buffer, ID id, const V &value)
Helper method to copy-assign the value to an entry in the data buffer.
INDEX dataIndex(ID id) const
Returns the data index into the data buffer arrays given its ID.
virtual void removeAllData()=0
Maintains a mapping from data ID to data index in the data buffer.
virtual void merge(const UN_CustomData &other, const UN_DataMergeInfo &data_merge_info)=0
GLuint index
Definition: glcorearb.h:786
bool isValid(UN_DataID data_id) const
Returns true if the given ID refers to a valid entry in the map.
virtual void addData(ID id, INDEX index)=0
Adds a new data slot to accommodate a new piece of data.
virtual void clearData(ID id, INDEX index)=0
UT_UniquePtr< UN_CustomData > UN_CustomDataPtr
Definition: UN_CustomData.h:23
const UN_DataIndexMap * indexMap() const
Definition: UN_CustomData.h:40
virtual ~UN_CustomData()=default
const V & getDataRef(const B &buffer, ID id, const V &default_value) const
Helper method to return a const ref to an entry in the data buffer.