All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
parserValueContext.h
1 //
2 // Copyright 2016 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 SDF_PARSERVALUECONTEXT_H
25 #define SDF_PARSERVALUECONTEXT_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/usd/sdf/parserHelpers.h"
29 #include "pxr/base/vt/array.h"
30 
31 #include <functional>
32 #include <string>
33 #include <vector>
34 
35 PXR_NAMESPACE_OPEN_SCOPE
36 
37 // Parses nested arrays of atomic values or tuples of atomic values. Validity
38 // checks are done while parsing to make sure arrays are "square" and tuples
39 // are all the same size. Each atomic value (number or string) is accumulated
40 // during parsing and all atomic values are used to produce a VtValue after
41 // parsing has finished. Example usage:
42 //
43 // SetupFactory("Point[]");
44 // BeginList();
45 // BeginTuple();
46 // AppendValue(1);
47 // AppendValue(2);
48 // AppendValue(3);
49 // EndTuple();
50 // BeginTuple();
51 // AppendValue(2);
52 // AppendValue(3);
53 // AppendValue(4);
54 // EndTuple();
55 // EndList();
56 // ProduceValue() == VtArray<Vec3d> { Vec3d(1, 2, 3), Vec3d(2, 3, 4) };
57 //
58 // Value factories are retrieved with GetValueFactoryForMenvaName(), which uses
59 // preprocessor-generated factories from _SDF_VALUE_TYPES.
60 //
61 // Ideally this would be self-contained, but the parser currently accesses lots
62 // of public member variables.
63 class Sdf_ParserValueContext {
64 public:
65  typedef Sdf_ParserHelpers::Value Value;
66  typedef std::function<void (const std::string &)> ErrorReporter;
67 
68  Sdf_ParserValueContext();
69 
70  // Sets up this context to produce a value with C++ type determined by
71  // the given \p typeName.
72  //
73  // Returns true if the given type is valid and recognized, false
74  // otherwise. If false is returned, the context will be unable to
75  // produce a value for this type.
76  bool SetupFactory(const std::string &typeName);
77 
78  // Make a shaped value from parsed context.
79  VtValue ProduceValue(std::string *errStrPtr);
80 
81  void Clear();
82 
83  void AppendValue(const Value& value);
84 
85  // Called before each list, corresponds to the '[' token
86  void BeginList();
87 
88  // Called after each list, corresponds to the ']' token
89  void EndList();
90 
91  // Called before each tuple, corresponds to the '(' token
92  void BeginTuple();
93 
94  // Called after each tuple, corresponds to the ')' token
95  void EndTuple();
96 
97  int dim;
98  std::vector<unsigned int> shape;
99  int tupleDepth;
100  SdfTupleDimensions tupleDimensions;
101  std::vector<Value> vars;
102  std::vector<unsigned int> workingShape;
103 
104  // The recorded dim at which we got our first AppendValue.
105  // If we get subsequent pushes where dim != pushDim, it is an error
106  // (eg [1, 2, [3, 4]]). Initially it is -1 to indicate we have never
107  // appended anything.
108  int pushDim;
109 
110  // The cached value factory information.
111  std::string valueTypeName;
112  bool valueTypeIsValid;
113  std::string lastTypeName;
114  Sdf_ParserHelpers::ValueFactoryFunc valueFunc;
115  bool valueIsShaped;
116  SdfTupleDimensions valueTupleDimensions;
117 
118  // A function to report textual errors as they are encountered. This is set
119  // to a function that calls TF_CODING_ERROR() by default, but is customizable
120  // so the parser can report parse errors instead.
121  ErrorReporter errorReporter;
122 
123  // To record a textual representation of the parsed value, call
124  // StartRecordingString() before parsing begins and GetRecordedString()
125  // after parsing ends. The string will continue to be accumulated until
126  // Clear() is called (ProduceValue() calls Clear() automatically).
127  void StartRecordingString();
128  void StopRecordingString();
129 
130  bool IsRecordingString() const;
131  std::string GetRecordedString() const;
132 
133  // Hook to override the recorded text
134  void SetRecordedString(const std::string &text);
135 
136 private:
137  bool _needComma, _isRecordingString;
138  std::string _recordedString;
139 };
140 
141 PXR_NAMESPACE_CLOSE_SCOPE
142 
143 #endif // SDF_PARSERVALUECONTEXT_H
Represents the shape of a value type (or that of an element in an array).
Definition: valueTypeName.h:46
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:182