containerDataSourceEditor.h
1 //
2 // Copyright 2021 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef PXR_IMAGING_HD_CONTAINER_DATA_SOURCE_EDITOR_H
25 #define PXR_IMAGING_HD_CONTAINER_DATA_SOURCE_EDITOR_H
26 
27 #include "pxr/imaging/hd/dataSource.h"
28 
30 
31 PXR_NAMESPACE_OPEN_SCOPE
32 
33 // utility for lazily constructing and composing data source hierarchies
34 class HdContainerDataSourceEditor
35 {
36 public:
37 
38  HdContainerDataSourceEditor() {}
39  HdContainerDataSourceEditor(
40  HdContainerDataSourceHandle initialContainer)
41  : _initialContainer(initialContainer) {}
42 
43  // Replaces data source at given locator and descending locations
44  // (if given a container data source) by given data source.
45  HD_API
46  HdContainerDataSourceEditor &Set(
47  const HdDataSourceLocator &locator,
48  const HdDataSourceBaseHandle &dataSource);
49 
50  // Overlays data source at given location by given data source so that
51  // data sources in the initial container at descending locations can
52  // still come through.
53  HD_API
54  HdContainerDataSourceEditor &Overlay(
55  const HdDataSourceLocator &locator,
56  const HdContainerDataSourceHandle &containerDataSource);
57 
58  HD_API
59  HdContainerDataSourceHandle Finish();
60 
61 private:
62  HdContainerDataSourceHandle _FinishWithNoInitialContainer();
63 
64  struct _Node;
65  using _NodeSharedPtr = std::shared_ptr<_Node>;
66 
67  struct _Entry
68  {
69  HdDataSourceBaseHandle dataSource;
70  _NodeSharedPtr childNode;
71  };
72 
73  struct _Node
74  {
75  using EntryMap = TfDenseHashMap<TfToken, _Entry,
76  TfToken::HashFunctor, std::equal_to<TfToken>, 8>;
77  EntryMap entries;
78  };
79 
80  _NodeSharedPtr _root;
81  HdContainerDataSourceHandle _initialContainer;
82 
83  // Calling Set with a container data source should mask any existing
84  // container child values coming from _initialContainer. If that's defined,
85  // record the paths for which containers have been set in order to build
86  // a hierarchy with HdBlockDataSources as leaves to place between.
87  TfSmallVector<HdDataSourceLocator, 4> _directContainerSets;
88 
89  _NodeSharedPtr _GetNode(const HdDataSourceLocator & locator);
90 
91  class _NodeContainerDataSource : public HdContainerDataSource
92  {
93  public:
94  HD_DECLARE_DATASOURCE(_NodeContainerDataSource);
95  _NodeContainerDataSource(_NodeSharedPtr node);
96 
97  TfTokenVector GetNames() override;
98  HdDataSourceBaseHandle Get(const TfToken &name) override;
99 
100  private:
101  _NodeSharedPtr _node;
102  };
103 };
104 
105 PXR_NAMESPACE_CLOSE_SCOPE
106 
107 #endif
Functor to use for hash maps from tokens to other things.
Definition: token.h:166
A datasource representing structured (named, hierarchical) data, for example a geometric primitive or...
Definition: dataSource.h:110
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
Represents an object that can identify the location of a data source.
This is a space efficient container that mimics the TfHashMap API that uses a vector for storage when...
Definition: denseHashMap.h:59
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:442
static HD_API HdDataSourceBaseHandle Get(const Handle &container, const HdDataSourceLocator &locator)
A convenience function: given container, return the descendant identified by locator,...