object.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. //
  2. // Copyright (c) 2019 Vinnie Falco ([email protected])
  3. //
  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)
  6. //
  7. // Official repository: https://github.com/boostorg/json
  8. //
  9. #ifndef BOOST_JSON_IMPL_OBJECT_HPP
  10. #define BOOST_JSON_IMPL_OBJECT_HPP
  11. #include <boost/json/value.hpp>
  12. #include <iterator>
  13. #include <cmath>
  14. #include <type_traits>
  15. #include <utility>
  16. namespace boost {
  17. namespace json {
  18. namespace detail {
  19. // Objects with size less than or equal
  20. // to this number will use a linear search
  21. // instead of the more expensive hash function.
  22. static
  23. constexpr
  24. std::size_t
  25. small_object_size_ = 18;
  26. BOOST_STATIC_ASSERT(
  27. small_object_size_ <
  28. BOOST_JSON_MAX_STRUCTURED_SIZE);
  29. } // detail
  30. //----------------------------------------------------------
  31. struct alignas(key_value_pair)
  32. object::table
  33. {
  34. std::uint32_t size = 0;
  35. std::uint32_t capacity = 0;
  36. std::uintptr_t salt = 0;
  37. #if defined(_MSC_VER) && BOOST_JSON_ARCH == 32
  38. // VFALCO If we make key_value_pair smaller,
  39. // then we might want to revisit this
  40. // padding.
  41. BOOST_STATIC_ASSERT(
  42. sizeof(key_value_pair) == 32);
  43. char pad[4] = {}; // silence warnings
  44. #endif
  45. constexpr table();
  46. // returns true if we use a linear
  47. // search instead of the hash table.
  48. bool is_small() const noexcept
  49. {
  50. return capacity <=
  51. detail::small_object_size_;
  52. }
  53. key_value_pair&
  54. operator[](
  55. std::size_t pos) noexcept
  56. {
  57. return reinterpret_cast<
  58. key_value_pair*>(
  59. this + 1)[pos];
  60. }
  61. // VFALCO This is exported for tests
  62. BOOST_JSON_DECL
  63. std::size_t
  64. digest(string_view key) const noexcept;
  65. inline
  66. index_t&
  67. bucket(std::size_t hash) noexcept;
  68. inline
  69. index_t&
  70. bucket(string_view key) noexcept;
  71. inline
  72. void
  73. clear() noexcept;
  74. static
  75. inline
  76. table*
  77. allocate(
  78. std::size_t capacity,
  79. std::uintptr_t salt,
  80. storage_ptr const& sp);
  81. static
  82. void
  83. deallocate(
  84. table* p,
  85. storage_ptr const& sp) noexcept
  86. {
  87. if(p->capacity == 0)
  88. return;
  89. if(! p->is_small())
  90. sp->deallocate(p,
  91. sizeof(table) + p->capacity * (
  92. sizeof(key_value_pair) +
  93. sizeof(index_t)));
  94. else
  95. sp->deallocate(p,
  96. sizeof(table) + p->capacity *
  97. sizeof(key_value_pair));
  98. }
  99. };
  100. //----------------------------------------------------------
  101. class object::revert_construct
  102. {
  103. object* obj_;
  104. BOOST_JSON_DECL
  105. void
  106. destroy() noexcept;
  107. public:
  108. explicit
  109. revert_construct(
  110. object& obj) noexcept
  111. : obj_(&obj)
  112. {
  113. }
  114. ~revert_construct()
  115. {
  116. if(! obj_)
  117. return;
  118. destroy();
  119. }
  120. void
  121. commit() noexcept
  122. {
  123. obj_ = nullptr;
  124. }
  125. };
  126. //----------------------------------------------------------
  127. class object::revert_insert
  128. {
  129. object* obj_;
  130. table* t_ = nullptr;
  131. std::size_t size_;
  132. BOOST_JSON_DECL
  133. void
  134. destroy() noexcept;
  135. public:
  136. explicit
  137. revert_insert(
  138. object& obj,
  139. std::size_t capacity)
  140. : obj_(&obj)
  141. , size_(obj_->size())
  142. {
  143. if( capacity > obj_->capacity() )
  144. t_ = obj_->reserve_impl(capacity);
  145. }
  146. ~revert_insert()
  147. {
  148. if(! obj_)
  149. return;
  150. destroy();
  151. if( t_ )
  152. {
  153. table::deallocate( obj_->t_, obj_->sp_ );
  154. obj_->t_ = t_;
  155. }
  156. else
  157. {
  158. obj_->t_->size = static_cast<index_t>(size_);
  159. }
  160. }
  161. void
  162. commit() noexcept
  163. {
  164. BOOST_ASSERT(obj_);
  165. if( t_ )
  166. table::deallocate( t_, obj_->sp_ );
  167. obj_ = nullptr;
  168. }
  169. };
  170. //----------------------------------------------------------
  171. //
  172. // Iterators
  173. //
  174. //----------------------------------------------------------
  175. auto
  176. object::
  177. begin() noexcept ->
  178. iterator
  179. {
  180. return &(*t_)[0];
  181. }
  182. auto
  183. object::
  184. begin() const noexcept ->
  185. const_iterator
  186. {
  187. return &(*t_)[0];
  188. }
  189. auto
  190. object::
  191. cbegin() const noexcept ->
  192. const_iterator
  193. {
  194. return &(*t_)[0];
  195. }
  196. auto
  197. object::
  198. end() noexcept ->
  199. iterator
  200. {
  201. return &(*t_)[t_->size];
  202. }
  203. auto
  204. object::
  205. end() const noexcept ->
  206. const_iterator
  207. {
  208. return &(*t_)[t_->size];
  209. }
  210. auto
  211. object::
  212. cend() const noexcept ->
  213. const_iterator
  214. {
  215. return &(*t_)[t_->size];
  216. }
  217. auto
  218. object::
  219. rbegin() noexcept ->
  220. reverse_iterator
  221. {
  222. return reverse_iterator(end());
  223. }
  224. auto
  225. object::
  226. rbegin() const noexcept ->
  227. const_reverse_iterator
  228. {
  229. return const_reverse_iterator(end());
  230. }
  231. auto
  232. object::
  233. crbegin() const noexcept ->
  234. const_reverse_iterator
  235. {
  236. return const_reverse_iterator(end());
  237. }
  238. auto
  239. object::
  240. rend() noexcept ->
  241. reverse_iterator
  242. {
  243. return reverse_iterator(begin());
  244. }
  245. auto
  246. object::
  247. rend() const noexcept ->
  248. const_reverse_iterator
  249. {
  250. return const_reverse_iterator(begin());
  251. }
  252. auto
  253. object::
  254. crend() const noexcept ->
  255. const_reverse_iterator
  256. {
  257. return const_reverse_iterator(begin());
  258. }
  259. //----------------------------------------------------------
  260. //
  261. // Capacity
  262. //
  263. //----------------------------------------------------------
  264. bool
  265. object::
  266. empty() const noexcept
  267. {
  268. return t_->size == 0;
  269. }
  270. auto
  271. object::
  272. size() const noexcept ->
  273. std::size_t
  274. {
  275. return t_->size;
  276. }
  277. constexpr
  278. std::size_t
  279. object::
  280. max_size() noexcept
  281. {
  282. // max_size depends on the address model
  283. using min = std::integral_constant<std::size_t,
  284. (std::size_t(-1) - sizeof(table)) /
  285. (sizeof(key_value_pair) + sizeof(index_t))>;
  286. return min::value < BOOST_JSON_MAX_STRUCTURED_SIZE ?
  287. min::value : BOOST_JSON_MAX_STRUCTURED_SIZE;
  288. }
  289. auto
  290. object::
  291. capacity() const noexcept ->
  292. std::size_t
  293. {
  294. return t_->capacity;
  295. }
  296. void
  297. object::
  298. reserve(std::size_t new_capacity)
  299. {
  300. if( new_capacity <= capacity() )
  301. return;
  302. table* const old_table = reserve_impl(new_capacity);
  303. table::deallocate( old_table, sp_ );
  304. }
  305. //----------------------------------------------------------
  306. //
  307. // Lookup
  308. //
  309. //----------------------------------------------------------
  310. auto
  311. object::
  312. at(string_view key) & ->
  313. value&
  314. {
  315. auto const& self = *this;
  316. return const_cast< value& >( self.at(key) );
  317. }
  318. auto
  319. object::
  320. at(string_view key) && ->
  321. value&&
  322. {
  323. return std::move( at(key) );
  324. }
  325. auto
  326. object::
  327. at(string_view key) const& ->
  328. value const&
  329. {
  330. auto it = find(key);
  331. if(it == end())
  332. {
  333. BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
  334. detail::throw_system_error( error::out_of_range, &loc );
  335. }
  336. return it->value();
  337. }
  338. //----------------------------------------------------------
  339. template<class P, class>
  340. auto
  341. object::
  342. insert(P&& p) ->
  343. std::pair<iterator, bool>
  344. {
  345. key_value_pair v(
  346. std::forward<P>(p), sp_);
  347. return emplace_impl( v.key(), pilfer(v) );
  348. }
  349. template<class M>
  350. auto
  351. object::
  352. insert_or_assign(
  353. string_view key, M&& m) ->
  354. std::pair<iterator, bool>
  355. {
  356. std::pair<iterator, bool> result = emplace_impl(
  357. key, key, static_cast<M&&>(m) );
  358. if( !result.second )
  359. {
  360. value(static_cast<M>(m), sp_).swap(
  361. result.first->value());
  362. }
  363. return result;
  364. }
  365. template<class Arg>
  366. auto
  367. object::
  368. emplace(
  369. string_view key,
  370. Arg&& arg) ->
  371. std::pair<iterator, bool>
  372. {
  373. return emplace_impl( key, key, static_cast<Arg&&>(arg) );
  374. }
  375. //----------------------------------------------------------
  376. //
  377. // (private)
  378. //
  379. //----------------------------------------------------------
  380. template<class InputIt>
  381. void
  382. object::
  383. construct(
  384. InputIt first,
  385. InputIt last,
  386. std::size_t min_capacity,
  387. std::input_iterator_tag)
  388. {
  389. reserve(min_capacity);
  390. revert_construct r(*this);
  391. while(first != last)
  392. {
  393. insert(*first);
  394. ++first;
  395. }
  396. r.commit();
  397. }
  398. template<class InputIt>
  399. void
  400. object::
  401. construct(
  402. InputIt first,
  403. InputIt last,
  404. std::size_t min_capacity,
  405. std::forward_iterator_tag)
  406. {
  407. auto n = static_cast<
  408. std::size_t>(std::distance(
  409. first, last));
  410. if( n < min_capacity)
  411. n = min_capacity;
  412. reserve(n);
  413. revert_construct r(*this);
  414. while(first != last)
  415. {
  416. insert(*first);
  417. ++first;
  418. }
  419. r.commit();
  420. }
  421. template<class InputIt>
  422. void
  423. object::
  424. insert(
  425. InputIt first,
  426. InputIt last,
  427. std::input_iterator_tag)
  428. {
  429. // Since input iterators cannot be rewound,
  430. // we keep inserted elements on an exception.
  431. //
  432. while(first != last)
  433. {
  434. insert(*first);
  435. ++first;
  436. }
  437. }
  438. template<class InputIt>
  439. void
  440. object::
  441. insert(
  442. InputIt first,
  443. InputIt last,
  444. std::forward_iterator_tag)
  445. {
  446. auto const n =
  447. static_cast<std::size_t>(
  448. std::distance(first, last));
  449. auto const n0 = size();
  450. if(n > max_size() - n0)
  451. {
  452. BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
  453. detail::throw_system_error( error::object_too_large, &loc );
  454. }
  455. revert_insert r( *this, n0 + n );
  456. while(first != last)
  457. {
  458. insert(*first);
  459. ++first;
  460. }
  461. r.commit();
  462. }
  463. template< class... Args >
  464. std::pair<object::iterator, bool>
  465. object::
  466. emplace_impl( string_view key, Args&& ... args )
  467. {
  468. std::pair<iterator, std::size_t> search_result(nullptr, 0);
  469. if( !empty() )
  470. {
  471. search_result = detail::find_in_object(*this, key);
  472. if( search_result.first )
  473. return { search_result.first, false };
  474. }
  475. // we create the new value before reserving, in case it is a reference to
  476. // a subobject of the current object
  477. key_value_pair kv( static_cast<Args&&>(args)..., sp_ );
  478. // the key might get deallocated too
  479. key = kv.key();
  480. std::size_t const old_capacity = capacity();
  481. reserve(size() + 1);
  482. if( (empty() && capacity() > detail::small_object_size_)
  483. || (capacity() != old_capacity) )
  484. search_result.second = detail::digest(
  485. key.begin(), key.end(), t_->salt);
  486. BOOST_ASSERT(
  487. t_->is_small() ||
  488. (search_result.second ==
  489. detail::digest(key.begin(), key.end(), t_->salt)) );
  490. return { insert_impl(pilfer(kv), search_result.second), true };
  491. }
  492. //----------------------------------------------------------
  493. namespace detail {
  494. unchecked_object::
  495. ~unchecked_object()
  496. {
  497. if(! data_)
  498. return;
  499. if(sp_.is_not_shared_and_deallocate_is_trivial())
  500. return;
  501. value* p = data_;
  502. while(size_--)
  503. {
  504. p[0].~value();
  505. p[1].~value();
  506. p += 2;
  507. }
  508. }
  509. } // detail
  510. } // namespace json
  511. } // namespace boost
  512. #endif