value.hpp 109 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324
  1. //
  2. // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
  3. // Copyright (c) 2020 Krystian Stasiowski (sdkrystian@gmail.com)
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // Official repository: https://github.com/boostorg/json
  9. //
  10. #ifndef BOOST_JSON_VALUE_HPP
  11. #define BOOST_JSON_VALUE_HPP
  12. #include <boost/json/detail/config.hpp>
  13. #include <boost/json/array.hpp>
  14. #include <boost/json/kind.hpp>
  15. #include <boost/json/object.hpp>
  16. #include <boost/json/pilfer.hpp>
  17. #include <boost/json/set_pointer_options.hpp>
  18. #include <boost/json/storage_ptr.hpp>
  19. #include <boost/json/string.hpp>
  20. #include <boost/json/string_view.hpp>
  21. #include <boost/json/value_ref.hpp>
  22. #include <boost/json/detail/except.hpp>
  23. #include <boost/json/detail/value.hpp>
  24. #include <cstdlib>
  25. #include <cstring>
  26. #include <initializer_list>
  27. #include <iosfwd>
  28. #include <limits>
  29. #include <new>
  30. #include <type_traits>
  31. #include <utility>
  32. namespace boost {
  33. namespace json {
  34. //----------------------------------------------------------
  35. /** The type used to represent any JSON value
  36. This is a
  37. <a href="https://en.cppreference.com/w/cpp/concepts/regular"><em>Regular</em></a>
  38. type which works like
  39. a variant of the basic JSON data types: array,
  40. object, string, number, boolean, and null.
  41. @par Thread Safety
  42. Distinct instances may be accessed concurrently.
  43. Non-const member functions of a shared instance
  44. may not be called concurrently with any other
  45. member functions of that instance.
  46. */
  47. class value
  48. {
  49. #ifndef BOOST_JSON_DOCS
  50. using scalar = detail::scalar;
  51. union
  52. {
  53. storage_ptr sp_; // must come first
  54. array arr_;
  55. object obj_;
  56. string str_;
  57. scalar sca_;
  58. };
  59. #endif
  60. struct init_iter;
  61. #ifndef BOOST_JSON_DOCS
  62. // VFALCO doc toolchain incorrectly treats this as public
  63. friend struct detail::access;
  64. #endif
  65. explicit
  66. value(
  67. detail::unchecked_array&& ua)
  68. : arr_(std::move(ua))
  69. {
  70. }
  71. explicit
  72. value(
  73. detail::unchecked_object&& uo)
  74. : obj_(std::move(uo))
  75. {
  76. }
  77. value(
  78. detail::key_t const&,
  79. string_view s,
  80. storage_ptr sp)
  81. : str_(detail::key_t{}, s, std::move(sp))
  82. {
  83. }
  84. value(
  85. detail::key_t const&,
  86. string_view s1,
  87. string_view s2,
  88. storage_ptr sp)
  89. : str_(detail::key_t{}, s1, s2, std::move(sp))
  90. {
  91. }
  92. inline bool is_scalar() const noexcept
  93. {
  94. return sca_.k < json::kind::string;
  95. }
  96. public:
  97. /** The type of _Allocator_ returned by @ref get_allocator
  98. This type is a @ref polymorphic_allocator.
  99. */
  100. #ifdef BOOST_JSON_DOCS
  101. // VFALCO doc toolchain renders this incorrectly
  102. using allocator_type = __see_below__;
  103. #else
  104. using allocator_type = polymorphic_allocator<value>;
  105. #endif
  106. /** Destructor.
  107. The value and all of its contents are destroyed.
  108. Any dynamically allocated memory that was allocated
  109. internally is freed.
  110. @par Complexity
  111. Constant, or linear in size for array or object.
  112. @par Exception Safety
  113. No-throw guarantee.
  114. */
  115. BOOST_JSON_DECL
  116. ~value() noexcept;
  117. /** Default constructor.
  118. The constructed value is null,
  119. using the [default memory resource].
  120. @par Complexity
  121. Constant.
  122. @par Exception Safety
  123. No-throw guarantee.
  124. [default memory resource]: json/allocators/storage_ptr.html#json.allocators.storage_ptr.default_memory_resource
  125. */
  126. value() noexcept
  127. : sca_()
  128. {
  129. }
  130. /** Constructor.
  131. The constructed value is null,
  132. using the specified @ref memory_resource.
  133. @par Complexity
  134. Constant.
  135. @par Exception Safety
  136. No-throw guarantee.
  137. @param sp A pointer to the @ref memory_resource
  138. to use. The container will acquire shared
  139. ownership of the memory resource.
  140. */
  141. explicit
  142. value(storage_ptr sp) noexcept
  143. : sca_(std::move(sp))
  144. {
  145. }
  146. /** Pilfer constructor.
  147. The value is constructed by acquiring ownership
  148. of the contents of `other` using pilfer semantics.
  149. This is more efficient than move construction, when
  150. it is known that the moved-from object will be
  151. immediately destroyed afterwards.
  152. @par Complexity
  153. Constant.
  154. @par Exception Safety
  155. No-throw guarantee.
  156. @param other The value to pilfer. After pilfer
  157. construction, `other` is not in a usable state
  158. and may only be destroyed.
  159. @see @ref pilfer,
  160. <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0308r0.html">
  161. Valueless Variants Considered Harmful</a>
  162. */
  163. value(pilfered<value> other) noexcept
  164. {
  165. relocate(this, other.get());
  166. ::new(&other.get().sca_) scalar();
  167. }
  168. /** Copy constructor.
  169. The value is constructed with a copy of the
  170. contents of `other`, using the same
  171. memory resource as `other`.
  172. @par Complexity
  173. Linear in the size of `other`.
  174. @par Exception Safety
  175. Strong guarantee.
  176. Calls to `memory_resource::allocate` may throw.
  177. @param other The value to copy.
  178. */
  179. value(value const& other)
  180. : value(other, other.storage())
  181. {
  182. }
  183. /** Copy constructor
  184. The value is constructed with a copy of the
  185. contents of `other`, using the
  186. specified memory resource.
  187. @par Complexity
  188. Linear in the size of `other`.
  189. @par Exception Safety
  190. Strong guarantee.
  191. Calls to `memory_resource::allocate` may throw.
  192. @param other The value to copy.
  193. @param sp A pointer to the @ref memory_resource
  194. to use. The container will acquire shared
  195. ownership of the memory resource.
  196. */
  197. BOOST_JSON_DECL
  198. value(
  199. value const& other,
  200. storage_ptr sp);
  201. /** Move constructor
  202. The value is constructed by acquiring ownership of
  203. the contents of `other` and shared ownership of
  204. `other`'s memory resource.
  205. @note
  206. After construction, the moved-from value becomes a
  207. null value with its current storage pointer.
  208. @par Complexity
  209. Constant.
  210. @par Exception Safety
  211. No-throw guarantee.
  212. @param other The value to move.
  213. */
  214. BOOST_JSON_DECL
  215. value(value&& other) noexcept;
  216. /** Move constructor
  217. The value is constructed with the contents of
  218. `other` by move semantics, using the specified
  219. memory resource:
  220. @li If `*other.storage() == *sp`, ownership of
  221. the underlying memory is transferred in constant
  222. time, with no possibility of exceptions.
  223. After construction, the moved-from value becomes
  224. a null value with its current storage pointer.
  225. @li If `*other.storage() != *sp`, an
  226. element-wise copy is performed if
  227. `other.is_structured() == true`, which may throw.
  228. In this case, the moved-from value is not
  229. changed.
  230. @par Complexity
  231. Constant or linear in the size of `other`.
  232. @par Exception Safety
  233. Strong guarantee.
  234. Calls to `memory_resource::allocate` may throw.
  235. @param other The value to move.
  236. @param sp A pointer to the @ref memory_resource
  237. to use. The container will acquire shared
  238. ownership of the memory resource.
  239. */
  240. BOOST_JSON_DECL
  241. value(
  242. value&& other,
  243. storage_ptr sp);
  244. //------------------------------------------------------
  245. //
  246. // Conversion
  247. //
  248. //------------------------------------------------------
  249. /** Construct a null.
  250. A null value is a monostate.
  251. @par Complexity
  252. Constant.
  253. @par Exception Safety
  254. No-throw guarantee.
  255. @param sp A pointer to the @ref memory_resource
  256. to use. The container will acquire shared
  257. ownership of the memory resource.
  258. */
  259. value(
  260. std::nullptr_t,
  261. storage_ptr sp = {}) noexcept
  262. : sca_(std::move(sp))
  263. {
  264. }
  265. /** Construct a bool.
  266. This constructs a `bool` value using
  267. the specified memory resource.
  268. @par Complexity
  269. Constant.
  270. @par Exception Safety
  271. No-throw guarantee.
  272. @param b The initial value.
  273. @param sp A pointer to the @ref memory_resource
  274. to use. The container will acquire shared
  275. ownership of the memory resource.
  276. */
  277. #ifdef BOOST_JSON_DOCS
  278. value(
  279. bool b,
  280. storage_ptr sp = {}) noexcept;
  281. #else
  282. template<class T
  283. ,class = typename std::enable_if<
  284. std::is_same<T, bool>::value>::type
  285. >
  286. value(
  287. T b,
  288. storage_ptr sp = {}) noexcept
  289. : sca_(b, std::move(sp))
  290. {
  291. }
  292. #endif
  293. /** Construct a `std::int64_t`.
  294. @par Complexity
  295. Constant.
  296. @par Exception Safety
  297. No-throw guarantee.
  298. @param i The initial value.
  299. @param sp A pointer to the @ref memory_resource
  300. to use. The container will acquire shared
  301. ownership of the memory resource.
  302. */
  303. value(
  304. signed char i,
  305. storage_ptr sp = {}) noexcept
  306. : sca_(static_cast<std::int64_t>(
  307. i), std::move(sp))
  308. {
  309. }
  310. /** Construct a `std::int64_t`.
  311. @par Complexity
  312. Constant.
  313. @par Exception Safety
  314. No-throw guarantee.
  315. @param i The initial value.
  316. @param sp A pointer to the @ref memory_resource
  317. to use. The container will acquire shared
  318. ownership of the memory resource.
  319. */
  320. value(
  321. short i,
  322. storage_ptr sp = {}) noexcept
  323. : sca_(static_cast<std::int64_t>(
  324. i), std::move(sp))
  325. {
  326. }
  327. /** Construct a `std::int64_t`.
  328. @par Complexity
  329. Constant.
  330. @par Exception Safety
  331. No-throw guarantee.
  332. @param i The initial value.
  333. @param sp A pointer to the @ref memory_resource
  334. to use. The container will acquire shared
  335. ownership of the memory resource.
  336. */
  337. value(
  338. int i,
  339. storage_ptr sp = {}) noexcept
  340. : sca_(static_cast<std::int64_t>(i),
  341. std::move(sp))
  342. {
  343. }
  344. /** Construct a `std::int64_t`.
  345. @par Complexity
  346. Constant.
  347. @par Exception Safety
  348. No-throw guarantee.
  349. @param i The initial value.
  350. @param sp A pointer to the @ref memory_resource
  351. to use. The container will acquire shared
  352. ownership of the memory resource.
  353. */
  354. value(
  355. long i,
  356. storage_ptr sp = {}) noexcept
  357. : sca_(static_cast<std::int64_t>(i),
  358. std::move(sp))
  359. {
  360. }
  361. /** Construct a `std::int64_t`.
  362. @par Complexity
  363. Constant.
  364. @par Exception Safety
  365. No-throw guarantee.
  366. @param i The initial value.
  367. @param sp A pointer to the @ref memory_resource
  368. to use. The container will acquire shared
  369. ownership of the memory resource.
  370. */
  371. value(
  372. long long i,
  373. storage_ptr sp = {}) noexcept
  374. : sca_(static_cast<std::int64_t>(i),
  375. std::move(sp))
  376. {
  377. }
  378. /** Construct a `std::uint64_t`.
  379. @par Complexity
  380. Constant.
  381. @par Exception Safety
  382. No-throw guarantee.
  383. @param u The initial value.
  384. @param sp A pointer to the @ref memory_resource
  385. to use. The container will acquire shared
  386. ownership of the memory resource.
  387. */
  388. value(
  389. unsigned char u,
  390. storage_ptr sp = {}) noexcept
  391. : sca_(static_cast<std::uint64_t>(
  392. u), std::move(sp))
  393. {
  394. }
  395. /** Construct a `std::uint64_t`.
  396. @par Complexity
  397. Constant.
  398. @par Exception Safety
  399. No-throw guarantee.
  400. @param u The initial value.
  401. @param sp A pointer to the @ref memory_resource
  402. to use. The container will acquire shared
  403. ownership of the memory resource.
  404. */
  405. value(
  406. unsigned short u,
  407. storage_ptr sp = {}) noexcept
  408. : sca_(static_cast<std::uint64_t>(u),
  409. std::move(sp))
  410. {
  411. }
  412. /** Construct a `std::uint64_t`.
  413. @par Complexity
  414. Constant.
  415. @par Exception Safety
  416. No-throw guarantee.
  417. @param u The initial value.
  418. @param sp A pointer to the @ref memory_resource
  419. to use. The container will acquire shared
  420. ownership of the memory resource.
  421. */
  422. value(
  423. unsigned int u,
  424. storage_ptr sp = {}) noexcept
  425. : sca_(static_cast<std::uint64_t>(u),
  426. std::move(sp))
  427. {
  428. }
  429. /** Construct a `std::uint64_t`.
  430. @par Complexity
  431. Constant.
  432. @par Exception Safety
  433. No-throw guarantee.
  434. @param u The initial value.
  435. @param sp A pointer to the @ref memory_resource
  436. to use. The container will acquire shared
  437. ownership of the memory resource.
  438. */
  439. value(
  440. unsigned long u,
  441. storage_ptr sp = {}) noexcept
  442. : sca_(static_cast<std::uint64_t>(u),
  443. std::move(sp))
  444. {
  445. }
  446. /** Construct a `std::uint64_t`.
  447. @par Complexity
  448. Constant.
  449. @par Exception Safety
  450. No-throw guarantee.
  451. @param u The initial value.
  452. @param sp A pointer to the @ref memory_resource
  453. to use. The container will acquire shared
  454. ownership of the memory resource.
  455. */
  456. value(
  457. unsigned long long u,
  458. storage_ptr sp = {}) noexcept
  459. : sca_(static_cast<std::uint64_t>(u),
  460. std::move(sp))
  461. {
  462. }
  463. /** Construct a `double`.
  464. @par Complexity
  465. Constant.
  466. @par Exception Safety
  467. No-throw guarantee.
  468. @param d The initial value.
  469. @param sp A pointer to the @ref memory_resource
  470. to use. The container will acquire shared
  471. ownership of the memory resource.
  472. */
  473. value(
  474. double d,
  475. storage_ptr sp = {}) noexcept
  476. : sca_(d, std::move(sp))
  477. {
  478. }
  479. /** Construct a @ref string.
  480. The string is constructed with a copy of the
  481. string view `s`, using the specified memory resource.
  482. @par Complexity
  483. Linear in `s.size()`.
  484. @par Exception Safety
  485. Strong guarantee.
  486. Calls to `memory_resource::allocate` may throw.
  487. @param s The string view to construct with.
  488. @param sp A pointer to the @ref memory_resource
  489. to use. The container will acquire shared
  490. ownership of the memory resource.
  491. */
  492. value(
  493. string_view s,
  494. storage_ptr sp = {})
  495. : str_(s, std::move(sp))
  496. {
  497. }
  498. /** Construct a @ref string.
  499. The string is constructed with a copy of the
  500. null-terminated string `s`, using the specified
  501. memory resource.
  502. @par Complexity
  503. Linear in `std::strlen(s)`.
  504. @par Exception Safety
  505. Strong guarantee.
  506. Calls to `memory_resource::allocate` may throw.
  507. @param s The null-terminated string to construct
  508. with.
  509. @param sp A pointer to the @ref memory_resource
  510. to use. The container will acquire shared
  511. ownership of the memory resource.
  512. */
  513. value(
  514. char const* s,
  515. storage_ptr sp = {})
  516. : str_(s, std::move(sp))
  517. {
  518. }
  519. /** Construct a @ref string.
  520. The value is constructed from `other`, using the
  521. same memory resource. To transfer ownership, use `std::move`:
  522. @par Example
  523. @code
  524. string str = "The Boost C++ Library Collection";
  525. // transfer ownership
  526. value jv( std::move(str) );
  527. assert( str.empty() );
  528. assert( *str.storage() == *jv.storage() );
  529. @endcode
  530. @par Complexity
  531. Constant.
  532. @par Exception Safety
  533. No-throw guarantee.
  534. @param other The string to construct with.
  535. */
  536. value(
  537. string other) noexcept
  538. : str_(std::move(other))
  539. {
  540. }
  541. /** Construct a @ref string.
  542. The value is copy constructed from `other`,
  543. using the specified memory resource.
  544. @par Complexity
  545. Linear in `other.size()`.
  546. @par Exception Safety
  547. Strong guarantee.
  548. Calls to `memory_resource::allocate` may throw.
  549. @param other The string to construct with.
  550. @param sp A pointer to the @ref memory_resource
  551. to use. The container will acquire shared
  552. ownership of the memory resource.
  553. */
  554. value(
  555. string const& other,
  556. storage_ptr sp)
  557. : str_(
  558. other,
  559. std::move(sp))
  560. {
  561. }
  562. /** Construct a @ref string.
  563. The value is move constructed from `other`,
  564. using the specified memory resource.
  565. @par Complexity
  566. Constant or linear in `other.size()`.
  567. @par Exception Safety
  568. Strong guarantee.
  569. Calls to `memory_resource::allocate` may throw.
  570. @param other The string to construct with.
  571. @param sp A pointer to the @ref memory_resource
  572. to use. The container will acquire shared
  573. ownership of the memory resource.
  574. */
  575. value(
  576. string&& other,
  577. storage_ptr sp)
  578. : str_(
  579. std::move(other),
  580. std::move(sp))
  581. {
  582. }
  583. /** Construct a @ref string.
  584. This is the fastest way to construct
  585. an empty string, using the specified
  586. memory resource. The variable @ref string_kind
  587. may be passed as the first parameter
  588. to select this overload:
  589. @par Example
  590. @code
  591. // Construct an empty string
  592. value jv( string_kind );
  593. @endcode
  594. @par Complexity
  595. Constant.
  596. @par Exception Safety
  597. No-throw guarantee.
  598. @param sp A pointer to the @ref memory_resource
  599. to use. The container will acquire shared
  600. ownership of the memory resource.
  601. @see @ref string_kind
  602. */
  603. value(
  604. string_kind_t,
  605. storage_ptr sp = {}) noexcept
  606. : str_(std::move(sp))
  607. {
  608. }
  609. /** Construct an @ref array.
  610. The value is constructed from `other`, using the
  611. same memory resource. To transfer ownership, use `std::move`:
  612. @par Example
  613. @code
  614. array arr( {1, 2, 3, 4, 5} );
  615. // transfer ownership
  616. value jv( std::move(arr) );
  617. assert( arr.empty() );
  618. assert( *arr.storage() == *jv.storage() );
  619. @endcode
  620. @par Complexity
  621. Constant.
  622. @par Exception Safety
  623. No-throw guarantee.
  624. @param other The array to construct with.
  625. */
  626. value(array other) noexcept
  627. : arr_(std::move(other))
  628. {
  629. }
  630. /** Construct an @ref array.
  631. The value is copy constructed from `other`,
  632. using the specified memory resource.
  633. @par Complexity
  634. Linear in `other.size()`.
  635. @par Exception Safety
  636. Strong guarantee.
  637. Calls to `memory_resource::allocate` may throw.
  638. @param other The array to construct with.
  639. @param sp A pointer to the @ref memory_resource
  640. to use. The container will acquire shared
  641. ownership of the memory resource.
  642. */
  643. value(
  644. array const& other,
  645. storage_ptr sp)
  646. : arr_(
  647. other,
  648. std::move(sp))
  649. {
  650. }
  651. /** Construct an @ref array.
  652. The value is move-constructed from `other`,
  653. using the specified memory resource.
  654. @par Complexity
  655. Constant or linear in `other.size()`.
  656. @par Exception Safety
  657. Strong guarantee.
  658. Calls to `memory_resource::allocate` may throw.
  659. @param other The array to construct with.
  660. @param sp A pointer to the @ref memory_resource
  661. to use. The container will acquire shared
  662. ownership of the memory resource.
  663. */
  664. value(
  665. array&& other,
  666. storage_ptr sp)
  667. : arr_(
  668. std::move(other),
  669. std::move(sp))
  670. {
  671. }
  672. /** Construct an @ref array.
  673. This is the fastest way to construct
  674. an empty array, using the specified
  675. memory resource. The variable @ref array_kind
  676. may be passed as the first parameter
  677. to select this overload:
  678. @par Example
  679. @code
  680. // Construct an empty array
  681. value jv( array_kind );
  682. @endcode
  683. @par Complexity
  684. Constant.
  685. @par Exception Safety
  686. No-throw guarantee.
  687. @param sp A pointer to the @ref memory_resource
  688. to use. The container will acquire shared
  689. ownership of the memory resource.
  690. @see @ref array_kind
  691. */
  692. value(
  693. array_kind_t,
  694. storage_ptr sp = {}) noexcept
  695. : arr_(std::move(sp))
  696. {
  697. }
  698. /** Construct an @ref object.
  699. The value is constructed from `other`, using the
  700. same memory resource. To transfer ownership, use `std::move`:
  701. @par Example
  702. @code
  703. object obj( {{"a",1}, {"b",2}, {"c"},3}} );
  704. // transfer ownership
  705. value jv( std::move(obj) );
  706. assert( obj.empty() );
  707. assert( *obj.storage() == *jv.storage() );
  708. @endcode
  709. @par Complexity
  710. Constant.
  711. @par Exception Safety
  712. No-throw guarantee.
  713. @param other The object to construct with.
  714. */
  715. value(object other) noexcept
  716. : obj_(std::move(other))
  717. {
  718. }
  719. /** Construct an @ref object.
  720. The value is copy constructed from `other`,
  721. using the specified memory resource.
  722. @par Complexity
  723. Linear in `other.size()`.
  724. @par Exception Safety
  725. Strong guarantee.
  726. Calls to `memory_resource::allocate` may throw.
  727. @param other The object to construct with.
  728. @param sp A pointer to the @ref memory_resource
  729. to use. The container will acquire shared
  730. ownership of the memory resource.
  731. */
  732. value(
  733. object const& other,
  734. storage_ptr sp)
  735. : obj_(
  736. other,
  737. std::move(sp))
  738. {
  739. }
  740. /** Construct an @ref object.
  741. The value is move constructed from `other`,
  742. using the specified memory resource.
  743. @par Complexity
  744. Constant or linear in `other.size()`.
  745. @par Exception Safety
  746. Strong guarantee.
  747. Calls to `memory_resource::allocate` may throw.
  748. @param other The object to construct with.
  749. @param sp A pointer to the @ref memory_resource
  750. to use. The container will acquire shared
  751. ownership of the memory resource.
  752. */
  753. value(
  754. object&& other,
  755. storage_ptr sp)
  756. : obj_(
  757. std::move(other),
  758. std::move(sp))
  759. {
  760. }
  761. /** Construct an @ref object.
  762. This is the fastest way to construct
  763. an empty object, using the specified
  764. memory resource. The variable @ref object_kind
  765. may be passed as the first parameter
  766. to select this overload:
  767. @par Example
  768. @code
  769. // Construct an empty object
  770. value jv( object_kind );
  771. @endcode
  772. @par Complexity
  773. Constant.
  774. @par Exception Safety
  775. No-throw guarantee.
  776. @param sp A pointer to the @ref memory_resource
  777. to use. The container will acquire shared
  778. ownership of the memory resource.
  779. @see @ref object_kind
  780. */
  781. value(
  782. object_kind_t,
  783. storage_ptr sp = {}) noexcept
  784. : obj_(std::move(sp))
  785. {
  786. }
  787. /** Construct from an initializer-list
  788. @li If the initializer list consists of key/value
  789. pairs, an @ref object is created; otherwise,
  790. @li if the size of the initializer list is exactly 1, the object is
  791. constructed directly from that sole element; otherwise,
  792. @li an @ref array is created.
  793. The contents of the initializer list are copied to the newly
  794. constructed value using the specified memory resource.
  795. @par Complexity
  796. Linear in `init.size()`.
  797. @par Exception Safety
  798. Strong guarantee.
  799. Calls to `memory_resource::allocate` may throw.
  800. @param init The initializer list to construct from.
  801. @param sp A pointer to the @ref memory_resource
  802. to use. The container will acquire shared
  803. ownership of the memory resource.
  804. @par Note
  805. The previous behavior of this constructor was to always
  806. construct either an @ref object or an @ref array. In practice though,
  807. several C++ implementations did not treat `value{x}` as a constructor
  808. from initializer list. This effectively resulted in different behavior
  809. on different implementations. <br>
  810. If you need the legacy behavior define macro
  811. `BOOST_JSON_LEGACY_INIT_LIST_BEHAVIOR` when you are building the
  812. library. The macro and the functionality will be deprecated in the
  813. future and then removed, so we urge you to change your code for the new
  814. behavior as soon as possible. The simplest way to create an @ref array
  815. with 1 element using an initializer list is via `array{x}`.
  816. */
  817. BOOST_JSON_DECL
  818. value(
  819. std::initializer_list<value_ref> init,
  820. storage_ptr sp = {});
  821. //------------------------------------------------------
  822. //
  823. // Assignment
  824. //
  825. //------------------------------------------------------
  826. /** Copy assignment.
  827. The contents of the value are replaced with an
  828. element-wise copy of the contents of `other`.
  829. @par Complexity
  830. Linear in the size of `*this` plus `other`.
  831. @par Exception Safety
  832. Strong guarantee.
  833. Calls to `memory_resource::allocate` may throw.
  834. @param other The value to copy.
  835. */
  836. BOOST_JSON_DECL
  837. value&
  838. operator=(value const& other);
  839. /** Move assignment.
  840. The contents of the value are replaced with the
  841. contents of `other` using move semantics:
  842. @li If `*other.storage() == *sp`, ownership of
  843. the underlying memory is transferred in constant
  844. time, with no possibility of exceptions.
  845. After assignment, the moved-from value becomes
  846. a null with its current storage pointer.
  847. @li If `*other.storage() != *sp`, an
  848. element-wise copy is performed if
  849. `other.is_structured() == true`, which may throw.
  850. In this case, the moved-from value is not
  851. changed.
  852. @par Complexity
  853. Constant, or linear in
  854. `this->size()` plus `other.size()`.
  855. @par Exception Safety
  856. Strong guarantee.
  857. Calls to `memory_resource::allocate` may throw.
  858. @param other The value to assign from.
  859. */
  860. BOOST_JSON_DECL
  861. value&
  862. operator=(value&& other);
  863. /** Assignment.
  864. Replace `*this` with the value formed by
  865. constructing from `init` and `this->storage()`.
  866. If the initializer list consists of key/value
  867. pairs, the resulting @ref object is assigned.
  868. Otherwise an @ref array is assigned. The contents
  869. of the initializer list are moved to `*this`
  870. using the existing memory resource.
  871. @par Complexity
  872. Linear in `init.size()`.
  873. @par Exception Safety
  874. Strong guarantee.
  875. Calls to `memory_resource::allocate` may throw.
  876. @param init The initializer list to assign from.
  877. */
  878. BOOST_JSON_DECL
  879. value&
  880. operator=(
  881. std::initializer_list<value_ref> init);
  882. /** Assignment.
  883. Replace `*this` with null.
  884. @par Exception Safety
  885. No-throw guarantee.
  886. @par Complexity
  887. Linear in the size of `*this`.
  888. */
  889. value&
  890. operator=(std::nullptr_t) noexcept
  891. {
  892. if(is_scalar())
  893. {
  894. sca_.k = json::kind::null;
  895. }
  896. else
  897. {
  898. ::new(&sca_) scalar(
  899. destroy());
  900. }
  901. return *this;
  902. }
  903. /** Assignment.
  904. Replace `*this` with `b`.
  905. @par Exception Safety
  906. No-throw guarantee.
  907. @par Complexity
  908. Linear in the size of `*this`.
  909. @param b The new value.
  910. */
  911. #ifdef BOOST_JSON_DOCS
  912. value& operator=(bool b) noexcept;
  913. #else
  914. template<class T
  915. ,class = typename std::enable_if<
  916. std::is_same<T, bool>::value>::type
  917. >
  918. value& operator=(T b) noexcept
  919. {
  920. if(is_scalar())
  921. {
  922. sca_.b = b;
  923. sca_.k = json::kind::bool_;
  924. }
  925. else
  926. {
  927. ::new(&sca_) scalar(
  928. b, destroy());
  929. }
  930. return *this;
  931. }
  932. #endif
  933. /** Assignment.
  934. Replace `*this` with `i`.
  935. @par Exception Safety
  936. No-throw guarantee.
  937. @par Complexity
  938. Linear in the size of `*this`.
  939. @param i The new value.
  940. */
  941. /** @{ */
  942. value& operator=(signed char i) noexcept
  943. {
  944. return operator=(
  945. static_cast<long long>(i));
  946. }
  947. value& operator=(short i) noexcept
  948. {
  949. return operator=(
  950. static_cast<long long>(i));
  951. }
  952. value& operator=(int i) noexcept
  953. {
  954. return operator=(
  955. static_cast<long long>(i));
  956. }
  957. value& operator=(long i) noexcept
  958. {
  959. return operator=(
  960. static_cast<long long>(i));
  961. }
  962. value& operator=(long long i) noexcept
  963. {
  964. if(is_scalar())
  965. {
  966. sca_.i = i;
  967. sca_.k = json::kind::int64;
  968. }
  969. else
  970. {
  971. ::new(&sca_) scalar(static_cast<
  972. std::int64_t>(i), destroy());
  973. }
  974. return *this;
  975. }
  976. /** @} */
  977. /** Assignment.
  978. Replace `*this` with `i`.
  979. @par Exception Safety
  980. No-throw guarantee.
  981. @par Complexity
  982. Linear in the size of `*this`.
  983. @param u The new value.
  984. */
  985. /** @{ */
  986. value& operator=(unsigned char u) noexcept
  987. {
  988. return operator=(static_cast<
  989. unsigned long long>(u));
  990. }
  991. value& operator=(unsigned short u) noexcept
  992. {
  993. return operator=(static_cast<
  994. unsigned long long>(u));
  995. }
  996. value& operator=(unsigned int u) noexcept
  997. {
  998. return operator=(static_cast<
  999. unsigned long long>(u));
  1000. }
  1001. value& operator=(unsigned long u) noexcept
  1002. {
  1003. return operator=(static_cast<
  1004. unsigned long long>(u));
  1005. }
  1006. value& operator=(unsigned long long u) noexcept
  1007. {
  1008. if(is_scalar())
  1009. {
  1010. sca_.u = u;
  1011. sca_.k = json::kind::uint64;
  1012. }
  1013. else
  1014. {
  1015. ::new(&sca_) scalar(static_cast<
  1016. std::uint64_t>(u), destroy());
  1017. }
  1018. return *this;
  1019. }
  1020. /** @} */
  1021. /** Assignment.
  1022. Replace `*this` with `d`.
  1023. @par Exception Safety
  1024. No-throw guarantee.
  1025. @par Complexity
  1026. Linear in the size of `*this`.
  1027. @param d The new value.
  1028. */
  1029. value& operator=(double d) noexcept
  1030. {
  1031. if(is_scalar())
  1032. {
  1033. sca_.d = d;
  1034. sca_.k = json::kind::double_;
  1035. }
  1036. else
  1037. {
  1038. ::new(&sca_) scalar(
  1039. d, destroy());
  1040. }
  1041. return *this;
  1042. }
  1043. /** Assignment.
  1044. Replace `*this` with a copy of the string `s`.
  1045. @par Exception Safety
  1046. Strong guarantee.
  1047. Calls to `memory_resource::allocate` may throw.
  1048. @par Complexity
  1049. Linear in the sum of sizes of `*this` and `s`
  1050. @param s The new string.
  1051. */
  1052. /** @{ */
  1053. BOOST_JSON_DECL value& operator=(string_view s);
  1054. BOOST_JSON_DECL value& operator=(char const* s);
  1055. BOOST_JSON_DECL value& operator=(string const& s);
  1056. /** @} */
  1057. /** Assignment.
  1058. The contents of the value are replaced with the
  1059. contents of `s` using move semantics:
  1060. @li If `*other.storage() == *this->storage()`,
  1061. ownership of the underlying memory is transferred
  1062. in constant time, with no possibility of exceptions.
  1063. After assignment, the moved-from string becomes
  1064. empty with its current storage pointer.
  1065. @li If `*other.storage() != *this->storage()`, an
  1066. element-wise copy is performed, which may throw.
  1067. In this case, the moved-from string is not
  1068. changed.
  1069. @par Complexity
  1070. Constant, or linear in the size of `*this` plus `s.size()`.
  1071. @par Exception Safety
  1072. Strong guarantee.
  1073. Calls to `memory_resource::allocate` may throw.
  1074. @param s The string to move-assign from.
  1075. */
  1076. BOOST_JSON_DECL value& operator=(string&& s);
  1077. /** Assignment.
  1078. Replace `*this` with a copy of the array `arr`.
  1079. @par Exception Safety
  1080. Strong guarantee.
  1081. Calls to `memory_resource::allocate` may throw.
  1082. @par Complexity
  1083. Linear in the sum of sizes of `*this` and `arr`
  1084. @param arr The new array.
  1085. */
  1086. BOOST_JSON_DECL value& operator=(array const& arr);
  1087. /** Assignment.
  1088. The contents of the value are replaced with the
  1089. contents of `arr` using move semantics:
  1090. @li If `*arr.storage() == *this->storage()`,
  1091. ownership of the underlying memory is transferred
  1092. in constant time, with no possibility of exceptions.
  1093. After assignment, the moved-from array becomes
  1094. empty with its current storage pointer.
  1095. @li If `*arr.storage() != *this->storage()`, an
  1096. element-wise copy is performed, which may throw.
  1097. In this case, the moved-from array is not
  1098. changed.
  1099. @par Complexity
  1100. Constant, or linear in the size of `*this` plus `arr.size()`.
  1101. @par Exception Safety
  1102. Strong guarantee.
  1103. Calls to `memory_resource::allocate` may throw.
  1104. @param arr The array to move-assign from.
  1105. */
  1106. BOOST_JSON_DECL value& operator=(array&& arr);
  1107. /** Assignment.
  1108. Replace `*this` with a copy of the obect `obj`.
  1109. @par Exception Safety
  1110. Strong guarantee.
  1111. Calls to `memory_resource::allocate` may throw.
  1112. @par Complexity
  1113. Linear in the sum of sizes of `*this` and `obj`
  1114. @param obj The new object.
  1115. */
  1116. BOOST_JSON_DECL value& operator=(object const& obj);
  1117. /** Assignment.
  1118. The contents of the value are replaced with the
  1119. contents of `obj` using move semantics:
  1120. @li If `*obj.storage() == *this->storage()`,
  1121. ownership of the underlying memory is transferred
  1122. in constant time, with no possibility of exceptions.
  1123. After assignment, the moved-from object becomes
  1124. empty with its current storage pointer.
  1125. @li If `*obj.storage() != *this->storage()`, an
  1126. element-wise copy is performed, which may throw.
  1127. In this case, the moved-from object is not
  1128. changed.
  1129. @par Complexity
  1130. Constant, or linear in the size of `*this` plus `obj.size()`.
  1131. @par Exception Safety
  1132. Strong guarantee.
  1133. Calls to `memory_resource::allocate` may throw.
  1134. @param obj The object to move-assign from.
  1135. */
  1136. BOOST_JSON_DECL value& operator=(object&& obj);
  1137. //------------------------------------------------------
  1138. //
  1139. // Modifiers
  1140. //
  1141. //------------------------------------------------------
  1142. /** Change the kind to null, discarding the previous contents.
  1143. The value is replaced with a null,
  1144. destroying the previous contents.
  1145. @par Complexity
  1146. Linear in the size of `*this`.
  1147. @par Exception Safety
  1148. No-throw guarantee.
  1149. */
  1150. void
  1151. emplace_null() noexcept
  1152. {
  1153. *this = nullptr;
  1154. }
  1155. /** Return a reference to a `bool`, changing the kind and replacing the contents.
  1156. The value is replaced with a `bool`
  1157. initialized to `false`, destroying the
  1158. previous contents.
  1159. @par Complexity
  1160. Linear in the size of `*this`.
  1161. @par Exception Safety
  1162. No-throw guarantee.
  1163. */
  1164. bool&
  1165. emplace_bool() noexcept
  1166. {
  1167. *this = false;
  1168. return sca_.b;
  1169. }
  1170. /** Return a reference to a `std::int64_t`, changing the kind and replacing the contents.
  1171. The value is replaced with a `std::int64_t`
  1172. initialized to zero, destroying the
  1173. previous contents.
  1174. @par Complexity
  1175. Linear in the size of `*this`.
  1176. @par Exception Safety
  1177. No-throw guarantee.
  1178. */
  1179. std::int64_t&
  1180. emplace_int64() noexcept
  1181. {
  1182. *this = std::int64_t{};
  1183. return sca_.i;
  1184. }
  1185. /** Return a reference to a `std::uint64_t`, changing the kind and replacing the contents.
  1186. The value is replaced with a `std::uint64_t`
  1187. initialized to zero, destroying the
  1188. previous contents.
  1189. @par Complexity
  1190. Linear in the size of `*this`.
  1191. @par Exception Safety
  1192. No-throw guarantee.
  1193. */
  1194. std::uint64_t&
  1195. emplace_uint64() noexcept
  1196. {
  1197. *this = std::uint64_t{};
  1198. return sca_.u;
  1199. }
  1200. /** Return a reference to a `double`, changing the kind and replacing the contents.
  1201. The value is replaced with a `double`
  1202. initialized to zero, destroying the
  1203. previous contents.
  1204. @par Complexity
  1205. Linear in the size of `*this`.
  1206. @par Exception Safety
  1207. No-throw guarantee.
  1208. */
  1209. double&
  1210. emplace_double() noexcept
  1211. {
  1212. *this = double{};
  1213. return sca_.d;
  1214. }
  1215. /** Return a reference to a @ref string, changing the kind and replacing the contents.
  1216. The value is replaced with an empty @ref string
  1217. using the current memory resource, destroying the
  1218. previous contents.
  1219. @par Complexity
  1220. Linear in the size of `*this`.
  1221. @par Exception Safety
  1222. No-throw guarantee.
  1223. */
  1224. BOOST_JSON_DECL
  1225. string&
  1226. emplace_string() noexcept;
  1227. /** Return a reference to an @ref array, changing the kind and replacing the contents.
  1228. The value is replaced with an empty @ref array
  1229. using the current memory resource, destroying the
  1230. previous contents.
  1231. @par Complexity
  1232. Linear in the size of `*this`.
  1233. @par Exception Safety
  1234. No-throw guarantee.
  1235. */
  1236. BOOST_JSON_DECL
  1237. array&
  1238. emplace_array() noexcept;
  1239. /** Return a reference to an @ref object, changing the kind and replacing the contents.
  1240. The contents are replaced with an empty @ref object
  1241. using the current @ref memory_resource. All
  1242. previously obtained iterators and references
  1243. obtained beforehand are invalidated.
  1244. @par Complexity
  1245. Linear in the size of `*this`.
  1246. @par Exception Safety
  1247. No-throw guarantee.
  1248. */
  1249. BOOST_JSON_DECL
  1250. object&
  1251. emplace_object() noexcept;
  1252. /** Swap the given values.
  1253. Exchanges the contents of this value with another
  1254. value. Ownership of the respective @ref memory_resource
  1255. objects is not transferred:
  1256. @li If `*other.storage() == *this->storage()`,
  1257. ownership of the underlying memory is swapped in
  1258. constant time, with no possibility of exceptions.
  1259. All iterators and references remain valid.
  1260. @li If `*other.storage() != *this->storage()`,
  1261. the contents are logically swapped by making copies,
  1262. which can throw. In this case all iterators and
  1263. references are invalidated.
  1264. @par Complexity
  1265. Constant or linear in the sum of the sizes of
  1266. the values.
  1267. @par Exception Safety
  1268. Strong guarantee.
  1269. Calls to `memory_resource::allocate` may throw.
  1270. @param other The value to swap with.
  1271. If `this == &other`, this function call has no effect.
  1272. */
  1273. BOOST_JSON_DECL
  1274. void
  1275. swap(value& other);
  1276. /** Swap the given values.
  1277. Exchanges the contents of value `lhs` with
  1278. another value `rhs`. Ownership of the respective
  1279. @ref memory_resource objects is not transferred.
  1280. @li If `*lhs.storage() == *rhs.storage()`,
  1281. ownership of the underlying memory is swapped in
  1282. constant time, with no possibility of exceptions.
  1283. All iterators and references remain valid.
  1284. @li If `*lhs.storage() != *rhs.storage`,
  1285. the contents are logically swapped by a copy,
  1286. which can throw. In this case all iterators and
  1287. references are invalidated.
  1288. @par Effects
  1289. @code
  1290. lhs.swap( rhs );
  1291. @endcode
  1292. @par Complexity
  1293. Constant or linear in the sum of the sizes of
  1294. the values.
  1295. @par Exception Safety
  1296. Strong guarantee.
  1297. Calls to `memory_resource::allocate` may throw.
  1298. @param lhs The value to exchange.
  1299. @param rhs The value to exchange.
  1300. If `&lhs == &rhs`, this function call has no effect.
  1301. @see @ref value::swap
  1302. */
  1303. friend
  1304. void
  1305. swap(value& lhs, value& rhs)
  1306. {
  1307. lhs.swap(rhs);
  1308. }
  1309. //------------------------------------------------------
  1310. //
  1311. // Observers
  1312. //
  1313. //------------------------------------------------------
  1314. /** Returns the kind of this JSON value.
  1315. This function returns the discriminating
  1316. enumeration constant of type @ref json::kind
  1317. corresponding to the underlying representation
  1318. stored in the container.
  1319. @par Complexity
  1320. Constant.
  1321. @par Exception Safety
  1322. No-throw guarantee.
  1323. */
  1324. json::kind
  1325. kind() const noexcept
  1326. {
  1327. return static_cast<json::kind>(
  1328. static_cast<unsigned char>(
  1329. sca_.k) & 0x3f);
  1330. }
  1331. /** Return `true` if this is an array
  1332. This function is used to determine if the underlying
  1333. representation is a certain kind.
  1334. @par Effects
  1335. @code
  1336. return this->kind() == kind::array;
  1337. @endcode
  1338. @par Complexity
  1339. Constant.
  1340. @par Exception Safety
  1341. No-throw guarantee.
  1342. */
  1343. bool
  1344. is_array() const noexcept
  1345. {
  1346. return kind() == json::kind::array;
  1347. }
  1348. /** Return `true` if this is an object
  1349. This function is used to determine if the underlying
  1350. representation is a certain kind.
  1351. @par Effects
  1352. @code
  1353. return this->kind() == kind::object;
  1354. @endcode
  1355. @par Complexity
  1356. Constant.
  1357. @par Exception Safety
  1358. No-throw guarantee.
  1359. */
  1360. bool
  1361. is_object() const noexcept
  1362. {
  1363. return kind() == json::kind::object;
  1364. }
  1365. /** Return `true` if this is a string
  1366. This function is used to determine if the underlying
  1367. representation is a certain kind.
  1368. @par Effects
  1369. @code
  1370. return this->kind() == kind::string;
  1371. @endcode
  1372. @par Complexity
  1373. Constant.
  1374. @par Exception Safety
  1375. No-throw guarantee.
  1376. */
  1377. bool
  1378. is_string() const noexcept
  1379. {
  1380. return kind() == json::kind::string;
  1381. }
  1382. /** Return `true` if this is a signed integer
  1383. This function is used to determine if the underlying
  1384. representation is a certain kind.
  1385. @par Effects
  1386. @code
  1387. return this->kind() == kind::int64;
  1388. @endcode
  1389. @par Complexity
  1390. Constant.
  1391. @par Exception Safety
  1392. No-throw guarantee.
  1393. */
  1394. bool
  1395. is_int64() const noexcept
  1396. {
  1397. return kind() == json::kind::int64;
  1398. }
  1399. /** Return `true` if this is a unsigned integer
  1400. This function is used to determine if the underlying
  1401. representation is a certain kind.
  1402. @par Effects
  1403. @code
  1404. return this->kind() == kind::uint64;
  1405. @endcode
  1406. @par Complexity
  1407. Constant.
  1408. @par Exception Safety
  1409. No-throw guarantee.
  1410. */
  1411. bool
  1412. is_uint64() const noexcept
  1413. {
  1414. return kind() == json::kind::uint64;
  1415. }
  1416. /** Return `true` if this is a double
  1417. This function is used to determine if the underlying
  1418. representation is a certain kind.
  1419. @par Effects
  1420. @code
  1421. return this->kind() == kind::double_;
  1422. @endcode
  1423. @par Complexity
  1424. Constant.
  1425. @par Exception Safety
  1426. No-throw guarantee.
  1427. */
  1428. bool
  1429. is_double() const noexcept
  1430. {
  1431. return kind() == json::kind::double_;
  1432. }
  1433. /** Return `true` if this is a bool
  1434. This function is used to determine if the underlying
  1435. representation is a certain kind.
  1436. @par Effects
  1437. @code
  1438. return this->kind() == kind::bool_;
  1439. @endcode
  1440. @par Complexity
  1441. Constant.
  1442. @par Exception Safety
  1443. No-throw guarantee.
  1444. */
  1445. bool
  1446. is_bool() const noexcept
  1447. {
  1448. return kind() == json::kind::bool_;
  1449. }
  1450. /** Returns true if this is a null.
  1451. This function is used to determine if the underlying
  1452. representation is a certain kind.
  1453. @par Effects
  1454. @code
  1455. return this->kind() == kind::null;
  1456. @endcode
  1457. @par Complexity
  1458. Constant.
  1459. @par Exception Safety
  1460. No-throw guarantee.
  1461. */
  1462. bool
  1463. is_null() const noexcept
  1464. {
  1465. return kind() == json::kind::null;
  1466. }
  1467. /** Returns true if this is an array or object.
  1468. This function returns `true` if
  1469. @ref kind() is either `kind::object` or
  1470. `kind::array`.
  1471. @par Complexity
  1472. Constant.
  1473. @par Exception Safety
  1474. No-throw guarantee.
  1475. */
  1476. bool
  1477. is_structured() const noexcept
  1478. {
  1479. // VFALCO Could use bit 0x20 for this
  1480. return
  1481. kind() == json::kind::object ||
  1482. kind() == json::kind::array;
  1483. }
  1484. /** Returns true if this is not an array or object.
  1485. This function returns `true` if
  1486. @ref kind() is neither `kind::object` nor
  1487. `kind::array`.
  1488. @par Complexity
  1489. Constant.
  1490. @par Exception Safety
  1491. No-throw guarantee.
  1492. */
  1493. bool
  1494. is_primitive() const noexcept
  1495. {
  1496. // VFALCO Could use bit 0x20 for this
  1497. return
  1498. sca_.k != json::kind::object &&
  1499. sca_.k != json::kind::array;
  1500. }
  1501. /** Returns true if this is a number.
  1502. This function returns `true` when
  1503. @ref kind() is one of the following values:
  1504. `kind::int64`, `kind::uint64`, or
  1505. `kind::double_`.
  1506. @par Complexity
  1507. Constant.
  1508. @par Exception Safety
  1509. No-throw guarantee.
  1510. */
  1511. bool
  1512. is_number() const noexcept
  1513. {
  1514. // VFALCO Could use bit 0x40 for this
  1515. return
  1516. kind() == json::kind::int64 ||
  1517. kind() == json::kind::uint64 ||
  1518. kind() == json::kind::double_;
  1519. }
  1520. //------------------------------------------------------
  1521. /** Return an @ref array pointer if this is an array, else return `nullptr`
  1522. If `this->kind() == kind::array`, returns a pointer
  1523. to the underlying array. Otherwise, returns `nullptr`.
  1524. @par Example
  1525. The return value is used in both a boolean context and
  1526. to assign a variable:
  1527. @code
  1528. if( auto p = jv.if_array() )
  1529. return *p;
  1530. @endcode
  1531. @par Complexity
  1532. Constant.
  1533. @par Exception Safety
  1534. No-throw guarantee.
  1535. */
  1536. array const*
  1537. if_array() const noexcept
  1538. {
  1539. if(kind() == json::kind::array)
  1540. return &arr_;
  1541. return nullptr;
  1542. }
  1543. /** Return an @ref array pointer if this is an array, else return `nullptr`
  1544. If `this->kind() == kind::array`, returns a pointer
  1545. to the underlying array. Otherwise, returns `nullptr`.
  1546. @par Example
  1547. The return value is used in both a boolean context and
  1548. to assign a variable:
  1549. @code
  1550. if( auto p = jv.if_array() )
  1551. return *p;
  1552. @endcode
  1553. @par Complexity
  1554. Constant.
  1555. @par Exception Safety
  1556. No-throw guarantee.
  1557. */
  1558. array*
  1559. if_array() noexcept
  1560. {
  1561. if(kind() == json::kind::array)
  1562. return &arr_;
  1563. return nullptr;
  1564. }
  1565. /** Return an @ref object pointer if this is an object, else return `nullptr`
  1566. If `this->kind() == kind::object`, returns a pointer
  1567. to the underlying object. Otherwise, returns `nullptr`.
  1568. @par Example
  1569. The return value is used in both a boolean context and
  1570. to assign a variable:
  1571. @code
  1572. if( auto p = jv.if_object() )
  1573. return *p;
  1574. @endcode
  1575. @par Complexity
  1576. Constant.
  1577. @par Exception Safety
  1578. No-throw guarantee.
  1579. */
  1580. object const*
  1581. if_object() const noexcept
  1582. {
  1583. if(kind() == json::kind::object)
  1584. return &obj_;
  1585. return nullptr;
  1586. }
  1587. /** Return an @ref object pointer if this is an object, else return `nullptr`
  1588. If `this->kind() == kind::object`, returns a pointer
  1589. to the underlying object. Otherwise, returns `nullptr`.
  1590. @par Example
  1591. The return value is used in both a boolean context and
  1592. to assign a variable:
  1593. @code
  1594. if( auto p = jv.if_object() )
  1595. return *p;
  1596. @endcode
  1597. @par Complexity
  1598. Constant.
  1599. @par Exception Safety
  1600. No-throw guarantee.
  1601. */
  1602. object*
  1603. if_object() noexcept
  1604. {
  1605. if(kind() == json::kind::object)
  1606. return &obj_;
  1607. return nullptr;
  1608. }
  1609. /** Return a @ref string pointer if this is a string, else return `nullptr`
  1610. If `this->kind() == kind::string`, returns a pointer
  1611. to the underlying object. Otherwise, returns `nullptr`.
  1612. @par Example
  1613. The return value is used in both a boolean context and
  1614. to assign a variable:
  1615. @code
  1616. if( auto p = jv.if_string() )
  1617. return *p;
  1618. @endcode
  1619. @par Complexity
  1620. Constant.
  1621. @par Exception Safety
  1622. No-throw guarantee.
  1623. */
  1624. string const*
  1625. if_string() const noexcept
  1626. {
  1627. if(kind() == json::kind::string)
  1628. return &str_;
  1629. return nullptr;
  1630. }
  1631. /** Return a @ref string pointer if this is a string, else return `nullptr`
  1632. If `this->kind() == kind::string`, returns a pointer
  1633. to the underlying object. Otherwise, returns `nullptr`.
  1634. @par Example
  1635. The return value is used in both a boolean context and
  1636. to assign a variable:
  1637. @code
  1638. if( auto p = jv.if_string() )
  1639. return *p;
  1640. @endcode
  1641. @par Complexity
  1642. Constant.
  1643. @par Exception Safety
  1644. No-throw guarantee.
  1645. */
  1646. string*
  1647. if_string() noexcept
  1648. {
  1649. if(kind() == json::kind::string)
  1650. return &str_;
  1651. return nullptr;
  1652. }
  1653. /** Return an `int64_t` pointer if this is a signed integer, else return `nullptr`
  1654. If `this->kind() == kind::int64`, returns a pointer
  1655. to the underlying integer. Otherwise, returns `nullptr`.
  1656. @par Example
  1657. The return value is used in both a boolean context and
  1658. to assign a variable:
  1659. @code
  1660. if( auto p = jv.if_int64() )
  1661. return *p;
  1662. @endcode
  1663. @par Complexity
  1664. Constant.
  1665. @par Exception Safety
  1666. No-throw guarantee.
  1667. */
  1668. std::int64_t const*
  1669. if_int64() const noexcept
  1670. {
  1671. if(kind() == json::kind::int64)
  1672. return &sca_.i;
  1673. return nullptr;
  1674. }
  1675. /** Return an `int64_t` pointer if this is a signed integer, else return `nullptr`
  1676. If `this->kind() == kind::int64`, returns a pointer
  1677. to the underlying integer. Otherwise, returns `nullptr`.
  1678. @par Example
  1679. The return value is used in both a boolean context and
  1680. to assign a variable:
  1681. @code
  1682. if( auto p = jv.if_int64() )
  1683. return *p;
  1684. @endcode
  1685. @par Complexity
  1686. Constant.
  1687. @par Exception Safety
  1688. No-throw guarantee.
  1689. */
  1690. std::int64_t*
  1691. if_int64() noexcept
  1692. {
  1693. if(kind() == json::kind::int64)
  1694. return &sca_.i;
  1695. return nullptr;
  1696. }
  1697. /** Return a `uint64_t` pointer if this is an unsigned integer, else return `nullptr`
  1698. If `this->kind() == kind::uint64`, returns a pointer
  1699. to the underlying unsigned integer. Otherwise, returns
  1700. `nullptr`.
  1701. @par Example
  1702. The return value is used in both a boolean context and
  1703. to assign a variable:
  1704. @code
  1705. if( auto p = jv.if_uint64() )
  1706. return *p;
  1707. @endcode
  1708. @par Complexity
  1709. Constant.
  1710. @par Exception Safety
  1711. No-throw guarantee.
  1712. */
  1713. std::uint64_t const*
  1714. if_uint64() const noexcept
  1715. {
  1716. if(kind() == json::kind::uint64)
  1717. return &sca_.u;
  1718. return nullptr;
  1719. }
  1720. /** Return a `uint64_t` pointer if this is an unsigned integer, else return `nullptr`
  1721. If `this->kind() == kind::uint64`, returns a pointer
  1722. to the underlying unsigned integer. Otherwise, returns
  1723. `nullptr`.
  1724. @par Example
  1725. The return value is used in both a boolean context and
  1726. to assign a variable:
  1727. @code
  1728. if( auto p = jv.if_uint64() )
  1729. return *p;
  1730. @endcode
  1731. @par Complexity
  1732. Constant.
  1733. @par Exception Safety
  1734. No-throw guarantee.
  1735. */
  1736. std::uint64_t*
  1737. if_uint64() noexcept
  1738. {
  1739. if(kind() == json::kind::uint64)
  1740. return &sca_.u;
  1741. return nullptr;
  1742. }
  1743. /** Return a `double` pointer if this is a double, else return `nullptr`
  1744. If `this->kind() == kind::double_`, returns a pointer
  1745. to the underlying double. Otherwise, returns
  1746. `nullptr`.
  1747. @par Example
  1748. The return value is used in both a boolean context and
  1749. to assign a variable:
  1750. @code
  1751. if( auto p = jv.if_double() )
  1752. return *p;
  1753. @endcode
  1754. @par Complexity
  1755. Constant.
  1756. @par Exception Safety
  1757. No-throw guarantee.
  1758. */
  1759. double const*
  1760. if_double() const noexcept
  1761. {
  1762. if(kind() == json::kind::double_)
  1763. return &sca_.d;
  1764. return nullptr;
  1765. }
  1766. /** Return a `double` pointer if this is a double, else return `nullptr`
  1767. If `this->kind() == kind::double_`, returns a pointer
  1768. to the underlying double. Otherwise, returns
  1769. `nullptr`.
  1770. @par Example
  1771. The return value is used in both a boolean context and
  1772. to assign a variable:
  1773. @code
  1774. if( auto p = jv.if_double() )
  1775. return *p;
  1776. @endcode
  1777. @par Complexity
  1778. Constant.
  1779. @par Exception Safety
  1780. No-throw guarantee.
  1781. */
  1782. double*
  1783. if_double() noexcept
  1784. {
  1785. if(kind() == json::kind::double_)
  1786. return &sca_.d;
  1787. return nullptr;
  1788. }
  1789. /** Return a `bool` pointer if this is a boolean, else return `nullptr`
  1790. If `this->kind() == kind::bool_`, returns a pointer
  1791. to the underlying boolean. Otherwise, returns
  1792. `nullptr`.
  1793. @par Example
  1794. The return value is used in both a boolean context and
  1795. to assign a variable:
  1796. @code
  1797. if( auto p = jv.if_bool() )
  1798. return *p;
  1799. @endcode
  1800. @par Complexity
  1801. Constant.
  1802. @par Exception Safety
  1803. No-throw guarantee.
  1804. */
  1805. bool const*
  1806. if_bool() const noexcept
  1807. {
  1808. if(kind() == json::kind::bool_)
  1809. return &sca_.b;
  1810. return nullptr;
  1811. }
  1812. /** Return a `bool` pointer if this is a boolean, else return `nullptr`
  1813. If `this->kind() == kind::bool_`, returns a pointer
  1814. to the underlying boolean. Otherwise, returns
  1815. `nullptr`.
  1816. @par Example
  1817. The return value is used in both a boolean context and
  1818. to assign a variable:
  1819. @code
  1820. if( auto p = jv.if_bool() )
  1821. return *p;
  1822. @endcode
  1823. @par Complexity
  1824. Constant.
  1825. @par Exception Safety
  1826. No-throw guarantee.
  1827. */
  1828. bool*
  1829. if_bool() noexcept
  1830. {
  1831. if(kind() == json::kind::bool_)
  1832. return &sca_.b;
  1833. return nullptr;
  1834. }
  1835. //------------------------------------------------------
  1836. /** Return the stored number cast to an arithmetic type.
  1837. This function attempts to return the stored value
  1838. converted to the arithmetic type `T` which may not
  1839. be `bool`:
  1840. @li If `T` is an integral type and the stored
  1841. value is a number which can be losslessly converted,
  1842. the conversion is performed without error and the
  1843. converted number is returned.
  1844. @li If `T` is an integral type and the stored value
  1845. is a number which cannot be losslessly converted,
  1846. then the operation fails with an error.
  1847. @li If `T` is a floating point type and the stored
  1848. value is a number, the conversion is performed
  1849. without error. The converted number is returned,
  1850. with a possible loss of precision.
  1851. @li Otherwise, if the stored value is not a number;
  1852. that is, if `this->is_number()` returns `false`, then
  1853. the operation fails with an error.
  1854. @par Constraints
  1855. @code
  1856. std::is_arithmetic< T >::value && ! std::is_same< T, bool >::value
  1857. @endcode
  1858. @par Complexity
  1859. Constant.
  1860. @par Exception Safety
  1861. No-throw guarantee.
  1862. @return The converted number.
  1863. @param ec Set to the error, if any occurred.
  1864. */
  1865. /** @{ */
  1866. template<class T>
  1867. #ifdef BOOST_JSON_DOCS
  1868. T
  1869. #else
  1870. typename std::enable_if<
  1871. std::is_arithmetic<T>::value &&
  1872. ! std::is_same<T, bool>::value,
  1873. T>::type
  1874. #endif
  1875. to_number(error_code& ec) const noexcept
  1876. {
  1877. error e;
  1878. auto result = to_number<T>(e);
  1879. BOOST_JSON_FAIL(ec, e);
  1880. return result;
  1881. }
  1882. template<class T>
  1883. #ifdef BOOST_JSON_DOCS
  1884. T
  1885. #else
  1886. typename std::enable_if<
  1887. std::is_arithmetic<T>::value &&
  1888. ! std::is_same<T, bool>::value,
  1889. T>::type
  1890. #endif
  1891. to_number(std::error_code& ec) const noexcept
  1892. {
  1893. error_code jec;
  1894. auto result = to_number<T>(jec);
  1895. ec = jec;
  1896. return result;
  1897. }
  1898. /** @} */
  1899. /** Return the stored number cast to an arithmetic type.
  1900. This function attempts to return the stored value
  1901. converted to the arithmetic type `T` which may not
  1902. be `bool`:
  1903. @li If `T` is an integral type and the stored
  1904. value is a number which can be losslessly converted,
  1905. the conversion is performed without error and the
  1906. converted number is returned.
  1907. @li If `T` is an integral type and the stored value
  1908. is a number which cannot be losslessly converted,
  1909. then the operation fails with an error.
  1910. @li If `T` is a floating point type and the stored
  1911. value is a number, the conversion is performed
  1912. without error. The converted number is returned,
  1913. with a possible loss of precision.
  1914. @li Otherwise, if the stored value is not a number;
  1915. that is, if `this->is_number()` returns `false`, then
  1916. the operation fails with an error.
  1917. @par Constraints
  1918. @code
  1919. std::is_arithmetic< T >::value && ! std::is_same< T, bool >::value
  1920. @endcode
  1921. @par Complexity
  1922. Constant.
  1923. @return The converted number.
  1924. @throw system_error on error.
  1925. */
  1926. template<class T>
  1927. #ifdef BOOST_JSON_DOCS
  1928. T
  1929. #else
  1930. typename std::enable_if<
  1931. std::is_arithmetic<T>::value &&
  1932. ! std::is_same<T, bool>::value,
  1933. T>::type
  1934. #endif
  1935. to_number() const
  1936. {
  1937. error e;
  1938. auto result = to_number<T>(e);
  1939. if( e != error() )
  1940. {
  1941. BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
  1942. detail::throw_system_error( e, &loc );
  1943. }
  1944. return result;
  1945. }
  1946. //------------------------------------------------------
  1947. //
  1948. // Accessors
  1949. //
  1950. //------------------------------------------------------
  1951. /** Return the memory resource associated with the value.
  1952. This returns a pointer to the memory resource
  1953. that was used to construct the value.
  1954. @par Complexity
  1955. Constant.
  1956. @par Exception Safety
  1957. No-throw guarantee.
  1958. */
  1959. storage_ptr const&
  1960. storage() const noexcept
  1961. {
  1962. return sp_;
  1963. }
  1964. /** Return the associated @ref memory_resource
  1965. This function returns an instance of
  1966. @ref polymorphic_allocator constructed from the
  1967. associated @ref memory_resource.
  1968. @par Complexity
  1969. Constant.
  1970. @par Exception Safety
  1971. No-throw guarantee.
  1972. */
  1973. allocator_type
  1974. get_allocator() const noexcept
  1975. {
  1976. return sp_.get();
  1977. }
  1978. //------------------------------------------------------
  1979. /** Return a reference to the underlying `object`, or throw an exception.
  1980. If @ref is_object() is `true`, returns
  1981. a reference to the underlying @ref object,
  1982. otherwise throws an exception.
  1983. @par Complexity
  1984. Constant.
  1985. @par Exception Safety
  1986. Strong guarantee.
  1987. @throw system_error `! this->is_object()`
  1988. */
  1989. /* @{ */
  1990. object&
  1991. as_object() &
  1992. {
  1993. auto const& self = *this;
  1994. return const_cast<object&>( self.as_object() );
  1995. }
  1996. object&&
  1997. as_object() &&
  1998. {
  1999. return std::move( as_object() );
  2000. }
  2001. object const&
  2002. as_object() const&
  2003. {
  2004. if( is_object() )
  2005. return obj_;
  2006. BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
  2007. detail::throw_system_error( error::not_object, &loc );
  2008. }
  2009. /* @} */
  2010. /** Return a reference to the underlying @ref array, or throw an exception.
  2011. If @ref is_array() is `true`, returns
  2012. a reference to the underlying @ref array,
  2013. otherwise throws an exception.
  2014. @par Complexity
  2015. Constant.
  2016. @par Exception Safety
  2017. Strong guarantee.
  2018. @throw system_error `! this->is_array()`
  2019. */
  2020. /* @{ */
  2021. array&
  2022. as_array() &
  2023. {
  2024. auto const& self = *this;
  2025. return const_cast<array&>( self.as_array() );
  2026. }
  2027. array&&
  2028. as_array() &&
  2029. {
  2030. return std::move( as_array() );
  2031. }
  2032. array const&
  2033. as_array() const&
  2034. {
  2035. if( is_array() )
  2036. return arr_;
  2037. BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
  2038. detail::throw_system_error( error::not_array, &loc );
  2039. }
  2040. /* @} */
  2041. /** Return a reference to the underlying `string`, or throw an exception.
  2042. If @ref is_string() is `true`, returns
  2043. a reference to the underlying @ref string,
  2044. otherwise throws an exception.
  2045. @par Complexity
  2046. Constant.
  2047. @par Exception Safety
  2048. Strong guarantee.
  2049. @throw system_error `! this->is_string()`
  2050. */
  2051. /* @{ */
  2052. string&
  2053. as_string() &
  2054. {
  2055. auto const& self = *this;
  2056. return const_cast<string&>( self.as_string() );
  2057. }
  2058. string&&
  2059. as_string() &&
  2060. {
  2061. return std::move( as_string() );
  2062. }
  2063. string const&
  2064. as_string() const&
  2065. {
  2066. if( is_string() )
  2067. return str_;
  2068. BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
  2069. detail::throw_system_error( error::not_string, &loc );
  2070. }
  2071. /* @} */
  2072. /** Return a reference to the underlying `std::int64_t`, or throw an exception.
  2073. If @ref is_int64() is `true`, returns
  2074. a reference to the underlying `std::int64_t`,
  2075. otherwise throws an exception.
  2076. @par Complexity
  2077. Constant.
  2078. @par Exception Safety
  2079. Strong guarantee.
  2080. @throw system_error `! this->is_int64()`
  2081. @par Note
  2082. This function is intended for direct access to the underlying object,
  2083. __if__ it has the type `std::int64_t`. It does not convert the
  2084. underlying object to type `std::int64_t` even if a lossless conversion
  2085. is possible. If you are not sure which kind your `value` has, and you
  2086. only care about getting a `std::int64_t` number, consider using
  2087. @ref to_number instead.
  2088. */
  2089. std::int64_t&
  2090. as_int64()
  2091. {
  2092. if( is_int64() )
  2093. return sca_.i;
  2094. BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
  2095. detail::throw_system_error( error::not_int64, &loc );
  2096. }
  2097. /** Return the underlying `std::int64_t`, or throw an exception.
  2098. If @ref is_int64() is `true`, returns
  2099. the underlying `std::int64_t`,
  2100. otherwise throws an exception.
  2101. @par Complexity
  2102. Constant.
  2103. @par Exception Safety
  2104. Strong guarantee.
  2105. @throw system_error `! this->is_int64()`
  2106. @par Note
  2107. This function is the const-qualified overload of @ref as_int64, which
  2108. is intended for direct access to the underlying object, __if__ it has
  2109. the type `std::int64_t`. It does not convert the underlying object to
  2110. type `std::int64_t` even if a lossless conversion is possible. If you
  2111. are not sure which kind your `value` has, and you only care about
  2112. getting a `std::int64_t` number, consider using @ref to_number instead.
  2113. */
  2114. std::int64_t
  2115. as_int64() const
  2116. {
  2117. if( is_int64() )
  2118. return sca_.i;
  2119. BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
  2120. detail::throw_system_error( error::not_int64, &loc );
  2121. }
  2122. /** Return a reference to the underlying `std::uint64_t`, or throw an exception.
  2123. If @ref is_uint64() is `true`, returns
  2124. a reference to the underlying `std::uint64_t`,
  2125. otherwise throws an exception.
  2126. @par Complexity
  2127. Constant.
  2128. @par Exception Safety
  2129. Strong guarantee.
  2130. @throw system_error `! this->is_uint64()`
  2131. @par Note
  2132. This function is intended for direct access to the underlying object,
  2133. __if__ it has the type `std::uint64_t`. It does not convert the
  2134. underlying object to type `std::uint64_t` even if a lossless conversion
  2135. is possible. If you are not sure which kind your `value` has, and you
  2136. only care about getting a `std::uint64_t` number, consider using
  2137. @ref to_number instead.
  2138. */
  2139. std::uint64_t&
  2140. as_uint64()
  2141. {
  2142. if( is_uint64() )
  2143. return sca_.u;
  2144. BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
  2145. detail::throw_system_error( error::not_uint64, &loc );
  2146. }
  2147. /** Return the underlying `std::uint64_t`, or throw an exception.
  2148. If @ref is_uint64() is `true`, returns
  2149. the underlying `std::uint64_t`,
  2150. otherwise throws an exception.
  2151. @par Complexity
  2152. Constant.
  2153. @par Exception Safety
  2154. Strong guarantee.
  2155. @throw system_error `! this->is_uint64()`
  2156. @par Note
  2157. This function is the const-qualified overload of @ref as_uint64, which
  2158. is intended for direct access to the underlying object, __if__ it has
  2159. the type `std::uint64_t`. It does not convert the underlying object to
  2160. type `std::uint64_t` even if a lossless conversion is possible. If you
  2161. are not sure which kind your `value` has, and you only care about
  2162. getting a `std::uint64_t` number, consider using
  2163. @ref to_number instead.
  2164. */
  2165. std::uint64_t
  2166. as_uint64() const
  2167. {
  2168. if( is_uint64() )
  2169. return sca_.u;
  2170. BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
  2171. detail::throw_system_error( error::not_uint64, &loc );
  2172. }
  2173. /** Return a reference to the underlying `double`, or throw an exception.
  2174. If @ref is_double() is `true`, returns
  2175. a reference to the underlying `double`,
  2176. otherwise throws an exception.
  2177. @par Complexity
  2178. Constant.
  2179. @par Exception Safety
  2180. Strong guarantee.
  2181. @throw system_error `! this->is_double()`
  2182. @par Note
  2183. This function is intended for direct access to the underlying object,
  2184. __if__ it has the type `double`. It does not convert the underlying
  2185. object to type `double` even if a lossless conversion is possible. If
  2186. you are not sure which kind your `value` has, and you only care about
  2187. getting a `double` number, consider using @ref to_number instead.
  2188. */
  2189. double&
  2190. as_double()
  2191. {
  2192. if( is_double() )
  2193. return sca_.d;
  2194. BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
  2195. detail::throw_system_error( error::not_double, &loc );
  2196. }
  2197. /** Return the underlying `double`, or throw an exception.
  2198. If @ref is_double() is `true`, returns
  2199. the underlying `double`,
  2200. otherwise throws an exception.
  2201. @par Complexity
  2202. Constant.
  2203. @par Exception Safety
  2204. Strong guarantee.
  2205. @throw system_error `! this->is_double()`
  2206. @par Note
  2207. This function is the const-qualified overload of @ref as_double, which
  2208. is intended for direct access to the underlying object, __if__ it has
  2209. the type `double`. It does not convert the underlying object to type
  2210. `double` even if a lossless conversion is possible. If you are not sure
  2211. which kind your `value` has, and you only care about getting a `double`
  2212. number, consider using @ref to_number instead.
  2213. */
  2214. double
  2215. as_double() const
  2216. {
  2217. if( is_double() )
  2218. return sca_.d;
  2219. BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
  2220. detail::throw_system_error( error::not_double, &loc );
  2221. }
  2222. /** Return a reference to the underlying `bool`, or throw an exception.
  2223. If @ref is_bool() is `true`, returns
  2224. a reference to the underlying `bool`,
  2225. otherwise throws an exception.
  2226. @par Complexity
  2227. Constant.
  2228. @par Exception Safety
  2229. Strong guarantee.
  2230. @throw system_error `! this->is_bool()`
  2231. */
  2232. bool&
  2233. as_bool()
  2234. {
  2235. if( is_bool() )
  2236. return sca_.b;
  2237. BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
  2238. detail::throw_system_error( error::not_bool, &loc );
  2239. }
  2240. /** Return the underlying `bool`, or throw an exception.
  2241. If @ref is_bool() is `true`, returns
  2242. the underlying `bool`,
  2243. otherwise throws an exception.
  2244. @par Complexity
  2245. Constant.
  2246. @par Exception Safety
  2247. Strong guarantee.
  2248. @throw system_error `! this->is_bool()`
  2249. */
  2250. bool
  2251. as_bool() const
  2252. {
  2253. if( is_bool() )
  2254. return sca_.b;
  2255. BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
  2256. detail::throw_system_error( error::not_bool, &loc );
  2257. }
  2258. //------------------------------------------------------
  2259. /** Return a reference to the underlying `object`, without checking.
  2260. This is the fastest way to access the underlying
  2261. representation when the kind is known in advance.
  2262. @par Preconditions
  2263. @code
  2264. this->is_object()
  2265. @endcode
  2266. @par Complexity
  2267. Constant.
  2268. @par Exception Safety
  2269. No-throw guarantee.
  2270. */
  2271. /* @{ */
  2272. object&
  2273. get_object() & noexcept
  2274. {
  2275. BOOST_ASSERT(is_object());
  2276. return obj_;
  2277. }
  2278. object&&
  2279. get_object() && noexcept
  2280. {
  2281. BOOST_ASSERT(is_object());
  2282. return std::move(obj_);
  2283. }
  2284. object const&
  2285. get_object() const& noexcept
  2286. {
  2287. BOOST_ASSERT(is_object());
  2288. return obj_;
  2289. }
  2290. /* @} */
  2291. /** Return a reference to the underlying `array`, without checking.
  2292. This is the fastest way to access the underlying
  2293. representation when the kind is known in advance.
  2294. @par Preconditions
  2295. @code
  2296. this->is_array()
  2297. @endcode
  2298. @par Complexity
  2299. Constant.
  2300. @par Exception Safety
  2301. No-throw guarantee.
  2302. */
  2303. /* @{ */
  2304. array&
  2305. get_array() & noexcept
  2306. {
  2307. BOOST_ASSERT(is_array());
  2308. return arr_;
  2309. }
  2310. array&&
  2311. get_array() && noexcept
  2312. {
  2313. BOOST_ASSERT(is_array());
  2314. return std::move(arr_);
  2315. }
  2316. array const&
  2317. get_array() const& noexcept
  2318. {
  2319. BOOST_ASSERT(is_array());
  2320. return arr_;
  2321. }
  2322. /* @} */
  2323. /** Return a reference to the underlying `string`, without checking.
  2324. This is the fastest way to access the underlying
  2325. representation when the kind is known in advance.
  2326. @par Preconditions
  2327. @code
  2328. this->is_string()
  2329. @endcode
  2330. @par Complexity
  2331. Constant.
  2332. @par Exception Safety
  2333. No-throw guarantee.
  2334. */
  2335. /* @{ */
  2336. string&
  2337. get_string() & noexcept
  2338. {
  2339. BOOST_ASSERT(is_string());
  2340. return str_;
  2341. }
  2342. string&&
  2343. get_string() && noexcept
  2344. {
  2345. BOOST_ASSERT(is_string());
  2346. return std::move(str_);
  2347. }
  2348. string const&
  2349. get_string() const& noexcept
  2350. {
  2351. BOOST_ASSERT(is_string());
  2352. return str_;
  2353. }
  2354. /* @} */
  2355. /** Return a reference to the underlying `std::int64_t`, without checking.
  2356. This is the fastest way to access the underlying
  2357. representation when the kind is known in advance.
  2358. @par Preconditions
  2359. @code
  2360. this->is_int64()
  2361. @endcode
  2362. @par Complexity
  2363. Constant.
  2364. @par Exception Safety
  2365. No-throw guarantee.
  2366. */
  2367. std::int64_t&
  2368. get_int64() noexcept
  2369. {
  2370. BOOST_ASSERT(is_int64());
  2371. return sca_.i;
  2372. }
  2373. /** Return the underlying `std::int64_t`, without checking.
  2374. This is the fastest way to access the underlying
  2375. representation when the kind is known in advance.
  2376. @par Preconditions
  2377. @code
  2378. this->is_int64()
  2379. @endcode
  2380. @par Complexity
  2381. Constant.
  2382. @par Exception Safety
  2383. No-throw guarantee.
  2384. */
  2385. std::int64_t
  2386. get_int64() const noexcept
  2387. {
  2388. BOOST_ASSERT(is_int64());
  2389. return sca_.i;
  2390. }
  2391. /** Return a reference to the underlying `std::uint64_t`, without checking.
  2392. This is the fastest way to access the underlying
  2393. representation when the kind is known in advance.
  2394. @par Preconditions
  2395. @code
  2396. this->is_uint64()
  2397. @endcode
  2398. @par Complexity
  2399. Constant.
  2400. @par Exception Safety
  2401. No-throw guarantee.
  2402. */
  2403. std::uint64_t&
  2404. get_uint64() noexcept
  2405. {
  2406. BOOST_ASSERT(is_uint64());
  2407. return sca_.u;
  2408. }
  2409. /** Return the underlying `std::uint64_t`, without checking.
  2410. This is the fastest way to access the underlying
  2411. representation when the kind is known in advance.
  2412. @par Preconditions
  2413. @code
  2414. this->is_uint64()
  2415. @endcode
  2416. @par Complexity
  2417. Constant.
  2418. @par Exception Safety
  2419. No-throw guarantee.
  2420. */
  2421. std::uint64_t
  2422. get_uint64() const noexcept
  2423. {
  2424. BOOST_ASSERT(is_uint64());
  2425. return sca_.u;
  2426. }
  2427. /** Return a reference to the underlying `double`, without checking.
  2428. This is the fastest way to access the underlying
  2429. representation when the kind is known in advance.
  2430. @par Preconditions
  2431. @code
  2432. this->is_double()
  2433. @endcode
  2434. @par Complexity
  2435. Constant.
  2436. @par Exception Safety
  2437. No-throw guarantee.
  2438. */
  2439. double&
  2440. get_double() noexcept
  2441. {
  2442. BOOST_ASSERT(is_double());
  2443. return sca_.d;
  2444. }
  2445. /** Return the underlying `double`, without checking.
  2446. This is the fastest way to access the underlying
  2447. representation when the kind is known in advance.
  2448. @par Preconditions
  2449. @code
  2450. this->is_double()
  2451. @endcode
  2452. @par Complexity
  2453. Constant.
  2454. @par Exception Safety
  2455. No-throw guarantee.
  2456. */
  2457. double
  2458. get_double() const noexcept
  2459. {
  2460. BOOST_ASSERT(is_double());
  2461. return sca_.d;
  2462. }
  2463. /** Return a reference to the underlying `bool`, without checking.
  2464. This is the fastest way to access the underlying
  2465. representation when the kind is known in advance.
  2466. @par Preconditions
  2467. @code
  2468. this->is_bool()
  2469. @endcode
  2470. @par Complexity
  2471. Constant.
  2472. @par Exception Safety
  2473. No-throw guarantee.
  2474. */
  2475. bool&
  2476. get_bool() noexcept
  2477. {
  2478. BOOST_ASSERT(is_bool());
  2479. return sca_.b;
  2480. }
  2481. /** Return the underlying `bool`, without checking.
  2482. This is the fastest way to access the underlying
  2483. representation when the kind is known in advance.
  2484. @par Preconditions
  2485. @code
  2486. this->is_bool()
  2487. @endcode
  2488. @par Complexity
  2489. Constant.
  2490. @par Exception Safety
  2491. No-throw guarantee.
  2492. */
  2493. bool
  2494. get_bool() const noexcept
  2495. {
  2496. BOOST_ASSERT(is_bool());
  2497. return sca_.b;
  2498. }
  2499. //------------------------------------------------------
  2500. /** Access an element, with bounds checking.
  2501. This function is used to access elements of
  2502. the underlying object, or throw an exception
  2503. if the value is not an object.
  2504. @par Complexity
  2505. Constant.
  2506. @par Exception Safety
  2507. Strong guarantee.
  2508. @param key The key of the element to find.
  2509. @return `this->as_object().at( key )`.
  2510. */
  2511. /** @{ */
  2512. value&
  2513. at(string_view key) &
  2514. {
  2515. return as_object().at(key);
  2516. }
  2517. value&&
  2518. at(string_view key) &&
  2519. {
  2520. return std::move( as_object() ).at(key);
  2521. }
  2522. value const&
  2523. at(string_view key) const&
  2524. {
  2525. return as_object().at(key);
  2526. }
  2527. /** @} */
  2528. /** Access an element, with bounds checking.
  2529. This function is used to access elements of
  2530. the underlying array, or throw an exception
  2531. if the value is not an array.
  2532. @par Complexity
  2533. Constant.
  2534. @par Exception Safety
  2535. Strong guarantee.
  2536. @param pos A zero-based array index.
  2537. @return `this->as_array().at( pos )`.
  2538. */
  2539. /** @{ */
  2540. value &
  2541. at(std::size_t pos) &
  2542. {
  2543. return as_array().at(pos);
  2544. }
  2545. value&&
  2546. at(std::size_t pos) &&
  2547. {
  2548. return std::move( as_array() ).at(pos);
  2549. }
  2550. value const&
  2551. at(std::size_t pos) const&
  2552. {
  2553. return as_array().at(pos);
  2554. }
  2555. /** @} */
  2556. /** Access an element via JSON Pointer.
  2557. This function is used to access a (potentially nested)
  2558. element of the value using a JSON Pointer string.
  2559. @par Complexity
  2560. Linear in the sizes of `ptr` and underlying array, object, or string.
  2561. @par Exception Safety
  2562. Strong guarantee.
  2563. @param ptr JSON Pointer string.
  2564. @return reference to the element identified by `ptr`.
  2565. @throw system_error if an error occurs.
  2566. @see
  2567. <a href="https://datatracker.ietf.org/doc/html/rfc6901">
  2568. RFC 6901 - JavaScript Object Notation (JSON) Pointer</a>
  2569. */
  2570. /** @{ */
  2571. BOOST_JSON_DECL
  2572. value const&
  2573. at_pointer(string_view ptr) const&;
  2574. inline
  2575. value&&
  2576. at_pointer(string_view ptr) &&;
  2577. inline
  2578. value&
  2579. at_pointer(string_view ptr) &;
  2580. /** @} */
  2581. /** Access an element via JSON Pointer.
  2582. This function is used to access a (potentially nested)
  2583. element of the value using a JSON Pointer string.
  2584. @par Complexity
  2585. Linear in the sizes of `ptr` and underlying array, object, or string.
  2586. @par Exception Safety
  2587. No-throw guarantee.
  2588. @param ptr JSON Pointer string.
  2589. @param ec Set to the error, if any occurred.
  2590. @return pointer to the element identified by `ptr`.
  2591. @see
  2592. <a href="https://datatracker.ietf.org/doc/html/rfc6901">
  2593. RFC 6901 - JavaScript Object Notation (JSON) Pointer</a>
  2594. */
  2595. /** @{ */
  2596. BOOST_JSON_DECL
  2597. value const*
  2598. find_pointer(string_view ptr, error_code& ec) const noexcept;
  2599. BOOST_JSON_DECL
  2600. value*
  2601. find_pointer(string_view ptr, error_code& ec) noexcept;
  2602. BOOST_JSON_DECL
  2603. value const*
  2604. find_pointer(string_view ptr, std::error_code& ec) const noexcept;
  2605. BOOST_JSON_DECL
  2606. value*
  2607. find_pointer(string_view ptr, std::error_code& ec) noexcept;
  2608. /** @} */
  2609. //------------------------------------------------------
  2610. /** Set an element via JSON Pointer.
  2611. This function is used to insert or assign to a potentially nested
  2612. element of the value using a JSON Pointer string. The function may
  2613. create intermediate elements corresponding to pointer segments.
  2614. <br/>
  2615. The particular conditions when and what kind of intermediate element
  2616. is created is governed by the `ptr` parameter.
  2617. Each pointer token is considered in sequence. For each token
  2618. - if the containing value is an @ref object, then a new `null`
  2619. element is created with key equal to unescaped token string; otherwise
  2620. - if the containing value is an @ref array, and the token represents a
  2621. past-the-end marker, then a `null` element is appended to the array;
  2622. otherwise
  2623. - if the containing value is an @ref array, and the token represents a
  2624. number, then if the difference between the number and array's size
  2625. is smaller than `opts.max_created_elements`, then the size of the
  2626. array is increased, so that the number can reference an element in the
  2627. array; otherwise
  2628. - if the containing value is of different @ref kind and
  2629. `opts.replace_any_scalar` is `true`, or the value is `null`, then
  2630. - if `opts.create_arrays` is `true` and the token either represents
  2631. past-the-end marker or a number, then the value is replaced with
  2632. an empty array and the token is considered again; otherwise
  2633. - if `opts.create_objects` is `true`, then the value is replaced
  2634. with an empty object and the token is considered again; otherwise
  2635. - an error is produced.
  2636. @par Complexity
  2637. Linear in the sum of size of `ptr`, size of underlying array, object,
  2638. or string and `opts.max_created_elements`.
  2639. @par Exception Safety
  2640. Basic guarantee.
  2641. Calls to `memory_resource::allocate` may throw.
  2642. @param sv JSON Pointer string.
  2643. @param ref The value to assign to pointed element.
  2644. @param opts The options for the algorithm.
  2645. @return Reference to the element identified by `ptr`.
  2646. @see @ref set_pointer_options,
  2647. <a href="https://datatracker.ietf.org/doc/html/rfc6901">
  2648. RFC 6901 - JavaScript Object Notation (JSON) Pointer</a>.
  2649. */
  2650. BOOST_JSON_DECL
  2651. value&
  2652. set_at_pointer(
  2653. string_view sv,
  2654. value_ref ref,
  2655. set_pointer_options const& opts = {} );
  2656. /** Set an element via JSON Pointer.
  2657. This function is used to insert or assign to a potentially nested
  2658. element of the value using a JSON Pointer string. The function may
  2659. create intermediate elements corresponding to pointer segments.
  2660. <br/>
  2661. The particular conditions when and what kind of intermediate element
  2662. is created is governed by the `ptr` parameter.
  2663. Each pointer token is considered in sequence. For each token
  2664. - if the containing value is an @ref object, then a new `null`
  2665. element is created with key equal to unescaped token string;
  2666. otherwise
  2667. - if the containing value is an @ref array, and the token represents a
  2668. past-the-end marker, then a `null` element is appended to the array;
  2669. otherwise
  2670. - if the containing value is an @ref array, and the token represents a
  2671. number, then if the difference between the number and array's size
  2672. is smaller than `opts.max_created_elements`, then the size of the
  2673. array is increased, so that the number can reference an element in the
  2674. array; otherwise
  2675. - if the containing value is of different @ref kind and
  2676. `opts.replace_any_scalar` is `true`, or the value is `null`, then
  2677. - if `opts.create_arrays` is `true` and the token either represents
  2678. past-the-end marker or a number, then the value is replaced with
  2679. an empty array and the token is considered again; otherwise
  2680. - if `opts.create_objects` is `true`, then the value is replaced
  2681. with an empty object and the token is considered again; otherwise
  2682. - an error is produced.
  2683. @par Complexity
  2684. Linear in the sum of size of `ptr`, size of underlying array, object,
  2685. or string and `opts.max_created_elements`.
  2686. @par Exception Safety
  2687. Basic guarantee.
  2688. Calls to `memory_resource::allocate` may throw.
  2689. @param sv JSON Pointer string.
  2690. @param ref The value to assign to pointed element.
  2691. @param ec Set to the error, if any occurred.
  2692. @param opts The options for the algorithm.
  2693. @return Pointer to the element identified by `ptr`.
  2694. @see @ref set_pointer_options,
  2695. <a href="https://datatracker.ietf.org/doc/html/rfc6901">
  2696. RFC 6901 - JavaScript Object Notation (JSON) Pointer</a>.
  2697. */
  2698. /** @{ */
  2699. BOOST_JSON_DECL
  2700. value*
  2701. set_at_pointer(
  2702. string_view sv,
  2703. value_ref ref,
  2704. error_code& ec,
  2705. set_pointer_options const& opts = {} );
  2706. BOOST_JSON_DECL
  2707. value*
  2708. set_at_pointer(
  2709. string_view sv,
  2710. value_ref ref,
  2711. std::error_code& ec,
  2712. set_pointer_options const& opts = {} );
  2713. /** @} */
  2714. //------------------------------------------------------
  2715. /** Return `true` if two values are equal.
  2716. Two values are equal when they are the
  2717. same kind and their referenced values
  2718. are equal, or when they are both integral
  2719. types and their integral representations
  2720. are equal.
  2721. @par Complexity
  2722. Constant or linear in the size of
  2723. the array, object, or string.
  2724. @par Exception Safety
  2725. No-throw guarantee.
  2726. */
  2727. // inline friend speeds up overload resolution
  2728. friend
  2729. bool
  2730. operator==(
  2731. value const& lhs,
  2732. value const& rhs) noexcept
  2733. {
  2734. return lhs.equal(rhs);
  2735. }
  2736. /** Return `true` if two values are not equal.
  2737. Two values are equal when they are the
  2738. same kind and their referenced values
  2739. are equal, or when they are both integral
  2740. types and their integral representations
  2741. are equal.
  2742. @par Complexity
  2743. Constant or linear in the size of
  2744. the array, object, or string.
  2745. @par Exception Safety
  2746. No-throw guarantee.
  2747. */
  2748. friend
  2749. bool
  2750. operator!=(
  2751. value const& lhs,
  2752. value const& rhs) noexcept
  2753. {
  2754. return ! (lhs == rhs);
  2755. }
  2756. /** Serialize @ref value to an output stream.
  2757. This function serializes a `value` as JSON into the output stream.
  2758. @return Reference to `os`.
  2759. @par Complexity
  2760. Constant or linear in the size of `jv`.
  2761. @par Exception Safety
  2762. Strong guarantee.
  2763. Calls to `memory_resource::allocate` may throw.
  2764. @param os The output stream to serialize to.
  2765. @param jv The value to serialize.
  2766. */
  2767. BOOST_JSON_DECL
  2768. friend
  2769. std::ostream&
  2770. operator<<(
  2771. std::ostream& os,
  2772. value const& jv);
  2773. /** Parse @ref value from an input stream.
  2774. This function parses JSON from an input stream into a `value`. If
  2775. parsing fails, `std::ios_base::failbit` will be set for `is` and
  2776. `jv` will be left unchanged. Regardless of whether `skipws` flag is set
  2777. on `is`, consumes whitespace before and after JSON, because whitespace
  2778. is considered a part of JSON. Behaves as [_FormattedInputFunction_]
  2779. (https://en.cppreference.com/w/cpp/named_req/FormattedInputFunction).<br>
  2780. Note: this operator cannot assume that the stream only contains a
  2781. single JSON document, which may result in **very underwhelming
  2782. performance**, if the stream isn't cooperative. If you know that your
  2783. input consists of a single JSON document, consider using @ref parse
  2784. function instead.
  2785. @return Reference to `is`.
  2786. @par Complexity
  2787. Linear in the size of JSON data.
  2788. @par Exception Safety
  2789. Basic guarantee.
  2790. Calls to `memory_resource::allocate` may throw.
  2791. The stream may throw as configured by
  2792. [`std::ios::exceptions`](https://en.cppreference.com/w/cpp/io/basic_ios/exceptions).
  2793. @param is The input stream to parse from.
  2794. @param jv The value to parse into.
  2795. @see @ref parse.
  2796. */
  2797. BOOST_JSON_DECL
  2798. friend
  2799. std::istream&
  2800. operator>>(
  2801. std::istream& is,
  2802. value& jv);
  2803. /** Helper for `boost::hash` support
  2804. Computes a hash value for `jv`. This function is used by
  2805. `boost::hash<value>`. Similar overloads for @ref array, @ref object,
  2806. and @ref string do not exist, because those types are supported by
  2807. `boost::hash` out of the box.
  2808. @return hash value for `jv`.
  2809. @param jv `value` for which a hash is to be computed.
  2810. @see [Boost.ContainerHash](https://boost.org/libs/container_hash).
  2811. */
  2812. #ifndef BOOST_JSON_DOCS
  2813. template<
  2814. class T,
  2815. typename std::enable_if<
  2816. std::is_same< detail::remove_cvref<T>, value >::value >::type*
  2817. = nullptr>
  2818. friend
  2819. std::size_t
  2820. hash_value( T const& jv ) noexcept
  2821. #else
  2822. friend
  2823. inline
  2824. std::size_t
  2825. hash_value( value const& jv ) noexcept
  2826. #endif
  2827. {
  2828. return detail::hash_value_impl(jv);
  2829. }
  2830. private:
  2831. static
  2832. void
  2833. relocate(
  2834. value* dest,
  2835. value const& src) noexcept
  2836. {
  2837. std::memcpy(
  2838. static_cast<void*>(dest),
  2839. &src,
  2840. sizeof(src));
  2841. }
  2842. BOOST_JSON_DECL
  2843. storage_ptr
  2844. destroy() noexcept;
  2845. BOOST_JSON_DECL
  2846. bool
  2847. equal(value const& other) const noexcept;
  2848. template<class T>
  2849. auto
  2850. to_number(error& e) const noexcept ->
  2851. typename std::enable_if<
  2852. std::is_signed<T>::value &&
  2853. ! std::is_floating_point<T>::value,
  2854. T>::type
  2855. {
  2856. if(sca_.k == json::kind::int64)
  2857. {
  2858. auto const i = sca_.i;
  2859. if( i >= (std::numeric_limits<T>::min)() &&
  2860. i <= (std::numeric_limits<T>::max)())
  2861. {
  2862. e = {};
  2863. return static_cast<T>(i);
  2864. }
  2865. e = error::not_exact;
  2866. }
  2867. else if(sca_.k == json::kind::uint64)
  2868. {
  2869. auto const u = sca_.u;
  2870. if(u <= static_cast<std::uint64_t>((
  2871. std::numeric_limits<T>::max)()))
  2872. {
  2873. e = {};
  2874. return static_cast<T>(u);
  2875. }
  2876. e = error::not_exact;
  2877. }
  2878. else if(sca_.k == json::kind::double_)
  2879. {
  2880. auto const d = sca_.d;
  2881. if( d >= static_cast<double>(
  2882. (detail::to_number_limit<T>::min)()) &&
  2883. d <= static_cast<double>(
  2884. (detail::to_number_limit<T>::max)()) &&
  2885. static_cast<T>(d) == d)
  2886. {
  2887. e = {};
  2888. return static_cast<T>(d);
  2889. }
  2890. e = error::not_exact;
  2891. }
  2892. else
  2893. {
  2894. e = error::not_number;
  2895. }
  2896. return T{};
  2897. }
  2898. template<class T>
  2899. auto
  2900. to_number(error& e) const noexcept ->
  2901. typename std::enable_if<
  2902. std::is_unsigned<T>::value &&
  2903. ! std::is_same<T, bool>::value,
  2904. T>::type
  2905. {
  2906. if(sca_.k == json::kind::int64)
  2907. {
  2908. auto const i = sca_.i;
  2909. if( i >= 0 && static_cast<std::uint64_t>(i) <=
  2910. (std::numeric_limits<T>::max)())
  2911. {
  2912. e = {};
  2913. return static_cast<T>(i);
  2914. }
  2915. e = error::not_exact;
  2916. }
  2917. else if(sca_.k == json::kind::uint64)
  2918. {
  2919. auto const u = sca_.u;
  2920. if(u <= (std::numeric_limits<T>::max)())
  2921. {
  2922. e = {};
  2923. return static_cast<T>(u);
  2924. }
  2925. e = error::not_exact;
  2926. }
  2927. else if(sca_.k == json::kind::double_)
  2928. {
  2929. auto const d = sca_.d;
  2930. if( d >= 0 &&
  2931. d <= (detail::to_number_limit<T>::max)() &&
  2932. static_cast<T>(d) == d)
  2933. {
  2934. e = {};
  2935. return static_cast<T>(d);
  2936. }
  2937. e = error::not_exact;
  2938. }
  2939. else
  2940. {
  2941. e = error::not_number;
  2942. }
  2943. return T{};
  2944. }
  2945. template<class T>
  2946. auto
  2947. to_number(error& e) const noexcept ->
  2948. typename std::enable_if<
  2949. std::is_floating_point<
  2950. T>::value, T>::type
  2951. {
  2952. if(sca_.k == json::kind::int64)
  2953. {
  2954. e = {};
  2955. return static_cast<T>(sca_.i);
  2956. }
  2957. if(sca_.k == json::kind::uint64)
  2958. {
  2959. e = {};
  2960. return static_cast<T>(sca_.u);
  2961. }
  2962. if(sca_.k == json::kind::double_)
  2963. {
  2964. e = {};
  2965. return static_cast<T>(sca_.d);
  2966. }
  2967. e = error::not_number;
  2968. return {};
  2969. }
  2970. };
  2971. // Make sure things are as big as we think they should be
  2972. #if BOOST_JSON_ARCH == 64
  2973. BOOST_STATIC_ASSERT(sizeof(value) == 24);
  2974. #elif BOOST_JSON_ARCH == 32
  2975. BOOST_STATIC_ASSERT(sizeof(value) == 16);
  2976. #else
  2977. # error Unknown architecture
  2978. #endif
  2979. //----------------------------------------------------------
  2980. /** A key/value pair.
  2981. This is the type of element used by the @ref object
  2982. container.
  2983. */
  2984. class key_value_pair
  2985. {
  2986. #ifndef BOOST_JSON_DOCS
  2987. friend struct detail::access;
  2988. using access = detail::access;
  2989. #endif
  2990. BOOST_JSON_DECL
  2991. static char const empty_[1];
  2992. inline
  2993. key_value_pair(
  2994. pilfered<json::value> k,
  2995. pilfered<json::value> v) noexcept;
  2996. public:
  2997. /// Copy assignment (deleted).
  2998. key_value_pair&
  2999. operator=(key_value_pair const&) = delete;
  3000. /** Destructor.
  3001. The value is destroyed and all internally
  3002. allocated memory is freed.
  3003. */
  3004. ~key_value_pair() noexcept
  3005. {
  3006. auto const& sp = value_.storage();
  3007. if(sp.is_not_shared_and_deallocate_is_trivial())
  3008. return;
  3009. if(key_ == empty_)
  3010. return;
  3011. sp->deallocate(const_cast<char*>(key_),
  3012. len_ + 1, alignof(char));
  3013. }
  3014. /** Copy constructor.
  3015. This constructs a key/value pair with a
  3016. copy of another key/value pair, using
  3017. the same memory resource as `other`.
  3018. @par Exception Safety
  3019. Strong guarantee.
  3020. Calls to `memory_resource::allocate` may throw.
  3021. @param other The key/value pair to copy.
  3022. */
  3023. key_value_pair(
  3024. key_value_pair const& other)
  3025. : key_value_pair(other,
  3026. other.storage())
  3027. {
  3028. }
  3029. /** Copy constructor.
  3030. This constructs a key/value pair with a
  3031. copy of another key/value pair, using
  3032. the specified memory resource.
  3033. @par Exception Safety
  3034. Strong guarantee.
  3035. Calls to `memory_resource::allocate` may throw.
  3036. @param other The key/value pair to copy.
  3037. @param sp A pointer to the @ref memory_resource
  3038. to use. The element will acquire shared
  3039. ownership of the memory resource.
  3040. */
  3041. BOOST_JSON_DECL
  3042. key_value_pair(
  3043. key_value_pair const& other,
  3044. storage_ptr sp);
  3045. /** Move constructor.
  3046. The pair is constructed by acquiring
  3047. ownership of the contents of `other` and
  3048. shared ownership of `other`'s memory resource.
  3049. @note
  3050. After construction, the moved-from pair holds an
  3051. empty key, and a null value with its current
  3052. storage pointer.
  3053. @par Complexity
  3054. Constant.
  3055. @par Exception Safety
  3056. No-throw guarantee.
  3057. @param other The pair to move.
  3058. */
  3059. key_value_pair(
  3060. key_value_pair&& other) noexcept
  3061. : value_(std::move(other.value_))
  3062. , key_(detail::exchange(
  3063. other.key_, empty_))
  3064. , len_(detail::exchange(
  3065. other.len_, 0))
  3066. {
  3067. }
  3068. /** Pilfer constructor.
  3069. The pair is constructed by acquiring ownership
  3070. of the contents of `other` using pilfer semantics.
  3071. This is more efficient than move construction, when
  3072. it is known that the moved-from object will be
  3073. immediately destroyed afterwards.
  3074. @par Complexity
  3075. Constant.
  3076. @par Exception Safety
  3077. No-throw guarantee.
  3078. @param other The value to pilfer. After pilfer
  3079. construction, `other` is not in a usable state
  3080. and may only be destroyed.
  3081. @see @ref pilfer,
  3082. <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0308r0.html">
  3083. Valueless Variants Considered Harmful</a>
  3084. */
  3085. key_value_pair(
  3086. pilfered<key_value_pair> other) noexcept
  3087. : value_(pilfer(other.get().value_))
  3088. , key_(detail::exchange(
  3089. other.get().key_, empty_))
  3090. , len_(detail::exchange(
  3091. other.get().len_, 0))
  3092. {
  3093. }
  3094. /** Constructor.
  3095. This constructs a key/value pair.
  3096. @par Exception Safety
  3097. Strong guarantee.
  3098. Calls to `memory_resource::allocate` may throw.
  3099. @param key The key string to use.
  3100. @param args Optional arguments forwarded to
  3101. the @ref value constructor.
  3102. */
  3103. template<class... Args>
  3104. explicit
  3105. key_value_pair(
  3106. string_view key,
  3107. Args&&... args)
  3108. : value_(std::forward<Args>(args)...)
  3109. {
  3110. if(key.size() > string::max_size())
  3111. {
  3112. BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
  3113. detail::throw_system_error( error::key_too_large, &loc );
  3114. }
  3115. auto s = reinterpret_cast<
  3116. char*>(value_.storage()->
  3117. allocate(key.size() + 1, alignof(char)));
  3118. std::memcpy(s, key.data(), key.size());
  3119. s[key.size()] = 0;
  3120. key_ = s;
  3121. len_ = static_cast<
  3122. std::uint32_t>(key.size());
  3123. }
  3124. /** Constructor.
  3125. This constructs a key/value pair. A
  3126. copy of the specified value is made,
  3127. using the specified memory resource.
  3128. @par Exception Safety
  3129. Strong guarantee.
  3130. Calls to `memory_resource::allocate` may throw.
  3131. @param p A `std::pair` with the key
  3132. string and @ref value to construct with.
  3133. @param sp A pointer to the @ref memory_resource
  3134. to use. The element will acquire shared
  3135. ownership of the memory resource.
  3136. */
  3137. explicit
  3138. key_value_pair(
  3139. std::pair<
  3140. string_view,
  3141. json::value> const& p,
  3142. storage_ptr sp = {})
  3143. : key_value_pair(
  3144. p.first,
  3145. p.second,
  3146. std::move(sp))
  3147. {
  3148. }
  3149. /** Constructor.
  3150. This constructs a key/value pair.
  3151. Ownership of the specified value is
  3152. transferred by move construction.
  3153. @par Exception Safety
  3154. Strong guarantee.
  3155. Calls to `memory_resource::allocate` may throw.
  3156. @param p A `std::pair` with the key
  3157. string and @ref value to construct with.
  3158. @param sp A pointer to the @ref memory_resource
  3159. to use. The element will acquire shared
  3160. ownership of the memory resource.
  3161. */
  3162. explicit
  3163. key_value_pair(
  3164. std::pair<
  3165. string_view,
  3166. json::value>&& p,
  3167. storage_ptr sp = {})
  3168. : key_value_pair(
  3169. p.first,
  3170. std::move(p).second,
  3171. std::move(sp))
  3172. {
  3173. }
  3174. /** Return the associated memory resource.
  3175. This returns a pointer to the memory
  3176. resource used to construct the value.
  3177. @par Complexity
  3178. Constant.
  3179. @par Exception Safety
  3180. No-throw guarantee.
  3181. */
  3182. storage_ptr const&
  3183. storage() const noexcept
  3184. {
  3185. return value_.storage();
  3186. }
  3187. /** Return the key of this element.
  3188. After construction, the key may
  3189. not be modified.
  3190. @par Complexity
  3191. Constant.
  3192. @par Exception Safety
  3193. No-throw guarantee.
  3194. */
  3195. string_view const
  3196. key() const noexcept
  3197. {
  3198. return { key_, len_ };
  3199. }
  3200. /** Return the key of this element as a null-terminated string.
  3201. @par Complexity
  3202. Constant.
  3203. @par Exception Safety
  3204. No-throw guarantee.
  3205. */
  3206. char const*
  3207. key_c_str() const noexcept
  3208. {
  3209. return key_;
  3210. }
  3211. /** Return the value of this element.
  3212. @par Complexity
  3213. Constant.
  3214. @par Exception Safety
  3215. No-throw guarantee.
  3216. */
  3217. /* @{ */
  3218. json::value const&
  3219. value() const& noexcept
  3220. {
  3221. return value_;
  3222. }
  3223. json::value&&
  3224. value() && noexcept
  3225. {
  3226. return std::move( value() );
  3227. }
  3228. json::value&
  3229. value() & noexcept
  3230. {
  3231. return value_;
  3232. }
  3233. /* @} */
  3234. private:
  3235. json::value value_;
  3236. char const* key_;
  3237. std::uint32_t len_;
  3238. std::uint32_t next_;
  3239. };
  3240. //----------------------------------------------------------
  3241. #ifdef BOOST_JSON_DOCS
  3242. /** Tuple-like element access.
  3243. This overload permits the key and value
  3244. of a `key_value_pair` to be accessed
  3245. by index. For example:
  3246. @code
  3247. key_value_pair kvp("num", 42);
  3248. string_view key = get<0>(kvp);
  3249. value& jv = get<1>(kvp);
  3250. @endcode
  3251. @par Structured Bindings
  3252. When using C++17 or greater, objects of type
  3253. @ref key_value_pair may be used to initialize
  3254. structured bindings:
  3255. @code
  3256. key_value_pair kvp("num", 42);
  3257. auto& [key, value] = kvp;
  3258. @endcode
  3259. Depending on the value of `I`, the return type will be:
  3260. @li `string_view const` if `I == 0`, or
  3261. @li `value&`, `value const&`, or `value&&` if `I == 1`.
  3262. Any other value for `I` is ill-formed.
  3263. @tparam I The element index to access.
  3264. @par Constraints
  3265. `std::is_same_v< std::remove_cvref_t<T>, key_value_pair >`
  3266. @return `kvp.key()` if `I == 0`, or `kvp.value()`
  3267. if `I == 1`.
  3268. @param kvp The @ref key_value_pair object
  3269. to access.
  3270. */
  3271. template<
  3272. std::size_t I,
  3273. class T>
  3274. __see_below__
  3275. get(T&& kvp) noexcept;
  3276. #else
  3277. template<std::size_t I>
  3278. auto
  3279. get(key_value_pair const&) noexcept ->
  3280. typename std::conditional<I == 0,
  3281. string_view const,
  3282. value const&>::type
  3283. {
  3284. static_assert(I == 0,
  3285. "key_value_pair index out of range");
  3286. }
  3287. template<std::size_t I>
  3288. auto
  3289. get(key_value_pair&) noexcept ->
  3290. typename std::conditional<I == 0,
  3291. string_view const,
  3292. value&>::type
  3293. {
  3294. static_assert(I == 0,
  3295. "key_value_pair index out of range");
  3296. }
  3297. template<std::size_t I>
  3298. auto
  3299. get(key_value_pair&&) noexcept ->
  3300. typename std::conditional<I == 0,
  3301. string_view const,
  3302. value&&>::type
  3303. {
  3304. static_assert(I == 0,
  3305. "key_value_pair index out of range");
  3306. }
  3307. /** Extracts a key_value_pair's key using tuple-like interface
  3308. */
  3309. template<>
  3310. inline
  3311. string_view const
  3312. get<0>(key_value_pair const& kvp) noexcept
  3313. {
  3314. return kvp.key();
  3315. }
  3316. /** Extracts a key_value_pair's key using tuple-like interface
  3317. */
  3318. template<>
  3319. inline
  3320. string_view const
  3321. get<0>(key_value_pair& kvp) noexcept
  3322. {
  3323. return kvp.key();
  3324. }
  3325. /** Extracts a key_value_pair's key using tuple-like interface
  3326. */
  3327. template<>
  3328. inline
  3329. string_view const
  3330. get<0>(key_value_pair&& kvp) noexcept
  3331. {
  3332. return kvp.key();
  3333. }
  3334. /** Extracts a key_value_pair's value using tuple-like interface
  3335. */
  3336. template<>
  3337. inline
  3338. value const&
  3339. get<1>(key_value_pair const& kvp) noexcept
  3340. {
  3341. return kvp.value();
  3342. }
  3343. /** Extracts a key_value_pair's value using tuple-like interface
  3344. */
  3345. template<>
  3346. inline
  3347. value&
  3348. get<1>(key_value_pair& kvp) noexcept
  3349. {
  3350. return kvp.value();
  3351. }
  3352. /** Extracts a key_value_pair's value using tuple-like interface
  3353. */
  3354. template<>
  3355. inline
  3356. value&&
  3357. get<1>(key_value_pair&& kvp) noexcept
  3358. {
  3359. return std::move(kvp.value());
  3360. }
  3361. #endif
  3362. } // namespace json
  3363. } // namespace boost
  3364. #ifdef __clang__
  3365. # pragma clang diagnostic push
  3366. # pragma clang diagnostic ignored "-Wmismatched-tags"
  3367. #endif
  3368. #ifndef BOOST_JSON_DOCS
  3369. namespace std {
  3370. /** Tuple-like size access for key_value_pair
  3371. */
  3372. template<>
  3373. struct tuple_size< ::boost::json::key_value_pair >
  3374. : std::integral_constant<std::size_t, 2>
  3375. {
  3376. };
  3377. /** Tuple-like access for the key type of key_value_pair
  3378. */
  3379. template<>
  3380. struct tuple_element<0, ::boost::json::key_value_pair>
  3381. {
  3382. using type = ::boost::json::string_view const;
  3383. };
  3384. /** Tuple-like access for the value type of key_value_pair
  3385. */
  3386. template<>
  3387. struct tuple_element<1, ::boost::json::key_value_pair>
  3388. {
  3389. using type = ::boost::json::value&;
  3390. };
  3391. /** Tuple-like access for the value type of key_value_pair
  3392. */
  3393. template<>
  3394. struct tuple_element<1, ::boost::json::key_value_pair const>
  3395. {
  3396. using type = ::boost::json::value const&;
  3397. };
  3398. } // std
  3399. #endif
  3400. // std::hash specialization
  3401. #ifndef BOOST_JSON_DOCS
  3402. namespace std {
  3403. template <>
  3404. struct hash< ::boost::json::value > {
  3405. BOOST_JSON_DECL
  3406. std::size_t
  3407. operator()(::boost::json::value const& jv) const noexcept;
  3408. };
  3409. } // std
  3410. #endif
  3411. #ifdef __clang__
  3412. # pragma clang diagnostic pop
  3413. #endif
  3414. // These are here because value, array,
  3415. // and object form cyclic references.
  3416. #include <boost/json/detail/impl/array.hpp>
  3417. #include <boost/json/impl/array.hpp>
  3418. #include <boost/json/impl/object.hpp>
  3419. #include <boost/json/impl/value.hpp>
  3420. // These must come after array and object
  3421. #include <boost/json/impl/value_ref.hpp>
  3422. #endif