98.63% Lines (72/73) 100.00% Functions (10/10)
TLA Baseline Branch
Line Hits Code Line Hits Code
1   // 1   //
2   // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com) 2   // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
3   // 3   //
4   // Distributed under the Boost Software License, Version 1.0. (See accompanying 4   // Distributed under the Boost Software License, Version 1.0. (See accompanying
5   // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 5   // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6   // 6   //
7   // Official repository: https://github.com/boostorg/json 7   // Official repository: https://github.com/boostorg/json
8   // 8   //
9   9  
10   #ifndef BOOST_JSON_IMPL_VALUE_REF_IPP 10   #ifndef BOOST_JSON_IMPL_VALUE_REF_IPP
11   #define BOOST_JSON_IMPL_VALUE_REF_IPP 11   #define BOOST_JSON_IMPL_VALUE_REF_IPP
12   12  
13   #include <boost/json/value_ref.hpp> 13   #include <boost/json/value_ref.hpp>
14   #include <boost/json/array.hpp> 14   #include <boost/json/array.hpp>
15   #include <boost/json/value.hpp> 15   #include <boost/json/value.hpp>
16   16  
17   namespace boost { 17   namespace boost {
18   namespace json { 18   namespace json {
19   19  
HITCBC 20   3241 value_ref:: 20   3241 value_ref::
21   operator 21   operator
22   value() const 22   value() const
23   { 23   {
HITCBC 24   3241 return make_value({}); 24   3241 return make_value({});
25   } 25   }
26   26  
27   bool 27   bool
HITCBC 28   860 value_ref:: 28   860 value_ref::
29   is_key_value_pair() const noexcept 29   is_key_value_pair() const noexcept
30   { 30   {
HITCBC 31   860 if(what_ != what::ini) 31   860 if(what_ != what::ini)
HITCBC 32   351 return false; 32   351 return false;
HITCBC 33   509 if(arg_.init_list_.size() != 2) 33   509 if(arg_.init_list_.size() != 2)
HITCBC 34   6 return false; 34   6 return false;
35   auto const& e = 35   auto const& e =
HITCBC 36   503 *arg_.init_list_.begin(); 36   503 *arg_.init_list_.begin();
HITCBC 37   503 if( e.what_ != what::str && 37   503 if( e.what_ != what::str &&
HITCBC 38   25 e.what_ != what::strfunc) 38   25 e.what_ != what::strfunc)
HITCBC 39   23 return false; 39   23 return false;
HITCBC 40   480 return true; 40   480 return true;
41   } 41   }
42   42  
43   bool 43   bool
HITCBC 44   513 value_ref:: 44   513 value_ref::
45   maybe_object( 45   maybe_object(
46   std::initializer_list< 46   std::initializer_list<
47   value_ref> init) noexcept 47   value_ref> init) noexcept
48   { 48   {
HITCBC 49   993 for(auto const& e : init) 49   993 for(auto const& e : init)
HITCBC 50   860 if(! e.is_key_value_pair()) 50   860 if(! e.is_key_value_pair())
HITCBC 51   380 return false; 51   380 return false;
HITCBC 52   133 return true; 52   133 return true;
53   } 53   }
54   54  
55   string_view 55   string_view
HITCBC 56   475 value_ref:: 56   475 value_ref::
57   get_string() const noexcept 57   get_string() const noexcept
58   { 58   {
HITCBC 59   475 BOOST_ASSERT( 59   475 BOOST_ASSERT(
60   what_ == what::str || 60   what_ == what::str ||
61   what_ == what::strfunc); 61   what_ == what::strfunc);
HITCBC 62   475 if (what_ == what::strfunc) 62   475 if (what_ == what::strfunc)
HITCBC 63   2 return *static_cast<const string*>(f_.p); 63   2 return *static_cast<const string*>(f_.p);
HITCBC 64   473 return arg_.str_; 64   473 return arg_.str_;
65   } 65   }
66   66  
67   value 67   value
HITCBC 68   9159 value_ref:: 68   9159 value_ref::
69   make_value( 69   make_value(
70   storage_ptr sp) const 70   storage_ptr sp) const
71   { 71   {
HITCBC 72   9159 switch(what_) 72   9159 switch(what_)
73   { 73   {
HITCBC 74   417 default: 74   417 default:
75   case what::str: 75   case what::str:
HITCBC 76   834 return string( 76   834 return string(
77   arg_.str_, 77   arg_.str_,
HITCBC 78   817 std::move(sp)); 78   817 std::move(sp));
79   79  
HITCBC 80   172 case what::ini: 80   172 case what::ini:
81   return make_value( 81   return make_value(
82   arg_.init_list_, 82   arg_.init_list_,
HITCBC 83   175 std::move(sp)); 83   175 std::move(sp));
84   84  
HITCBC 85   61 case what::func: 85   61 case what::func:
HITCBC 86   122 return f_.f(f_.p, 86   122 return f_.f(f_.p,
HITCBC 87   61 std::move(sp)); 87   61 std::move(sp));
88   88  
HITCBC 89   5 case what::strfunc: 89   5 case what::strfunc:
HITCBC 90   10 return f_.f(f_.p, 90   10 return f_.f(f_.p,
HITCBC 91   5 std::move(sp)); 91   5 std::move(sp));
92   92  
HITCBC 93   8504 case what::cfunc: 93   8504 case what::cfunc:
HITCBC 94   17008 return cf_.f(cf_.p, 94   17008 return cf_.f(cf_.p,
HITCBC 95   8504 std::move(sp)); 95   8504 std::move(sp));
96   } 96   }
97   } 97   }
98   98  
99   value 99   value
HITCBC 100   174 value_ref:: 100   174 value_ref::
101   make_value( 101   make_value(
102   std::initializer_list< 102   std::initializer_list<
103   value_ref> init, 103   value_ref> init,
104   storage_ptr sp) 104   storage_ptr sp)
105   { 105   {
HITCBC 106   174 if(maybe_object(init)) 106   174 if(maybe_object(init))
HITCBC 107   60 return make_object( 107   60 return make_object(
HITCBC 108   60 init, std::move(sp)); 108   60 init, std::move(sp));
HITCBC 109   288 return make_array( 109   288 return make_array(
HITCBC 110   285 init, std::move(sp)); 110   285 init, std::move(sp));
111   } 111   }
112   112  
113   object 113   object
HITCBC 114   133 value_ref:: 114   133 value_ref::
115   make_object( 115   make_object(
116   std::initializer_list<value_ref> init, 116   std::initializer_list<value_ref> init,
117   storage_ptr sp) 117   storage_ptr sp)
118   { 118   {
HITCBC 119   133 object obj(std::move(sp)); 119   133 object obj(std::move(sp));
HITCBC 120   133 obj.reserve(init.size()); 120   133 obj.reserve(init.size());
HITCBC 121   608 for(auto const& e : init) 121   608 for(auto const& e : init)
HITCBC 122   475 obj.emplace( 122   475 obj.emplace(
123   e.arg_.init_list_.begin()[0].get_string(), 123   e.arg_.init_list_.begin()[0].get_string(),
HITCBC 124   950 e.arg_.init_list_.begin()[1].make_value( 124   950 e.arg_.init_list_.begin()[1].make_value(
125   obj.storage())); 125   obj.storage()));
HITCBC 126   133 return obj; 126   133 return obj;
MISUBC 127   ✗ } 127   ✗ }
128   128  
129   array 129   array
HITCBC 130   366 value_ref:: 130   366 value_ref::
131   make_array( 131   make_array(
132   std::initializer_list< 132   std::initializer_list<
133   value_ref> init, 133   value_ref> init,
134   storage_ptr sp) 134   storage_ptr sp)
135   { 135   {
HITCBC 136   366 array arr(std::move(sp)); 136   366 array arr(std::move(sp));
HITCBC 137   366 arr.reserve(init.size()); 137   366 arr.reserve(init.size());
HITCBC 138   1382 for(auto const& e : init) 138   1382 for(auto const& e : init)
HITCBC 139   1019 arr.emplace_back( 139   1019 arr.emplace_back(
HITCBC 140   2038 e.make_value( 140   2038 e.make_value(
141   arr.storage())); 141   arr.storage()));
HITCBC 142   363 return arr; 142   363 return arr;
HITCBC 143   3 } 143   3 }
144   144  
145   void 145   void
HITCBC 146   241 value_ref:: 146   241 value_ref::
147   write_array( 147   write_array(
148   value* dest, 148   value* dest,
149   std::initializer_list< 149   std::initializer_list<
150   value_ref> init, 150   value_ref> init,
151   storage_ptr const& sp) 151   storage_ptr const& sp)
152   { 152   {
153   struct undo 153   struct undo
154   { 154   {
155   value* const base; 155   value* const base;
156   value* pos; 156   value* pos;
HITCBC 157   241 ~undo() 157   241 ~undo()
158   { 158   {
HITCBC 159   241 if(pos) 159   241 if(pos)
HITCBC 160   40 while(pos > base) 160   40 while(pos > base)
HITCBC 161   22 (--pos)->~value(); 161   22 (--pos)->~value();
HITCBC 162   241 } 162   241 }
163   }; 163   };
HITCBC 164   241 undo u{dest, dest}; 164   241 undo u{dest, dest};
HITCBC 165   817 for(auto const& e : init) 165   817 for(auto const& e : init)
166   { 166   {
HITCBC 167   18 ::new(u.pos) value( 167   18 ::new(u.pos) value(
HITCBC 168   630 e.make_value(sp)); 168   630 e.make_value(sp));
HITCBC 169   576 ++u.pos; 169   576 ++u.pos;
170   } 170   }
HITCBC 171   223 u.pos = nullptr; 171   223 u.pos = nullptr;
HITCBC 172   241 } 172   241 }
173   173  
174   } // namespace json 174   } // namespace json
175   } // namespace boost 175   } // namespace boost
176   176  
177   #endif 177   #endif