Blender  V3.3
BLI_serialize.hh
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 #pragma once
4 
61 #include <ostream>
62 
63 #include "BLI_map.hh"
64 #include "BLI_string_ref.hh"
65 #include "BLI_vector.hh"
66 
68 
74 enum class eValueType {
75  String,
76  Int,
77  Array,
78  Null,
79  Boolean,
80  Double,
81  Dictionary,
82 };
83 
84 class Value;
85 class StringValue;
86 class DictionaryValue;
87 template<typename T, eValueType V> class PrimitiveValue;
91 
92 template<typename Container, eValueType V, typename ContainerItem = typename Container::value_type>
93 class ContainerValue;
94 /* ArrayValue stores its items as shared pointer as it shares data with a lookup table that can
95  * be created by calling `create_lookup`. */
97 
115 class Value {
116  private:
117  eValueType type_;
118 
119  protected:
120  Value() = delete;
121  explicit Value(eValueType type) : type_(type)
122  {
123  }
124 
125  public:
126  virtual ~Value() = default;
127  eValueType type() const
128  {
129  return type_;
130  }
131 
136  const StringValue *as_string_value() const;
137 
142  const IntValue *as_int_value() const;
143 
148  const DoubleValue *as_double_value() const;
149 
154  const BooleanValue *as_boolean_value() const;
155 
160  const ArrayValue *as_array_value() const;
161 
166  const DictionaryValue *as_dictionary_value() const;
167 };
168 
172 template<
174  typename T,
176  eValueType V>
177 class PrimitiveValue : public Value {
178  private:
179  T inner_value_{};
180 
181  public:
182  explicit PrimitiveValue(const T value) : Value(V), inner_value_(value)
183  {
184  }
185 
186  const T value() const
187  {
188  return inner_value_;
189  }
190 };
191 
192 class NullValue : public Value {
193  public:
195  {
196  }
197 };
198 
199 class StringValue : public Value {
200  private:
201  std::string string_;
202 
203  public:
204  StringValue(const StringRef string) : Value(eValueType::String), string_(string)
205  {
206  }
207 
208  const std::string &value() const
209  {
210  return string_;
211  }
212 };
213 
219 template<
221  typename Container,
222 
224  eValueType V,
225 
227  typename ContainerItem>
228 class ContainerValue : public Value {
229  public:
230  using Items = Container;
231  using Item = ContainerItem;
232 
233  private:
234  Container inner_value_;
235 
236  public:
238  {
239  }
240 
241  const Container &elements() const
242  {
243  return inner_value_;
244  }
245 
246  Container &elements()
247  {
248  return inner_value_;
249  }
250 };
251 
258 using DictionaryElementType = std::pair<std::string, std::shared_ptr<Value>>;
259 
265  : public ContainerValue<Vector<DictionaryElementType>, eValueType::Dictionary> {
266  public:
267  using LookupValue = std::shared_ptr<Value>;
269 
275  const Lookup create_lookup() const
276  {
277  Lookup result;
278  for (const Item &item : elements()) {
279  result.add_as(item.first, item.second);
280  }
281  return result;
282  }
283 };
284 
288 class Formatter {
289  public:
290  virtual ~Formatter() = default;
291 
293  virtual void serialize(std::ostream &os, const Value &value) = 0;
294 
296  virtual std::unique_ptr<Value> deserialize(std::istream &is) = 0;
297 };
298 
302 class JsonFormatter : public Formatter {
303  public:
309 
310  public:
311  void serialize(std::ostream &os, const Value &value) override;
312  std::unique_ptr<Value> deserialize(std::istream &is) override;
313 };
314 
315 } // namespace blender::io::serialize
Group Output data from inside of a node group A color picker Mix two input colors RGB to Convert a color s luminance to a grayscale value Generate a normal vector and a dot product Bright Control the brightness and contrast of the input color Vector Map an input vectors to used to fine tune the interpolation of the input Camera Retrieve information about the camera and how it relates to the current shading point s position Clamp a value between a minimum and a maximum Vector Perform vector math operation Invert a producing a negative Combine Generate a color from its and blue Hue Saturation Value
const Container & elements() const
std::shared_ptr< Value > LookupValue
virtual std::unique_ptr< Value > deserialize(std::istream &is)=0
virtual void serialize(std::ostream &os, const Value &value)=0
std::unique_ptr< Value > deserialize(std::istream &is) override
Definition: serialize.cc:212
void serialize(std::ostream &os, const Value &value) override
Definition: serialize.cc:200
StringValue(const StringRef string)
const std::string & value() const
const BooleanValue * as_boolean_value() const
Definition: serialize.cc:33
const ArrayValue * as_array_value() const
Definition: serialize.cc:41
const StringValue * as_string_value() const
Definition: serialize.cc:9
const IntValue * as_int_value() const
Definition: serialize.cc:17
const DoubleValue * as_double_value() const
Definition: serialize.cc:25
const DictionaryValue * as_dictionary_value() const
Definition: serialize.cc:49
#define T
std::pair< std::string, std::shared_ptr< Value > > DictionaryElementType
signed char int8_t
Definition: stdint.h:75
CCL_NAMESPACE_BEGIN struct Window V