Horizon
json_ref.hpp
1 #pragma once
2 
3 #include <initializer_list>
4 #include <utility>
5 
6 namespace nlohmann
7 {
8 namespace detail
9 {
10 template<typename BasicJsonType>
11 class json_ref
12 {
13  public:
14  using value_type = BasicJsonType;
15 
16  json_ref(value_type&& value)
17  : owned_value(std::move(value)), value_ref(&owned_value), is_rvalue(true)
18  {}
19 
20  json_ref(const value_type& value)
21  : value_ref(const_cast<value_type*>(&value)), is_rvalue(false)
22  {}
23 
24  json_ref(std::initializer_list<json_ref> init)
25  : owned_value(init), value_ref(&owned_value), is_rvalue(true)
26  {}
27 
28  template<class... Args>
29  json_ref(Args&& ... args)
30  : owned_value(std::forward<Args>(args)...), value_ref(&owned_value), is_rvalue(true)
31  {}
32 
33  // class should be movable only
34  json_ref(json_ref&&) = default;
35  json_ref(const json_ref&) = delete;
36  json_ref& operator=(const json_ref&) = delete;
37 
38  value_type moved_or_copied() const
39  {
40  if (is_rvalue)
41  {
42  return std::move(*value_ref);
43  }
44  return *value_ref;
45  }
46 
47  value_type const& operator*() const
48  {
49  return *static_cast<value_type const*>(value_ref);
50  }
51 
52  value_type const* operator->() const
53  {
54  return static_cast<value_type const*>(value_ref);
55  }
56 
57  private:
58  mutable value_type owned_value = nullptr;
59  value_type* value_ref = nullptr;
60  const bool is_rvalue;
61 };
62 }
63 }
nlohmann
namespace for Niels Lohmann
Definition: adl_serializer.hpp:8
nlohmann::detail::json_ref
Definition: json_ref.hpp:11