object.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. //
  2. // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
  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. value&
  311. object::
  312. at(string_view key, source_location const& loc) &
  313. {
  314. auto const& self = *this;
  315. return const_cast< value& >( self.at(key, loc) );
  316. }
  317. value&&
  318. object::
  319. at(string_view key, source_location const& loc) &&
  320. {
  321. return std::move( at(key, loc) );
  322. }
  323. //----------------------------------------------------------
  324. template<class P, class>
  325. auto
  326. object::
  327. insert(P&& p) ->
  328. std::pair<iterator, bool>
  329. {
  330. key_value_pair v(
  331. std::forward<P>(p), sp_);
  332. return emplace_impl( v.key(), pilfer(v) );
  333. }
  334. template<class M>
  335. auto
  336. object::
  337. insert_or_assign(
  338. string_view key, M&& m) ->
  339. std::pair<iterator, bool>
  340. {
  341. std::pair<iterator, bool> result = emplace_impl(
  342. key, key, static_cast<M&&>(m) );
  343. if( !result.second )
  344. {
  345. value(static_cast<M>(m), sp_).swap(
  346. result.first->value());
  347. }
  348. return result;
  349. }
  350. template<class Arg>
  351. auto
  352. object::
  353. emplace(
  354. string_view key,
  355. Arg&& arg) ->
  356. std::pair<iterator, bool>
  357. {
  358. return emplace_impl( key, key, static_cast<Arg&&>(arg) );
  359. }
  360. //----------------------------------------------------------
  361. //
  362. // (private)
  363. //
  364. //----------------------------------------------------------
  365. template<class InputIt>
  366. void
  367. object::
  368. construct(
  369. InputIt first,
  370. InputIt last,
  371. std::size_t min_capacity,
  372. std::input_iterator_tag)
  373. {
  374. reserve(min_capacity);
  375. revert_construct r(*this);
  376. while(first != last)
  377. {
  378. insert(*first);
  379. ++first;
  380. }
  381. r.commit();
  382. }
  383. template<class InputIt>
  384. void
  385. object::
  386. construct(
  387. InputIt first,
  388. InputIt last,
  389. std::size_t min_capacity,
  390. std::forward_iterator_tag)
  391. {
  392. auto n = static_cast<
  393. std::size_t>(std::distance(
  394. first, last));
  395. if( n < min_capacity)
  396. n = min_capacity;
  397. reserve(n);
  398. revert_construct r(*this);
  399. while(first != last)
  400. {
  401. insert(*first);
  402. ++first;
  403. }
  404. r.commit();
  405. }
  406. template<class InputIt>
  407. void
  408. object::
  409. insert(
  410. InputIt first,
  411. InputIt last,
  412. std::input_iterator_tag)
  413. {
  414. // Since input iterators cannot be rewound,
  415. // we keep inserted elements on an exception.
  416. //
  417. while(first != last)
  418. {
  419. insert(*first);
  420. ++first;
  421. }
  422. }
  423. template<class InputIt>
  424. void
  425. object::
  426. insert(
  427. InputIt first,
  428. InputIt last,
  429. std::forward_iterator_tag)
  430. {
  431. auto const n =
  432. static_cast<std::size_t>(
  433. std::distance(first, last));
  434. auto const n0 = size();
  435. if(n > max_size() - n0)
  436. {
  437. BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
  438. detail::throw_system_error( error::object_too_large, &loc );
  439. }
  440. revert_insert r( *this, n0 + n );
  441. while(first != last)
  442. {
  443. insert(*first);
  444. ++first;
  445. }
  446. r.commit();
  447. }
  448. template< class... Args >
  449. std::pair<object::iterator, bool>
  450. object::
  451. emplace_impl( string_view key, Args&& ... args )
  452. {
  453. std::pair<iterator, std::size_t> search_result(nullptr, 0);
  454. if( !empty() )
  455. {
  456. search_result = detail::find_in_object(*this, key);
  457. if( search_result.first )
  458. return { search_result.first, false };
  459. }
  460. // we create the new value before reserving, in case it is a reference to
  461. // a subobject of the current object
  462. key_value_pair kv( static_cast<Args&&>(args)..., sp_ );
  463. // the key might get deallocated too
  464. key = kv.key();
  465. std::size_t const old_capacity = capacity();
  466. reserve(size() + 1);
  467. if( (empty() && capacity() > detail::small_object_size_)
  468. || (capacity() != old_capacity) )
  469. search_result.second = detail::digest(
  470. key.begin(), key.end(), t_->salt);
  471. BOOST_ASSERT(
  472. t_->is_small() ||
  473. (search_result.second ==
  474. detail::digest(key.begin(), key.end(), t_->salt)) );
  475. return { insert_impl(pilfer(kv), search_result.second), true };
  476. }
  477. //----------------------------------------------------------
  478. namespace detail {
  479. unchecked_object::
  480. ~unchecked_object()
  481. {
  482. if(! data_)
  483. return;
  484. if(sp_.is_not_shared_and_deallocate_is_trivial())
  485. return;
  486. value* p = data_;
  487. while(size_--)
  488. {
  489. p[0].~value();
  490. p[1].~value();
  491. p += 2;
  492. }
  493. }
  494. } // detail
  495. } // namespace json
  496. } // namespace boost
  497. #endif