HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UN_SubnetData.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_SubnetData.h ( UN Library, C++)
7  *
8  * COMMENTS:
9  * Core functionality for a utility-based subnet data.
10  */
11 
12 #ifndef __UN_SubnetData_h__
13 #define __UN_SubnetData_h__
14 
15 #include "UN_API.h"
16 #include "UN_Data.h"
17 class UN_GraphData;
18 
19 
20 // ============================================================================
23 
24 // ============================================================================
25 /// Container of data for all the subnetworks in the graph.
26 ///
27 /// NOTE: This class is generally not trivially relocatable, because
28 /// its base class (UN_Data) is not trivially relocatable (see comment there).
29 
31 {
32 public:
33  /// Constructors and destructors.
36  ~UN_SubnetData() = default;
37 
40 
41 protected:
42  /// This container can have large data, so generally, forbid
43  /// the costly copy operations to the public.
44  /// Knowledgable friends and derived classes can still use them.
45  friend UN_GraphData;
46  UN_SubnetData(const UN_SubnetData&) = default;
47  UN_SubnetData & operator=(const UN_SubnetData&) = default;
48 
49 public:
50  /// Increases the capacity of the data buffer arrays to the given minimum,
51  /// unless the capacity is already greater or equal to that minimum,
52  /// in which case the capacity remains unchanged.
53  void setCapacityIfNeeded( UN_DataSize min_capacity );
54 
55  /// @{ Returns subnet's custom data container, if one was provided to c-tor.
57  {
59  }
60 
62  {
63  return UTverify_cast<const UN_CustomSubnetData*>( customData() );
64  }
65  /// @}
66 
67  /// Merges another subnet data into this one.
68  /// Returns a mapping between the data IDs in the merge source
69  /// and destination (this) data buffers.
70  UN_SubnetIDRemap merge( const UN_SubnetData &src_subnet_data );
71 
72  /// Adds a slot for a new subnet in this container.
73  /// Returns an ID that should be used to refer to this new subnet
74  /// for accessing its data (eg, children, etc).
75  UN_SubnetID addSubnet();
76 
77  /// Adds a root subnet or returns one if it already exists.
78  UN_SubnetID findOrAddRootSubnet();
79 
80  /// Clears the data for the given subnet to the default state.
81  void clearSubnet( UN_SubnetID subnet_id );
82 
83  /// Removes the subnet from this container and frees the
84  /// data slot it occupied. After calling this method,
85  /// the container will have no knowledge of the node by this ID.
86  /// Returns true on successs; false if the node was not found.
87  bool removeSubnet( UN_SubnetID subnet_id );
88 
89  /// Frees the data slots occupied by all the subnets.
90  /// @param 'reset_next_id' If true, the node ID generator is reset to zero.
91  void removeAllSubnets( bool reset_next_id );
92 
93  /// Returns true if the given ID refers to a valid subnet entry
94  /// in the data buffers. Ie, such a subnet exists and is valid.
95  bool isValid( UN_SubnetID subnet_id ) const
96  { return UN_Data::isValid( subnet_id ); }
97 
98 
99  /// Iterator for traversing valid subnet IDs.
101  {
102  public:
103  using value_type = UN_SubnetID;
104 
106  : UN_DataIndexMap::IDIterator( id_iterator )
107  {}
108 
110  { return UN_SubnetID( UN_DataIndexMap::IDIterator::operator*() ); }
111  };
112 
113  /// Iterator for traversing valid subnet IDs in an ascending order.
115  {
116  public:
117  using value_type = UN_SubnetID;
118 
120  : UN_DataIndexMap::OrderedIDIterator( id_iterator )
121  {}
122 
124  { return UN_SubnetID(UN_DataIndexMap::OrderedIDIterator::operator*()); }
125  };
126 
127 
128  /// Returns a range for iterating valid subnet IDs.
130  IDRange idRange() const
131  {
132  auto id_range( UN_Data::idRange() );
133  return IDRange( id_range.begin(), id_range.end() );
134  }
135 
136  /// Returns a range for iterating valid subnet IDs.
139  {
140  auto id_range( UN_Data::orderedIDRange() );
141  return OrderedIDRange( id_range.begin(), id_range.end() );
142  }
143 
144  /// Returns a list of all the valid subnet IDs in the graph.
145  UN_SubnetIDList subnetIDs() const
146  {
147  SYS_STATIC_ASSERT( sizeof(UN_SubnetID) == sizeof(UN_DataID) );
148  return reinterpret_cast<UN_SubnetIDList&&>( std::move( dataIDs() ));
149  }
150 
151  /// Returns a list of all the valid subnet IDs in the graph.
152  UN_SubnetIDList sortedSubnetIDs() const
153  {
154  SYS_STATIC_ASSERT( sizeof(UN_SubnetID) == sizeof(UN_DataID) );
155  return reinterpret_cast<UN_SubnetIDList&&>(std::move(sortedDataIDs()));
156  }
157 
158 
159 protected:
160  /// Returns the subnet data index into the data buffer arrays.
161  UN_SubnetIndex subnetIndex( UN_SubnetID id ) const
162  {
163  return UN_SubnetIndex(
164  UN_Data::dataIndex( id ));
165  }
166 
167 private:
168  /// Returns the current size of the internals data buffers.
169  /// The number of valid entries (slots) in data buffers is less than
170  /// or equal to that number, since some data slots may be free.
171  UN_DataSize coreDataBufferSize() const
172  {
173  UT_ASSERT( isCoreDataSizeConsistent() );
174  // NOTE: currently subnets don't have
175  // any intrinsic data, so buffer
176  // size is always zero.
177  return UN_DataSize(0) ;
178  }
179 
180 
181  /// Helper that adds a new data slot for the given subnet.
182  UN_SubnetID addSubnetData(UN_SubnetID subnet_id,
183  UN_SubnetIndex subnet_index );
184 
185 
186  /// Handles merging of another subnet data container into this one.
187  void mergeCoreData( const UN_SubnetData &src,
188  const UN_DataMergeInfo &merge_info );
189 
190  /// Adds a new data slot to accommodate a new data piece at the given index.
191  void addCoreData( UN_SubnetIndex subnet_index );
192 
193  /// Clears the data for the given subnet to the default state.
194  void clearCoreData( UN_SubnetIndex subnet_index );
195 
196  /// Frees the data slot occupied by the given subnet,
197  /// clearing it if necessary.
198  void removeCoreData() //, index)
199  // Nothing urgent to free; will clear lazily
200  { UT_ASSERT( isDataSizeConsistent() ); }
201 
202  /// Frees all the data slots.
203  void removeAllCoreData();
204 
205  /// Sets the capacity of core data buffers if needed.
206  void setCoreDataCapacityIfNeeded(
207  UN_DataSize min_capacity );
208 
209  /// Returns true if the sizes of all core data buffers are equal.
210  bool isCoreDataSizeConsistent() const;
211 
212  /// Returns true if the sizes of data buffers and index map are equal.
213  bool isDataSizeConsistent() const;
214 
215 private:
216  // TODO: FIXME: Add a cache for child ID lookup (child name -> child id)
217  // The graph can use it in conjunciton with topology to speed
218  // up the child lookup based on its name.
219  // This class is where such map logically belongs, since
220  // topology contains minimal set of only stictly necessary
221  // relations. Name to ID is an optional optimization.
222 };
223 
224 
225 #endif
IDRange idRange() const
Definition: UN_Data.h:55
value_type operator*() const
#define SYS_STATIC_ASSERT(expr)
friend UN_GraphData
Definition: UN_SubnetData.h:45
UN_SubnetIDList subnetIDs() const
Returns a list of all the valid subnet IDs in the graph.
OrderedIDRange orderedIDRange() const
Iterator for traversing valid subnet IDs in an ascending order.
bool isValid(UN_DataID data_id) const
Definition: UN_Data.h:140
UN_SubnetIDList sortedSubnetIDs() const
Returns a list of all the valid subnet IDs in the graph.
SYS_FORCE_INLINE TO_T UTverify_cast(FROM_T from)
Definition: UT_Assert.h:242
Iterator for traversing valid data IDs in the map.
#define UN_API
Definition: UN_API.h:11
UN_Data & operator=(UN_Data &&)
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:39
UN_DataIndex dataIndex(UN_DataID id) const
Definition: UN_Data.h:147
UT_UniquePtr< UN_CustomSubnetData > UN_CustomSubnetDataPtr
Definition: UN_SubnetData.h:22
Iterator for traversing valid subnet IDs.
UN_CustomSubnetData * customSubnetData()
Returns subnet's custom data container, if one was provided to c-tor.
Definition: UN_SubnetData.h:56
UN_CustomData * customData()
Returns the custom data container, if one was provided.
Definition: UN_Data.h:47
UN_DataIDList dataIDs() const
Returns a list of all the valid IDs in this container.
Definition: UN_Data.h:64
UN_DataIndexMap::IDRange IDRange
Returns a range for iterating valid IDs.
Definition: UN_Data.h:54
Maintains a mapping from data ID to data index in the data buffer.
Iterator for traversing valid data IDs in the map in an ordered fashion.
IDIterator(const UN_DataIndexMap::IDIterator &id_iterator)
bool isValid(UN_SubnetID subnet_id) const
Definition: UN_SubnetData.h:95
const UN_CustomSubnetData * customSubnetData() const
Returns subnet's custom data container, if one was provided to c-tor.
Definition: UN_SubnetData.h:61
UN_SubnetIndex subnetIndex(UN_SubnetID id) const
Returns the subnet data index into the data buffer arrays.
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:165
OutGridT XformOp bool bool MergePolicy merge
UN_DataIDList sortedDataIDs() const
Returns a sorted list of all the valid IDs in this container.
Definition: UN_Data.h:68
OrderedIDIterator(const UN_DataIndexMap::OrderedIDIterator &id_iterator)
UN_DataIndexMap::OrderedIDRange OrderedIDRange
Returns a range for iterating valid IDs in an ascending order.
Definition: UN_Data.h:59
Definition: UNI_ID.h:25
OrderedIDRange orderedIDRange() const
Definition: UN_Data.h:60
IDRange idRange() const
GLenum src
Definition: glcorearb.h:1793