value.hpp 100 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243
  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. If the initializer list consists of key/value
  789. pairs, an @ref object is created. Otherwise
  790. an @ref array is created. The contents of the
  791. initializer list are copied to the newly constructed
  792. value using the specified memory resource.
  793. @par Complexity
  794. Linear in `init.size()`.
  795. @par Exception Safety
  796. Strong guarantee.
  797. Calls to `memory_resource::allocate` may throw.
  798. @param init The initializer list to construct from.
  799. @param sp A pointer to the @ref memory_resource
  800. to use. The container will acquire shared
  801. ownership of the memory resource.
  802. */
  803. BOOST_JSON_DECL
  804. value(
  805. std::initializer_list<value_ref> init,
  806. storage_ptr sp = {});
  807. //------------------------------------------------------
  808. //
  809. // Assignment
  810. //
  811. //------------------------------------------------------
  812. /** Copy assignment.
  813. The contents of the value are replaced with an
  814. element-wise copy of the contents of `other`.
  815. @par Complexity
  816. Linear in the size of `*this` plus `other`.
  817. @par Exception Safety
  818. Strong guarantee.
  819. Calls to `memory_resource::allocate` may throw.
  820. @param other The value to copy.
  821. */
  822. BOOST_JSON_DECL
  823. value&
  824. operator=(value const& other);
  825. /** Move assignment.
  826. The contents of the value are replaced with the
  827. contents of `other` using move semantics:
  828. @li If `*other.storage() == *sp`, ownership of
  829. the underlying memory is transferred in constant
  830. time, with no possibility of exceptions.
  831. After assignment, the moved-from value becomes
  832. a null with its current storage pointer.
  833. @li If `*other.storage() != *sp`, an
  834. element-wise copy is performed if
  835. `other.is_structured() == true`, which may throw.
  836. In this case, the moved-from value is not
  837. changed.
  838. @par Complexity
  839. Constant, or linear in
  840. `this->size()` plus `other.size()`.
  841. @par Exception Safety
  842. Strong guarantee.
  843. Calls to `memory_resource::allocate` may throw.
  844. @param other The value to assign from.
  845. */
  846. BOOST_JSON_DECL
  847. value&
  848. operator=(value&& other);
  849. /** Assignment.
  850. Replace `*this` with the value formed by
  851. constructing from `init` and `this->storage()`.
  852. If the initializer list consists of key/value
  853. pairs, the resulting @ref object is assigned.
  854. Otherwise an @ref array is assigned. The contents
  855. of the initializer list are moved to `*this`
  856. using the existing memory resource.
  857. @par Complexity
  858. Linear in `init.size()`.
  859. @par Exception Safety
  860. Strong guarantee.
  861. Calls to `memory_resource::allocate` may throw.
  862. @param init The initializer list to assign from.
  863. */
  864. BOOST_JSON_DECL
  865. value&
  866. operator=(
  867. std::initializer_list<value_ref> init);
  868. /** Assignment.
  869. Replace `*this` with null.
  870. @par Exception Safety
  871. No-throw guarantee.
  872. @par Complexity
  873. Linear in the size of `*this`.
  874. */
  875. value&
  876. operator=(std::nullptr_t) noexcept
  877. {
  878. if(is_scalar())
  879. {
  880. sca_.k = json::kind::null;
  881. }
  882. else
  883. {
  884. ::new(&sca_) scalar(
  885. destroy());
  886. }
  887. return *this;
  888. }
  889. /** Assignment.
  890. Replace `*this` with `b`.
  891. @par Exception Safety
  892. No-throw guarantee.
  893. @par Complexity
  894. Linear in the size of `*this`.
  895. @param b The new value.
  896. */
  897. #ifdef BOOST_JSON_DOCS
  898. value& operator=(bool b) noexcept;
  899. #else
  900. template<class T
  901. ,class = typename std::enable_if<
  902. std::is_same<T, bool>::value>::type
  903. >
  904. value& operator=(T b) noexcept
  905. {
  906. if(is_scalar())
  907. {
  908. sca_.b = b;
  909. sca_.k = json::kind::bool_;
  910. }
  911. else
  912. {
  913. ::new(&sca_) scalar(
  914. b, destroy());
  915. }
  916. return *this;
  917. }
  918. #endif
  919. /** Assignment.
  920. Replace `*this` with `i`.
  921. @par Exception Safety
  922. No-throw guarantee.
  923. @par Complexity
  924. Linear in the size of `*this`.
  925. @param i The new value.
  926. */
  927. /** @{ */
  928. value& operator=(signed char i) noexcept
  929. {
  930. return operator=(
  931. static_cast<long long>(i));
  932. }
  933. value& operator=(short i) noexcept
  934. {
  935. return operator=(
  936. static_cast<long long>(i));
  937. }
  938. value& operator=(int i) noexcept
  939. {
  940. return operator=(
  941. static_cast<long long>(i));
  942. }
  943. value& operator=(long i) noexcept
  944. {
  945. return operator=(
  946. static_cast<long long>(i));
  947. }
  948. value& operator=(long long i) noexcept
  949. {
  950. if(is_scalar())
  951. {
  952. sca_.i = i;
  953. sca_.k = json::kind::int64;
  954. }
  955. else
  956. {
  957. ::new(&sca_) scalar(static_cast<
  958. std::int64_t>(i), destroy());
  959. }
  960. return *this;
  961. }
  962. /** @} */
  963. /** Assignment.
  964. Replace `*this` with `i`.
  965. @par Exception Safety
  966. No-throw guarantee.
  967. @par Complexity
  968. Linear in the size of `*this`.
  969. @param u The new value.
  970. */
  971. /** @{ */
  972. value& operator=(unsigned char u) noexcept
  973. {
  974. return operator=(static_cast<
  975. unsigned long long>(u));
  976. }
  977. value& operator=(unsigned short u) noexcept
  978. {
  979. return operator=(static_cast<
  980. unsigned long long>(u));
  981. }
  982. value& operator=(unsigned int u) noexcept
  983. {
  984. return operator=(static_cast<
  985. unsigned long long>(u));
  986. }
  987. value& operator=(unsigned long u) noexcept
  988. {
  989. return operator=(static_cast<
  990. unsigned long long>(u));
  991. }
  992. value& operator=(unsigned long long u) noexcept
  993. {
  994. if(is_scalar())
  995. {
  996. sca_.u = u;
  997. sca_.k = json::kind::uint64;
  998. }
  999. else
  1000. {
  1001. ::new(&sca_) scalar(static_cast<
  1002. std::uint64_t>(u), destroy());
  1003. }
  1004. return *this;
  1005. }
  1006. /** @} */
  1007. /** Assignment.
  1008. Replace `*this` with `d`.
  1009. @par Exception Safety
  1010. No-throw guarantee.
  1011. @par Complexity
  1012. Linear in the size of `*this`.
  1013. @param d The new value.
  1014. */
  1015. value& operator=(double d) noexcept
  1016. {
  1017. if(is_scalar())
  1018. {
  1019. sca_.d = d;
  1020. sca_.k = json::kind::double_;
  1021. }
  1022. else
  1023. {
  1024. ::new(&sca_) scalar(
  1025. d, destroy());
  1026. }
  1027. return *this;
  1028. }
  1029. /** Assignment.
  1030. Replace `*this` with a copy of the string `s`.
  1031. @par Exception Safety
  1032. Strong guarantee.
  1033. Calls to `memory_resource::allocate` may throw.
  1034. @par Complexity
  1035. Linear in the sum of sizes of `*this` and `s`
  1036. @param s The new string.
  1037. */
  1038. /** @{ */
  1039. BOOST_JSON_DECL value& operator=(string_view s);
  1040. BOOST_JSON_DECL value& operator=(char const* s);
  1041. BOOST_JSON_DECL value& operator=(string const& s);
  1042. /** @} */
  1043. /** Assignment.
  1044. The contents of the value are replaced with the
  1045. contents of `s` using move semantics:
  1046. @li If `*other.storage() == *this->storage()`,
  1047. ownership of the underlying memory is transferred
  1048. in constant time, with no possibility of exceptions.
  1049. After assignment, the moved-from string becomes
  1050. empty with its current storage pointer.
  1051. @li If `*other.storage() != *this->storage()`, an
  1052. element-wise copy is performed, which may throw.
  1053. In this case, the moved-from string is not
  1054. changed.
  1055. @par Complexity
  1056. Constant, or linear in the size of `*this` plus `s.size()`.
  1057. @par Exception Safety
  1058. Strong guarantee.
  1059. Calls to `memory_resource::allocate` may throw.
  1060. @param s The string to move-assign from.
  1061. */
  1062. BOOST_JSON_DECL value& operator=(string&& s);
  1063. /** Assignment.
  1064. Replace `*this` with a copy of the array `arr`.
  1065. @par Exception Safety
  1066. Strong guarantee.
  1067. Calls to `memory_resource::allocate` may throw.
  1068. @par Complexity
  1069. Linear in the sum of sizes of `*this` and `arr`
  1070. @param arr The new array.
  1071. */
  1072. BOOST_JSON_DECL value& operator=(array const& arr);
  1073. /** Assignment.
  1074. The contents of the value are replaced with the
  1075. contents of `arr` using move semantics:
  1076. @li If `*arr.storage() == *this->storage()`,
  1077. ownership of the underlying memory is transferred
  1078. in constant time, with no possibility of exceptions.
  1079. After assignment, the moved-from array becomes
  1080. empty with its current storage pointer.
  1081. @li If `*arr.storage() != *this->storage()`, an
  1082. element-wise copy is performed, which may throw.
  1083. In this case, the moved-from array is not
  1084. changed.
  1085. @par Complexity
  1086. Constant, or linear in the size of `*this` plus `arr.size()`.
  1087. @par Exception Safety
  1088. Strong guarantee.
  1089. Calls to `memory_resource::allocate` may throw.
  1090. @param arr The array to move-assign from.
  1091. */
  1092. BOOST_JSON_DECL value& operator=(array&& arr);
  1093. /** Assignment.
  1094. Replace `*this` with a copy of the obect `obj`.
  1095. @par Exception Safety
  1096. Strong guarantee.
  1097. Calls to `memory_resource::allocate` may throw.
  1098. @par Complexity
  1099. Linear in the sum of sizes of `*this` and `obj`
  1100. @param obj The new object.
  1101. */
  1102. BOOST_JSON_DECL value& operator=(object const& obj);
  1103. /** Assignment.
  1104. The contents of the value are replaced with the
  1105. contents of `obj` using move semantics:
  1106. @li If `*obj.storage() == *this->storage()`,
  1107. ownership of the underlying memory is transferred
  1108. in constant time, with no possibility of exceptions.
  1109. After assignment, the moved-from object becomes
  1110. empty with its current storage pointer.
  1111. @li If `*obj.storage() != *this->storage()`, an
  1112. element-wise copy is performed, which may throw.
  1113. In this case, the moved-from object is not
  1114. changed.
  1115. @par Complexity
  1116. Constant, or linear in the size of `*this` plus `obj.size()`.
  1117. @par Exception Safety
  1118. Strong guarantee.
  1119. Calls to `memory_resource::allocate` may throw.
  1120. @param obj The object to move-assign from.
  1121. */
  1122. BOOST_JSON_DECL value& operator=(object&& obj);
  1123. //------------------------------------------------------
  1124. //
  1125. // Modifiers
  1126. //
  1127. //------------------------------------------------------
  1128. /** Change the kind to null, discarding the previous contents.
  1129. The value is replaced with a null,
  1130. destroying the previous contents.
  1131. @par Complexity
  1132. Linear in the size of `*this`.
  1133. @par Exception Safety
  1134. No-throw guarantee.
  1135. */
  1136. void
  1137. emplace_null() noexcept
  1138. {
  1139. *this = nullptr;
  1140. }
  1141. /** Return a reference to a `bool`, changing the kind and replacing the contents.
  1142. The value is replaced with a `bool`
  1143. initialized to `false`, destroying the
  1144. previous contents.
  1145. @par Complexity
  1146. Linear in the size of `*this`.
  1147. @par Exception Safety
  1148. No-throw guarantee.
  1149. */
  1150. bool&
  1151. emplace_bool() noexcept
  1152. {
  1153. *this = false;
  1154. return sca_.b;
  1155. }
  1156. /** Return a reference to a `std::int64_t`, changing the kind and replacing the contents.
  1157. The value is replaced with a `std::int64_t`
  1158. initialized to zero, destroying the
  1159. previous contents.
  1160. @par Complexity
  1161. Linear in the size of `*this`.
  1162. @par Exception Safety
  1163. No-throw guarantee.
  1164. */
  1165. std::int64_t&
  1166. emplace_int64() noexcept
  1167. {
  1168. *this = std::int64_t{};
  1169. return sca_.i;
  1170. }
  1171. /** Return a reference to a `std::uint64_t`, changing the kind and replacing the contents.
  1172. The value is replaced with a `std::uint64_t`
  1173. initialized to zero, destroying the
  1174. previous contents.
  1175. @par Complexity
  1176. Linear in the size of `*this`.
  1177. @par Exception Safety
  1178. No-throw guarantee.
  1179. */
  1180. std::uint64_t&
  1181. emplace_uint64() noexcept
  1182. {
  1183. *this = std::uint64_t{};
  1184. return sca_.u;
  1185. }
  1186. /** Return a reference to a `double`, changing the kind and replacing the contents.
  1187. The value is replaced with a `double`
  1188. initialized to zero, destroying the
  1189. previous contents.
  1190. @par Complexity
  1191. Linear in the size of `*this`.
  1192. @par Exception Safety
  1193. No-throw guarantee.
  1194. */
  1195. double&
  1196. emplace_double() noexcept
  1197. {
  1198. *this = double{};
  1199. return sca_.d;
  1200. }
  1201. /** Return a reference to a @ref string, changing the kind and replacing the contents.
  1202. The value is replaced with an empty @ref string
  1203. using the current memory resource, destroying the
  1204. previous contents.
  1205. @par Complexity
  1206. Linear in the size of `*this`.
  1207. @par Exception Safety
  1208. No-throw guarantee.
  1209. */
  1210. BOOST_JSON_DECL
  1211. string&
  1212. emplace_string() noexcept;
  1213. /** Return a reference to an @ref array, changing the kind and replacing the contents.
  1214. The value is replaced with an empty @ref array
  1215. using the current memory resource, destroying the
  1216. previous contents.
  1217. @par Complexity
  1218. Linear in the size of `*this`.
  1219. @par Exception Safety
  1220. No-throw guarantee.
  1221. */
  1222. BOOST_JSON_DECL
  1223. array&
  1224. emplace_array() noexcept;
  1225. /** Return a reference to an @ref object, changing the kind and replacing the contents.
  1226. The contents are replaced with an empty @ref object
  1227. using the current @ref memory_resource. All
  1228. previously obtained iterators and references
  1229. obtained beforehand are invalidated.
  1230. @par Complexity
  1231. Linear in the size of `*this`.
  1232. @par Exception Safety
  1233. No-throw guarantee.
  1234. */
  1235. BOOST_JSON_DECL
  1236. object&
  1237. emplace_object() noexcept;
  1238. /** Swap the given values.
  1239. Exchanges the contents of this value with another
  1240. value. Ownership of the respective @ref memory_resource
  1241. objects is not transferred:
  1242. @li If `*other.storage() == *this->storage()`,
  1243. ownership of the underlying memory is swapped in
  1244. constant time, with no possibility of exceptions.
  1245. All iterators and references remain valid.
  1246. @li If `*other.storage() != *this->storage()`,
  1247. the contents are logically swapped by making copies,
  1248. which can throw. In this case all iterators and
  1249. references are invalidated.
  1250. @par Complexity
  1251. Constant or linear in the sum of the sizes of
  1252. the values.
  1253. @par Exception Safety
  1254. Strong guarantee.
  1255. Calls to `memory_resource::allocate` may throw.
  1256. @param other The value to swap with.
  1257. If `this == &other`, this function call has no effect.
  1258. */
  1259. BOOST_JSON_DECL
  1260. void
  1261. swap(value& other);
  1262. /** Swap the given values.
  1263. Exchanges the contents of value `lhs` with
  1264. another value `rhs`. Ownership of the respective
  1265. @ref memory_resource objects is not transferred.
  1266. @li If `*lhs.storage() == *rhs.storage()`,
  1267. ownership of the underlying memory is swapped in
  1268. constant time, with no possibility of exceptions.
  1269. All iterators and references remain valid.
  1270. @li If `*lhs.storage() != *rhs.storage`,
  1271. the contents are logically swapped by a copy,
  1272. which can throw. In this case all iterators and
  1273. references are invalidated.
  1274. @par Effects
  1275. @code
  1276. lhs.swap( rhs );
  1277. @endcode
  1278. @par Complexity
  1279. Constant or linear in the sum of the sizes of
  1280. the values.
  1281. @par Exception Safety
  1282. Strong guarantee.
  1283. Calls to `memory_resource::allocate` may throw.
  1284. @param lhs The value to exchange.
  1285. @param rhs The value to exchange.
  1286. If `&lhs == &rhs`, this function call has no effect.
  1287. @see @ref value::swap
  1288. */
  1289. friend
  1290. void
  1291. swap(value& lhs, value& rhs)
  1292. {
  1293. lhs.swap(rhs);
  1294. }
  1295. //------------------------------------------------------
  1296. //
  1297. // Observers
  1298. //
  1299. //------------------------------------------------------
  1300. /** Returns the kind of this JSON value.
  1301. This function returns the discriminating
  1302. enumeration constant of type @ref json::kind
  1303. corresponding to the underlying representation
  1304. stored in the container.
  1305. @par Complexity
  1306. Constant.
  1307. @par Exception Safety
  1308. No-throw guarantee.
  1309. */
  1310. json::kind
  1311. kind() const noexcept
  1312. {
  1313. return static_cast<json::kind>(
  1314. static_cast<unsigned char>(
  1315. sca_.k) & 0x3f);
  1316. }
  1317. /** Return `true` if this is an array
  1318. This function is used to determine if the underlying
  1319. representation is a certain kind.
  1320. @par Effects
  1321. @code
  1322. return this->kind() == kind::array;
  1323. @endcode
  1324. @par Complexity
  1325. Constant.
  1326. @par Exception Safety
  1327. No-throw guarantee.
  1328. */
  1329. bool
  1330. is_array() const noexcept
  1331. {
  1332. return kind() == json::kind::array;
  1333. }
  1334. /** Return `true` if this is an object
  1335. This function is used to determine if the underlying
  1336. representation is a certain kind.
  1337. @par Effects
  1338. @code
  1339. return this->kind() == kind::object;
  1340. @endcode
  1341. @par Complexity
  1342. Constant.
  1343. @par Exception Safety
  1344. No-throw guarantee.
  1345. */
  1346. bool
  1347. is_object() const noexcept
  1348. {
  1349. return kind() == json::kind::object;
  1350. }
  1351. /** Return `true` if this is a string
  1352. This function is used to determine if the underlying
  1353. representation is a certain kind.
  1354. @par Effects
  1355. @code
  1356. return this->kind() == kind::string;
  1357. @endcode
  1358. @par Complexity
  1359. Constant.
  1360. @par Exception Safety
  1361. No-throw guarantee.
  1362. */
  1363. bool
  1364. is_string() const noexcept
  1365. {
  1366. return kind() == json::kind::string;
  1367. }
  1368. /** Return `true` if this is a signed integer
  1369. This function is used to determine if the underlying
  1370. representation is a certain kind.
  1371. @par Effects
  1372. @code
  1373. return this->kind() == kind::int64;
  1374. @endcode
  1375. @par Complexity
  1376. Constant.
  1377. @par Exception Safety
  1378. No-throw guarantee.
  1379. */
  1380. bool
  1381. is_int64() const noexcept
  1382. {
  1383. return kind() == json::kind::int64;
  1384. }
  1385. /** Return `true` if this is a unsigned integer
  1386. This function is used to determine if the underlying
  1387. representation is a certain kind.
  1388. @par Effects
  1389. @code
  1390. return this->kind() == kind::uint64;
  1391. @endcode
  1392. @par Complexity
  1393. Constant.
  1394. @par Exception Safety
  1395. No-throw guarantee.
  1396. */
  1397. bool
  1398. is_uint64() const noexcept
  1399. {
  1400. return kind() == json::kind::uint64;
  1401. }
  1402. /** Return `true` if this is a double
  1403. This function is used to determine if the underlying
  1404. representation is a certain kind.
  1405. @par Effects
  1406. @code
  1407. return this->kind() == kind::double_;
  1408. @endcode
  1409. @par Complexity
  1410. Constant.
  1411. @par Exception Safety
  1412. No-throw guarantee.
  1413. */
  1414. bool
  1415. is_double() const noexcept
  1416. {
  1417. return kind() == json::kind::double_;
  1418. }
  1419. /** Return `true` if this is a bool
  1420. This function is used to determine if the underlying
  1421. representation is a certain kind.
  1422. @par Effects
  1423. @code
  1424. return this->kind() == kind::bool_;
  1425. @endcode
  1426. @par Complexity
  1427. Constant.
  1428. @par Exception Safety
  1429. No-throw guarantee.
  1430. */
  1431. bool
  1432. is_bool() const noexcept
  1433. {
  1434. return kind() == json::kind::bool_;
  1435. }
  1436. /** Returns true if this is a null.
  1437. This function is used to determine if the underlying
  1438. representation is a certain kind.
  1439. @par Effects
  1440. @code
  1441. return this->kind() == kind::null;
  1442. @endcode
  1443. @par Complexity
  1444. Constant.
  1445. @par Exception Safety
  1446. No-throw guarantee.
  1447. */
  1448. bool
  1449. is_null() const noexcept
  1450. {
  1451. return kind() == json::kind::null;
  1452. }
  1453. /** Returns true if this is an array or object.
  1454. This function returns `true` if
  1455. @ref kind() is either `kind::object` or
  1456. `kind::array`.
  1457. @par Complexity
  1458. Constant.
  1459. @par Exception Safety
  1460. No-throw guarantee.
  1461. */
  1462. bool
  1463. is_structured() const noexcept
  1464. {
  1465. // VFALCO Could use bit 0x20 for this
  1466. return
  1467. kind() == json::kind::object ||
  1468. kind() == json::kind::array;
  1469. }
  1470. /** Returns true if this is not an array or object.
  1471. This function returns `true` if
  1472. @ref kind() is neither `kind::object` nor
  1473. `kind::array`.
  1474. @par Complexity
  1475. Constant.
  1476. @par Exception Safety
  1477. No-throw guarantee.
  1478. */
  1479. bool
  1480. is_primitive() const noexcept
  1481. {
  1482. // VFALCO Could use bit 0x20 for this
  1483. return
  1484. sca_.k != json::kind::object &&
  1485. sca_.k != json::kind::array;
  1486. }
  1487. /** Returns true if this is a number.
  1488. This function returns `true` when
  1489. @ref kind() is one of the following values:
  1490. `kind::int64`, `kind::uint64`, or
  1491. `kind::double_`.
  1492. @par Complexity
  1493. Constant.
  1494. @par Exception Safety
  1495. No-throw guarantee.
  1496. */
  1497. bool
  1498. is_number() const noexcept
  1499. {
  1500. // VFALCO Could use bit 0x40 for this
  1501. return
  1502. kind() == json::kind::int64 ||
  1503. kind() == json::kind::uint64 ||
  1504. kind() == json::kind::double_;
  1505. }
  1506. //------------------------------------------------------
  1507. /** Return an @ref array pointer if this is an array, else return `nullptr`
  1508. If `this->kind() == kind::array`, returns a pointer
  1509. to the underlying array. Otherwise, returns `nullptr`.
  1510. @par Example
  1511. The return value is used in both a boolean context and
  1512. to assign a variable:
  1513. @code
  1514. if( auto p = jv.if_array() )
  1515. return *p;
  1516. @endcode
  1517. @par Complexity
  1518. Constant.
  1519. @par Exception Safety
  1520. No-throw guarantee.
  1521. */
  1522. array const*
  1523. if_array() const noexcept
  1524. {
  1525. if(kind() == json::kind::array)
  1526. return &arr_;
  1527. return nullptr;
  1528. }
  1529. /** Return an @ref array pointer if this is an array, else return `nullptr`
  1530. If `this->kind() == kind::array`, returns a pointer
  1531. to the underlying array. Otherwise, returns `nullptr`.
  1532. @par Example
  1533. The return value is used in both a boolean context and
  1534. to assign a variable:
  1535. @code
  1536. if( auto p = jv.if_array() )
  1537. return *p;
  1538. @endcode
  1539. @par Complexity
  1540. Constant.
  1541. @par Exception Safety
  1542. No-throw guarantee.
  1543. */
  1544. array*
  1545. if_array() noexcept
  1546. {
  1547. if(kind() == json::kind::array)
  1548. return &arr_;
  1549. return nullptr;
  1550. }
  1551. /** Return an @ref object pointer if this is an object, else return `nullptr`
  1552. If `this->kind() == kind::object`, returns a pointer
  1553. to the underlying object. Otherwise, returns `nullptr`.
  1554. @par Example
  1555. The return value is used in both a boolean context and
  1556. to assign a variable:
  1557. @code
  1558. if( auto p = jv.if_object() )
  1559. return *p;
  1560. @endcode
  1561. @par Complexity
  1562. Constant.
  1563. @par Exception Safety
  1564. No-throw guarantee.
  1565. */
  1566. object const*
  1567. if_object() const noexcept
  1568. {
  1569. if(kind() == json::kind::object)
  1570. return &obj_;
  1571. return nullptr;
  1572. }
  1573. /** Return an @ref object pointer if this is an object, else return `nullptr`
  1574. If `this->kind() == kind::object`, returns a pointer
  1575. to the underlying object. Otherwise, returns `nullptr`.
  1576. @par Example
  1577. The return value is used in both a boolean context and
  1578. to assign a variable:
  1579. @code
  1580. if( auto p = jv.if_object() )
  1581. return *p;
  1582. @endcode
  1583. @par Complexity
  1584. Constant.
  1585. @par Exception Safety
  1586. No-throw guarantee.
  1587. */
  1588. object*
  1589. if_object() noexcept
  1590. {
  1591. if(kind() == json::kind::object)
  1592. return &obj_;
  1593. return nullptr;
  1594. }
  1595. /** Return a @ref string pointer if this is a string, else return `nullptr`
  1596. If `this->kind() == kind::string`, returns a pointer
  1597. to the underlying object. Otherwise, returns `nullptr`.
  1598. @par Example
  1599. The return value is used in both a boolean context and
  1600. to assign a variable:
  1601. @code
  1602. if( auto p = jv.if_string() )
  1603. return *p;
  1604. @endcode
  1605. @par Complexity
  1606. Constant.
  1607. @par Exception Safety
  1608. No-throw guarantee.
  1609. */
  1610. string const*
  1611. if_string() const noexcept
  1612. {
  1613. if(kind() == json::kind::string)
  1614. return &str_;
  1615. return nullptr;
  1616. }
  1617. /** Return a @ref string pointer if this is a string, else return `nullptr`
  1618. If `this->kind() == kind::string`, returns a pointer
  1619. to the underlying object. Otherwise, returns `nullptr`.
  1620. @par Example
  1621. The return value is used in both a boolean context and
  1622. to assign a variable:
  1623. @code
  1624. if( auto p = jv.if_string() )
  1625. return *p;
  1626. @endcode
  1627. @par Complexity
  1628. Constant.
  1629. @par Exception Safety
  1630. No-throw guarantee.
  1631. */
  1632. string*
  1633. if_string() noexcept
  1634. {
  1635. if(kind() == json::kind::string)
  1636. return &str_;
  1637. return nullptr;
  1638. }
  1639. /** Return an `int64_t` pointer if this is a signed integer, else return `nullptr`
  1640. If `this->kind() == kind::int64`, returns a pointer
  1641. to the underlying integer. Otherwise, returns `nullptr`.
  1642. @par Example
  1643. The return value is used in both a boolean context and
  1644. to assign a variable:
  1645. @code
  1646. if( auto p = jv.if_int64() )
  1647. return *p;
  1648. @endcode
  1649. @par Complexity
  1650. Constant.
  1651. @par Exception Safety
  1652. No-throw guarantee.
  1653. */
  1654. std::int64_t const*
  1655. if_int64() const noexcept
  1656. {
  1657. if(kind() == json::kind::int64)
  1658. return &sca_.i;
  1659. return nullptr;
  1660. }
  1661. /** Return an `int64_t` pointer if this is a signed integer, else return `nullptr`
  1662. If `this->kind() == kind::int64`, returns a pointer
  1663. to the underlying integer. Otherwise, returns `nullptr`.
  1664. @par Example
  1665. The return value is used in both a boolean context and
  1666. to assign a variable:
  1667. @code
  1668. if( auto p = jv.if_int64() )
  1669. return *p;
  1670. @endcode
  1671. @par Complexity
  1672. Constant.
  1673. @par Exception Safety
  1674. No-throw guarantee.
  1675. */
  1676. std::int64_t*
  1677. if_int64() noexcept
  1678. {
  1679. if(kind() == json::kind::int64)
  1680. return &sca_.i;
  1681. return nullptr;
  1682. }
  1683. /** Return a `uint64_t` pointer if this is an unsigned integer, else return `nullptr`
  1684. If `this->kind() == kind::uint64`, returns a pointer
  1685. to the underlying unsigned integer. Otherwise, returns
  1686. `nullptr`.
  1687. @par Example
  1688. The return value is used in both a boolean context and
  1689. to assign a variable:
  1690. @code
  1691. if( auto p = jv.if_uint64() )
  1692. return *p;
  1693. @endcode
  1694. @par Complexity
  1695. Constant.
  1696. @par Exception Safety
  1697. No-throw guarantee.
  1698. */
  1699. std::uint64_t const*
  1700. if_uint64() const noexcept
  1701. {
  1702. if(kind() == json::kind::uint64)
  1703. return &sca_.u;
  1704. return nullptr;
  1705. }
  1706. /** Return a `uint64_t` pointer if this is an unsigned integer, else return `nullptr`
  1707. If `this->kind() == kind::uint64`, returns a pointer
  1708. to the underlying unsigned integer. Otherwise, returns
  1709. `nullptr`.
  1710. @par Example
  1711. The return value is used in both a boolean context and
  1712. to assign a variable:
  1713. @code
  1714. if( auto p = jv.if_uint64() )
  1715. return *p;
  1716. @endcode
  1717. @par Complexity
  1718. Constant.
  1719. @par Exception Safety
  1720. No-throw guarantee.
  1721. */
  1722. std::uint64_t*
  1723. if_uint64() noexcept
  1724. {
  1725. if(kind() == json::kind::uint64)
  1726. return &sca_.u;
  1727. return nullptr;
  1728. }
  1729. /** Return a `double` pointer if this is a double, else return `nullptr`
  1730. If `this->kind() == kind::double_`, returns a pointer
  1731. to the underlying double. Otherwise, returns
  1732. `nullptr`.
  1733. @par Example
  1734. The return value is used in both a boolean context and
  1735. to assign a variable:
  1736. @code
  1737. if( auto p = jv.if_double() )
  1738. return *p;
  1739. @endcode
  1740. @par Complexity
  1741. Constant.
  1742. @par Exception Safety
  1743. No-throw guarantee.
  1744. */
  1745. double const*
  1746. if_double() const noexcept
  1747. {
  1748. if(kind() == json::kind::double_)
  1749. return &sca_.d;
  1750. return nullptr;
  1751. }
  1752. /** Return a `double` pointer if this is a double, else return `nullptr`
  1753. If `this->kind() == kind::double_`, returns a pointer
  1754. to the underlying double. Otherwise, returns
  1755. `nullptr`.
  1756. @par Example
  1757. The return value is used in both a boolean context and
  1758. to assign a variable:
  1759. @code
  1760. if( auto p = jv.if_double() )
  1761. return *p;
  1762. @endcode
  1763. @par Complexity
  1764. Constant.
  1765. @par Exception Safety
  1766. No-throw guarantee.
  1767. */
  1768. double*
  1769. if_double() noexcept
  1770. {
  1771. if(kind() == json::kind::double_)
  1772. return &sca_.d;
  1773. return nullptr;
  1774. }
  1775. /** Return a `bool` pointer if this is a boolean, else return `nullptr`
  1776. If `this->kind() == kind::bool_`, returns a pointer
  1777. to the underlying boolean. Otherwise, returns
  1778. `nullptr`.
  1779. @par Example
  1780. The return value is used in both a boolean context and
  1781. to assign a variable:
  1782. @code
  1783. if( auto p = jv.if_bool() )
  1784. return *p;
  1785. @endcode
  1786. @par Complexity
  1787. Constant.
  1788. @par Exception Safety
  1789. No-throw guarantee.
  1790. */
  1791. bool const*
  1792. if_bool() const noexcept
  1793. {
  1794. if(kind() == json::kind::bool_)
  1795. return &sca_.b;
  1796. return nullptr;
  1797. }
  1798. /** Return a `bool` pointer if this is a boolean, else return `nullptr`
  1799. If `this->kind() == kind::bool_`, returns a pointer
  1800. to the underlying boolean. Otherwise, returns
  1801. `nullptr`.
  1802. @par Example
  1803. The return value is used in both a boolean context and
  1804. to assign a variable:
  1805. @code
  1806. if( auto p = jv.if_bool() )
  1807. return *p;
  1808. @endcode
  1809. @par Complexity
  1810. Constant.
  1811. @par Exception Safety
  1812. No-throw guarantee.
  1813. */
  1814. bool*
  1815. if_bool() noexcept
  1816. {
  1817. if(kind() == json::kind::bool_)
  1818. return &sca_.b;
  1819. return nullptr;
  1820. }
  1821. //------------------------------------------------------
  1822. /** Return the stored number cast to an arithmetic type.
  1823. This function attempts to return the stored value
  1824. converted to the arithmetic type `T` which may not
  1825. be `bool`:
  1826. @li If `T` is an integral type and the stored
  1827. value is a number which can be losslessly converted,
  1828. the conversion is performed without error and the
  1829. converted number is returned.
  1830. @li If `T` is an integral type and the stored value
  1831. is a number which cannot be losslessly converted,
  1832. then the operation fails with an error.
  1833. @li If `T` is a floating point type and the stored
  1834. value is a number, the conversion is performed
  1835. without error. The converted number is returned,
  1836. with a possible loss of precision.
  1837. @li Otherwise, if the stored value is not a number;
  1838. that is, if `this->is_number()` returns `false`, then
  1839. the operation fails with an error.
  1840. @par Constraints
  1841. @code
  1842. std::is_arithmetic< T >::value && ! std::is_same< T, bool >::value
  1843. @endcode
  1844. @par Complexity
  1845. Constant.
  1846. @par Exception Safety
  1847. No-throw guarantee.
  1848. @return The converted number.
  1849. @param ec Set to the error, if any occurred.
  1850. */
  1851. /** @{ */
  1852. template<class T>
  1853. #ifdef BOOST_JSON_DOCS
  1854. T
  1855. #else
  1856. typename std::enable_if<
  1857. std::is_arithmetic<T>::value &&
  1858. ! std::is_same<T, bool>::value,
  1859. T>::type
  1860. #endif
  1861. to_number(error_code& ec) const noexcept
  1862. {
  1863. error e;
  1864. auto result = to_number<T>(e);
  1865. BOOST_JSON_FAIL(ec, e);
  1866. return result;
  1867. }
  1868. template<class T>
  1869. #ifdef BOOST_JSON_DOCS
  1870. T
  1871. #else
  1872. typename std::enable_if<
  1873. std::is_arithmetic<T>::value &&
  1874. ! std::is_same<T, bool>::value,
  1875. T>::type
  1876. #endif
  1877. to_number(std::error_code& ec) const noexcept
  1878. {
  1879. error_code jec;
  1880. auto result = to_number<T>(jec);
  1881. ec = jec;
  1882. return result;
  1883. }
  1884. /** @} */
  1885. /** Return the stored number cast to an arithmetic type.
  1886. This function attempts to return the stored value
  1887. converted to the arithmetic type `T` which may not
  1888. be `bool`:
  1889. @li If `T` is an integral type and the stored
  1890. value is a number which can be losslessly converted,
  1891. the conversion is performed without error and the
  1892. converted number is returned.
  1893. @li If `T` is an integral type and the stored value
  1894. is a number which cannot be losslessly converted,
  1895. then the operation fails with an error.
  1896. @li If `T` is a floating point type and the stored
  1897. value is a number, the conversion is performed
  1898. without error. The converted number is returned,
  1899. with a possible loss of precision.
  1900. @li Otherwise, if the stored value is not a number;
  1901. that is, if `this->is_number()` returns `false`, then
  1902. the operation fails with an error.
  1903. @par Constraints
  1904. @code
  1905. std::is_arithmetic< T >::value && ! std::is_same< T, bool >::value
  1906. @endcode
  1907. @par Complexity
  1908. Constant.
  1909. @return The converted number.
  1910. @throw system_error on error.
  1911. */
  1912. template<class T>
  1913. #ifdef BOOST_JSON_DOCS
  1914. T
  1915. #else
  1916. typename std::enable_if<
  1917. std::is_arithmetic<T>::value &&
  1918. ! std::is_same<T, bool>::value,
  1919. T>::type
  1920. #endif
  1921. to_number() const
  1922. {
  1923. error_code ec;
  1924. auto result = to_number<T>(ec);
  1925. if(ec)
  1926. detail::throw_system_error( ec );
  1927. return result;
  1928. }
  1929. //------------------------------------------------------
  1930. //
  1931. // Accessors
  1932. //
  1933. //------------------------------------------------------
  1934. /** Return the memory resource associated with the value.
  1935. This returns a pointer to the memory resource
  1936. that was used to construct the value.
  1937. @par Complexity
  1938. Constant.
  1939. @par Exception Safety
  1940. No-throw guarantee.
  1941. */
  1942. storage_ptr const&
  1943. storage() const noexcept
  1944. {
  1945. return sp_;
  1946. }
  1947. /** Return the associated @ref memory_resource
  1948. This function returns an instance of
  1949. @ref polymorphic_allocator constructed from the
  1950. associated @ref memory_resource.
  1951. @par Complexity
  1952. Constant.
  1953. @par Exception Safety
  1954. No-throw guarantee.
  1955. */
  1956. allocator_type
  1957. get_allocator() const noexcept
  1958. {
  1959. return sp_.get();
  1960. }
  1961. //------------------------------------------------------
  1962. /** Return a reference to the underlying `object`, or throw an exception.
  1963. If @ref is_object() is `true`, returns
  1964. a reference to the underlying @ref object,
  1965. otherwise throws an exception.
  1966. @par Complexity
  1967. Constant.
  1968. @par Exception Safety
  1969. Strong guarantee.
  1970. @throw std::invalid_argument `! this->is_object()`
  1971. */
  1972. /* @{ */
  1973. object&
  1974. as_object() &
  1975. {
  1976. if(! is_object())
  1977. detail::throw_invalid_argument( "not an object" );
  1978. return obj_;
  1979. }
  1980. object&&
  1981. as_object() &&
  1982. {
  1983. return std::move( as_object() );
  1984. }
  1985. object const&
  1986. as_object() const&
  1987. {
  1988. if(! is_object())
  1989. detail::throw_invalid_argument( "not an object" );
  1990. return obj_;
  1991. }
  1992. /* @} */
  1993. /** Return a reference to the underlying @ref array, or throw an exception.
  1994. If @ref is_array() is `true`, returns
  1995. a reference to the underlying @ref array,
  1996. otherwise throws an exception.
  1997. @par Complexity
  1998. Constant.
  1999. @par Exception Safety
  2000. Strong guarantee.
  2001. @throw std::invalid_argument `! this->is_array()`
  2002. */
  2003. /* @{ */
  2004. array&
  2005. as_array() &
  2006. {
  2007. if(! is_array())
  2008. detail::throw_invalid_argument( "array required" );
  2009. return arr_;
  2010. }
  2011. array&&
  2012. as_array() &&
  2013. {
  2014. return std::move( as_array() );
  2015. }
  2016. array const&
  2017. as_array() const&
  2018. {
  2019. if(! is_array())
  2020. detail::throw_invalid_argument( "array required" );
  2021. return arr_;
  2022. }
  2023. /* @} */
  2024. /** Return a reference to the underlying `string`, or throw an exception.
  2025. If @ref is_string() is `true`, returns
  2026. a reference to the underlying @ref string,
  2027. otherwise throws an exception.
  2028. @par Complexity
  2029. Constant.
  2030. @par Exception Safety
  2031. Strong guarantee.
  2032. @throw std::invalid_argument `! this->is_string()`
  2033. */
  2034. /* @{ */
  2035. string&
  2036. as_string() &
  2037. {
  2038. if(! is_string())
  2039. detail::throw_invalid_argument( "not a string" );
  2040. return str_;
  2041. }
  2042. string&&
  2043. as_string() &&
  2044. {
  2045. return std::move( as_string() );
  2046. }
  2047. string const&
  2048. as_string() const&
  2049. {
  2050. if(! is_string())
  2051. detail::throw_invalid_argument( "not a string" );
  2052. return str_;
  2053. }
  2054. /* @} */
  2055. /** Return a reference to the underlying `std::int64_t`, or throw an exception.
  2056. If @ref is_int64() is `true`, returns
  2057. a reference to the underlying `std::int64_t`,
  2058. otherwise throws an exception.
  2059. @par Complexity
  2060. Constant.
  2061. @par Exception Safety
  2062. Strong guarantee.
  2063. @throw std::invalid_argument `! this->is_int64()`
  2064. */
  2065. std::int64_t&
  2066. as_int64()
  2067. {
  2068. if(! is_int64())
  2069. detail::throw_invalid_argument( "not an int64" );
  2070. return sca_.i;
  2071. }
  2072. /** Return the underlying `std::int64_t`, or throw an exception.
  2073. If @ref is_int64() is `true`, returns
  2074. the underlying `std::int64_t`,
  2075. otherwise throws an exception.
  2076. @par Complexity
  2077. Constant.
  2078. @par Exception Safety
  2079. Strong guarantee.
  2080. @throw std::invalid_argument `! this->is_int64()`
  2081. */
  2082. std::int64_t
  2083. as_int64() const
  2084. {
  2085. if(! is_int64())
  2086. detail::throw_invalid_argument( "not an int64" );
  2087. return sca_.i;
  2088. }
  2089. /** Return a reference to the underlying `std::uint64_t`, or throw an exception.
  2090. If @ref is_uint64() is `true`, returns
  2091. a reference to the underlying `std::uint64_t`,
  2092. otherwise throws an exception.
  2093. @par Complexity
  2094. Constant.
  2095. @par Exception Safety
  2096. Strong guarantee.
  2097. @throw std::invalid_argument `! this->is_uint64()`
  2098. */
  2099. std::uint64_t&
  2100. as_uint64()
  2101. {
  2102. if(! is_uint64())
  2103. detail::throw_invalid_argument( "not a uint64" );
  2104. return sca_.u;
  2105. }
  2106. /** Return the underlying `std::uint64_t`, or throw an exception.
  2107. If @ref is_int64() is `true`, returns
  2108. the underlying `std::uint64_t`,
  2109. otherwise throws an exception.
  2110. @par Complexity
  2111. Constant.
  2112. @par Exception Safety
  2113. Strong guarantee.
  2114. @throw std::length_error `! this->is_uint64()`
  2115. */
  2116. std::uint64_t
  2117. as_uint64() const
  2118. {
  2119. if(! is_uint64())
  2120. detail::throw_invalid_argument( "not a uint64" );
  2121. return sca_.u;
  2122. }
  2123. /** Return a reference to the underlying `double`, or throw an exception.
  2124. If @ref is_double() is `true`, returns
  2125. a reference to the underlying `double`,
  2126. otherwise throws an exception.
  2127. @par Complexity
  2128. Constant.
  2129. @par Exception Safety
  2130. Strong guarantee.
  2131. @throw std::invalid_argument `! this->is_double()`
  2132. */
  2133. double&
  2134. as_double()
  2135. {
  2136. if(! is_double())
  2137. detail::throw_invalid_argument( "not a double" );
  2138. return sca_.d;
  2139. }
  2140. /** Return the underlying `double`, or throw an exception.
  2141. If @ref is_int64() is `true`, returns
  2142. the underlying `double`,
  2143. otherwise throws an exception.
  2144. @par Complexity
  2145. Constant.
  2146. @par Exception Safety
  2147. Strong guarantee.
  2148. @throw std::invalid_argument `! this->is_double()`
  2149. */
  2150. double
  2151. as_double() const
  2152. {
  2153. if(! is_double())
  2154. detail::throw_invalid_argument( "not a double" );
  2155. return sca_.d;
  2156. }
  2157. /** Return a reference to the underlying `bool`, or throw an exception.
  2158. If @ref is_bool() is `true`, returns
  2159. a reference to the underlying `bool`,
  2160. otherwise throws an exception.
  2161. @par Complexity
  2162. Constant.
  2163. @par Exception Safety
  2164. Strong guarantee.
  2165. @throw std::invalid_argument `! this->is_bool()`
  2166. */
  2167. bool&
  2168. as_bool()
  2169. {
  2170. if(! is_bool())
  2171. detail::throw_invalid_argument( "bool required" );
  2172. return sca_.b;
  2173. }
  2174. /** Return the underlying `bool`, or throw an exception.
  2175. If @ref is_bool() is `true`, returns
  2176. the underlying `bool`,
  2177. otherwise throws an exception.
  2178. @par Complexity
  2179. Constant.
  2180. @par Exception Safety
  2181. Strong guarantee.
  2182. @throw std::invalid_argument `! this->is_bool()`
  2183. */
  2184. bool
  2185. as_bool() const
  2186. {
  2187. if(! is_bool())
  2188. detail::throw_invalid_argument( "bool required" );
  2189. return sca_.b;
  2190. }
  2191. //------------------------------------------------------
  2192. /** Return a reference to the underlying `object`, without checking.
  2193. This is the fastest way to access the underlying
  2194. representation when the kind is known in advance.
  2195. @par Preconditions
  2196. @code
  2197. this->is_object()
  2198. @endcode
  2199. @par Complexity
  2200. Constant.
  2201. @par Exception Safety
  2202. No-throw guarantee.
  2203. */
  2204. /* @{ */
  2205. object&
  2206. get_object() & noexcept
  2207. {
  2208. BOOST_ASSERT(is_object());
  2209. return obj_;
  2210. }
  2211. object&&
  2212. get_object() && noexcept
  2213. {
  2214. BOOST_ASSERT(is_object());
  2215. return std::move(obj_);
  2216. }
  2217. object const&
  2218. get_object() const& noexcept
  2219. {
  2220. BOOST_ASSERT(is_object());
  2221. return obj_;
  2222. }
  2223. /* @} */
  2224. /** Return a reference to the underlying `array`, without checking.
  2225. This is the fastest way to access the underlying
  2226. representation when the kind is known in advance.
  2227. @par Preconditions
  2228. @code
  2229. this->is_array()
  2230. @endcode
  2231. @par Complexity
  2232. Constant.
  2233. @par Exception Safety
  2234. No-throw guarantee.
  2235. */
  2236. /* @{ */
  2237. array&
  2238. get_array() & noexcept
  2239. {
  2240. BOOST_ASSERT(is_array());
  2241. return arr_;
  2242. }
  2243. array&&
  2244. get_array() && noexcept
  2245. {
  2246. BOOST_ASSERT(is_array());
  2247. return std::move(arr_);
  2248. }
  2249. array const&
  2250. get_array() const& noexcept
  2251. {
  2252. BOOST_ASSERT(is_array());
  2253. return arr_;
  2254. }
  2255. /* @} */
  2256. /** Return a reference to the underlying `string`, without checking.
  2257. This is the fastest way to access the underlying
  2258. representation when the kind is known in advance.
  2259. @par Preconditions
  2260. @code
  2261. this->is_string()
  2262. @endcode
  2263. @par Complexity
  2264. Constant.
  2265. @par Exception Safety
  2266. No-throw guarantee.
  2267. */
  2268. /* @{ */
  2269. string&
  2270. get_string() & noexcept
  2271. {
  2272. BOOST_ASSERT(is_string());
  2273. return str_;
  2274. }
  2275. string&&
  2276. get_string() && noexcept
  2277. {
  2278. BOOST_ASSERT(is_string());
  2279. return std::move(str_);
  2280. }
  2281. string const&
  2282. get_string() const& noexcept
  2283. {
  2284. BOOST_ASSERT(is_string());
  2285. return str_;
  2286. }
  2287. /* @} */
  2288. /** Return a reference to the underlying `std::int64_t`, without checking.
  2289. This is the fastest way to access the underlying
  2290. representation when the kind is known in advance.
  2291. @par Preconditions
  2292. @code
  2293. this->is_int64()
  2294. @endcode
  2295. @par Complexity
  2296. Constant.
  2297. @par Exception Safety
  2298. No-throw guarantee.
  2299. */
  2300. std::int64_t&
  2301. get_int64() noexcept
  2302. {
  2303. BOOST_ASSERT(is_int64());
  2304. return sca_.i;
  2305. }
  2306. /** Return the underlying `std::int64_t`, without checking.
  2307. This is the fastest way to access the underlying
  2308. representation when the kind is known in advance.
  2309. @par Preconditions
  2310. @code
  2311. this->is_int64()
  2312. @endcode
  2313. @par Complexity
  2314. Constant.
  2315. @par Exception Safety
  2316. No-throw guarantee.
  2317. */
  2318. std::int64_t
  2319. get_int64() const noexcept
  2320. {
  2321. BOOST_ASSERT(is_int64());
  2322. return sca_.i;
  2323. }
  2324. /** Return a reference to the underlying `std::uint64_t`, without checking.
  2325. This is the fastest way to access the underlying
  2326. representation when the kind is known in advance.
  2327. @par Preconditions
  2328. @code
  2329. this->is_uint64()
  2330. @endcode
  2331. @par Complexity
  2332. Constant.
  2333. @par Exception Safety
  2334. No-throw guarantee.
  2335. */
  2336. std::uint64_t&
  2337. get_uint64() noexcept
  2338. {
  2339. BOOST_ASSERT(is_uint64());
  2340. return sca_.u;
  2341. }
  2342. /** Return the underlying `std::uint64_t`, without checking.
  2343. This is the fastest way to access the underlying
  2344. representation when the kind is known in advance.
  2345. @par Preconditions
  2346. @code
  2347. this->is_uint64()
  2348. @endcode
  2349. @par Complexity
  2350. Constant.
  2351. @par Exception Safety
  2352. No-throw guarantee.
  2353. */
  2354. std::uint64_t
  2355. get_uint64() const noexcept
  2356. {
  2357. BOOST_ASSERT(is_uint64());
  2358. return sca_.u;
  2359. }
  2360. /** Return a reference to the underlying `double`, without checking.
  2361. This is the fastest way to access the underlying
  2362. representation when the kind is known in advance.
  2363. @par Preconditions
  2364. @code
  2365. this->is_double()
  2366. @endcode
  2367. @par Complexity
  2368. Constant.
  2369. @par Exception Safety
  2370. No-throw guarantee.
  2371. */
  2372. double&
  2373. get_double() noexcept
  2374. {
  2375. BOOST_ASSERT(is_double());
  2376. return sca_.d;
  2377. }
  2378. /** Return the underlying `double`, without checking.
  2379. This is the fastest way to access the underlying
  2380. representation when the kind is known in advance.
  2381. @par Preconditions
  2382. @code
  2383. this->is_double()
  2384. @endcode
  2385. @par Complexity
  2386. Constant.
  2387. @par Exception Safety
  2388. No-throw guarantee.
  2389. */
  2390. double
  2391. get_double() const noexcept
  2392. {
  2393. BOOST_ASSERT(is_double());
  2394. return sca_.d;
  2395. }
  2396. /** Return a reference to the underlying `bool`, without checking.
  2397. This is the fastest way to access the underlying
  2398. representation when the kind is known in advance.
  2399. @par Preconditions
  2400. @code
  2401. this->is_bool()
  2402. @endcode
  2403. @par Complexity
  2404. Constant.
  2405. @par Exception Safety
  2406. No-throw guarantee.
  2407. */
  2408. bool&
  2409. get_bool() noexcept
  2410. {
  2411. BOOST_ASSERT(is_bool());
  2412. return sca_.b;
  2413. }
  2414. /** Return the underlying `bool`, without checking.
  2415. This is the fastest way to access the underlying
  2416. representation when the kind is known in advance.
  2417. @par Preconditions
  2418. @code
  2419. this->is_bool()
  2420. @endcode
  2421. @par Complexity
  2422. Constant.
  2423. @par Exception Safety
  2424. No-throw guarantee.
  2425. */
  2426. bool
  2427. get_bool() const noexcept
  2428. {
  2429. BOOST_ASSERT(is_bool());
  2430. return sca_.b;
  2431. }
  2432. //------------------------------------------------------
  2433. /** Access an element, with bounds checking.
  2434. This function is used to access elements of
  2435. the underlying object, or throw an exception
  2436. if the value is not an object.
  2437. @par Complexity
  2438. Constant.
  2439. @par Exception Safety
  2440. Strong guarantee.
  2441. @param key The key of the element to find.
  2442. @return `this->as_object().at( key )`.
  2443. */
  2444. /** @{ */
  2445. value&
  2446. at(string_view key) &
  2447. {
  2448. return as_object().at(key);
  2449. }
  2450. value&&
  2451. at(string_view key) &&
  2452. {
  2453. return std::move( as_object() ).at(key);
  2454. }
  2455. value const&
  2456. at(string_view key) const&
  2457. {
  2458. return as_object().at(key);
  2459. }
  2460. /** @} */
  2461. /** Access an element, with bounds checking.
  2462. This function is used to access elements of
  2463. the underlying array, or throw an exception
  2464. if the value is not an array.
  2465. @par Complexity
  2466. Constant.
  2467. @par Exception Safety
  2468. Strong guarantee.
  2469. @param pos A zero-based array index.
  2470. @return `this->as_array().at( pos )`.
  2471. */
  2472. /** @{ */
  2473. value &
  2474. at(std::size_t pos) &
  2475. {
  2476. return as_array().at(pos);
  2477. }
  2478. value&&
  2479. at(std::size_t pos) &&
  2480. {
  2481. return std::move( as_array() ).at(pos);
  2482. }
  2483. value const&
  2484. at(std::size_t pos) const&
  2485. {
  2486. return as_array().at(pos);
  2487. }
  2488. /** @} */
  2489. /** Access an element via JSON Pointer.
  2490. This function is used to access a (potentially nested)
  2491. element of the value using a JSON Pointer string.
  2492. @par Complexity
  2493. Linear in the sizes of `ptr` and underlying array, object, or string.
  2494. @par Exception Safety
  2495. Strong guarantee.
  2496. @param ptr JSON Pointer string.
  2497. @return reference to the element identified by `ptr`.
  2498. @throw system_error if an error occurs.
  2499. @see
  2500. <a href="https://datatracker.ietf.org/doc/html/rfc6901">
  2501. RFC 6901 - JavaScript Object Notation (JSON) Pointer</a>
  2502. */
  2503. /** @{ */
  2504. BOOST_JSON_DECL
  2505. value const&
  2506. at_pointer(string_view ptr) const&;
  2507. inline
  2508. value&&
  2509. at_pointer(string_view ptr) &&;
  2510. inline
  2511. value&
  2512. at_pointer(string_view ptr) &;
  2513. /** @} */
  2514. /** Access an element via JSON Pointer.
  2515. This function is used to access a (potentially nested)
  2516. element of the value using a JSON Pointer string.
  2517. @par Complexity
  2518. Linear in the sizes of `ptr` and underlying array, object, or string.
  2519. @par Exception Safety
  2520. No-throw guarantee.
  2521. @param ptr JSON Pointer string.
  2522. @param ec Set to the error, if any occurred.
  2523. @return pointer to the element identified by `ptr`.
  2524. @see
  2525. <a href="https://datatracker.ietf.org/doc/html/rfc6901">
  2526. RFC 6901 - JavaScript Object Notation (JSON) Pointer</a>
  2527. */
  2528. /** @{ */
  2529. BOOST_JSON_DECL
  2530. value const*
  2531. find_pointer(string_view ptr, error_code& ec) const noexcept;
  2532. BOOST_JSON_DECL
  2533. value*
  2534. find_pointer(string_view ptr, error_code& ec) noexcept;
  2535. BOOST_JSON_DECL
  2536. value const*
  2537. find_pointer(string_view ptr, std::error_code& ec) const noexcept;
  2538. BOOST_JSON_DECL
  2539. value*
  2540. find_pointer(string_view ptr, std::error_code& ec) noexcept;
  2541. /** @} */
  2542. //------------------------------------------------------
  2543. /** Set an element via JSON Pointer.
  2544. This function is used to insert or assign to a potentially nested
  2545. element of the value using a JSON Pointer string. The function may
  2546. create intermediate elements corresponding to pointer segments.
  2547. <br/>
  2548. The particular conditions when and what kind of intermediate element
  2549. is created is governed by the `ptr` parameter.
  2550. Each pointer token is considered in sequence. For each token
  2551. - if the containing value is an @ref object, then a new `null`
  2552. element is created with key equal to unescaped token string; otherwise
  2553. - if the containing value is an @ref array, and the token represents a
  2554. past-the-end marker, then a `null` element is appended to the array;
  2555. otherwise
  2556. - if the containing value is an @ref array, and the token represents a
  2557. number, then if the difference between the number and array's size
  2558. is smaller than `opts.max_created_elements`, then the size of the
  2559. array is increased, so that the number can reference an element in the
  2560. array; otherwise
  2561. - if the containing value is of different @ref kind and
  2562. `opts.replace_any_scalar` is `true`, or the value is `null`, then
  2563. - if `opts.create_arrays` is `true` and the token either represents
  2564. past-the-end marker or a number, then the value is replaced with
  2565. an empty array and the token is considered again; otherwise
  2566. - if `opts.create_objects` is `true`, then the value is replaced
  2567. with an empty object and the token is considered again; otherwise
  2568. - an error is produced.
  2569. @par Complexity
  2570. Linear in the sum of size of `ptr`, size of underlying array, object,
  2571. or string and `opts.max_created_elements`.
  2572. @par Exception Safety
  2573. Basic guarantee.
  2574. Calls to `memory_resource::allocate` may throw.
  2575. @param sv JSON Pointer string.
  2576. @param ref The value to assign to pointed element.
  2577. @param opts The options for the algorithm.
  2578. @return Reference to the element identified by `ptr`.
  2579. @see @ref set_pointer_options,
  2580. <a href="https://datatracker.ietf.org/doc/html/rfc6901">
  2581. RFC 6901 - JavaScript Object Notation (JSON) Pointer</a>.
  2582. */
  2583. BOOST_JSON_DECL
  2584. value&
  2585. set_at_pointer(
  2586. string_view sv,
  2587. value_ref ref,
  2588. set_pointer_options const& opts = {} );
  2589. /** Set an element via JSON Pointer.
  2590. This function is used to insert or assign to a potentially nested
  2591. element of the value using a JSON Pointer string. The function may
  2592. create intermediate elements corresponding to pointer segments.
  2593. <br/>
  2594. The particular conditions when and what kind of intermediate element
  2595. is created is governed by the `ptr` parameter.
  2596. Each pointer token is considered in sequence. For each token
  2597. - if the containing value is an @ref object, then a new `null`
  2598. element is created with key equal to unescaped token string;
  2599. otherwise
  2600. - if the containing value is an @ref array, and the token represents a
  2601. past-the-end marker, then a `null` element is appended to the array;
  2602. otherwise
  2603. - if the containing value is an @ref array, and the token represents a
  2604. number, then if the difference between the number and array's size
  2605. is smaller than `opts.max_created_elements`, then the size of the
  2606. array is increased, so that the number can reference an element in the
  2607. array; otherwise
  2608. - if the containing value is of different @ref kind and
  2609. `opts.replace_any_scalar` is `true`, or the value is `null`, then
  2610. - if `opts.create_arrays` is `true` and the token either represents
  2611. past-the-end marker or a number, then the value is replaced with
  2612. an empty array and the token is considered again; otherwise
  2613. - if `opts.create_objects` is `true`, then the value is replaced
  2614. with an empty object and the token is considered again; otherwise
  2615. - an error is produced.
  2616. @par Complexity
  2617. Linear in the sum of size of `ptr`, size of underlying array, object,
  2618. or string and `opts.max_created_elements`.
  2619. @par Exception Safety
  2620. Basic guarantee.
  2621. Calls to `memory_resource::allocate` may throw.
  2622. @param sv JSON Pointer string.
  2623. @param ref The value to assign to pointed element.
  2624. @param ec Set to the error, if any occurred.
  2625. @param opts The options for the algorithm.
  2626. @return Pointer to the element identified by `ptr`.
  2627. @see @ref set_pointer_options,
  2628. <a href="https://datatracker.ietf.org/doc/html/rfc6901">
  2629. RFC 6901 - JavaScript Object Notation (JSON) Pointer</a>.
  2630. */
  2631. /** @{ */
  2632. BOOST_JSON_DECL
  2633. value*
  2634. set_at_pointer(
  2635. string_view sv,
  2636. value_ref ref,
  2637. error_code& ec,
  2638. set_pointer_options const& opts = {} );
  2639. BOOST_JSON_DECL
  2640. value*
  2641. set_at_pointer(
  2642. string_view sv,
  2643. value_ref ref,
  2644. std::error_code& ec,
  2645. set_pointer_options const& opts = {} );
  2646. /** @} */
  2647. //------------------------------------------------------
  2648. /** Return `true` if two values are equal.
  2649. Two values are equal when they are the
  2650. same kind and their referenced values
  2651. are equal, or when they are both integral
  2652. types and their integral representations
  2653. are equal.
  2654. @par Complexity
  2655. Constant or linear in the size of
  2656. the array, object, or string.
  2657. @par Exception Safety
  2658. No-throw guarantee.
  2659. */
  2660. // inline friend speeds up overload resolution
  2661. friend
  2662. bool
  2663. operator==(
  2664. value const& lhs,
  2665. value const& rhs) noexcept
  2666. {
  2667. return lhs.equal(rhs);
  2668. }
  2669. /** Return `true` if two values are not equal.
  2670. Two values are equal when they are the
  2671. same kind and their referenced values
  2672. are equal, or when they are both integral
  2673. types and their integral representations
  2674. are equal.
  2675. @par Complexity
  2676. Constant or linear in the size of
  2677. the array, object, or string.
  2678. @par Exception Safety
  2679. No-throw guarantee.
  2680. */
  2681. friend
  2682. bool
  2683. operator!=(
  2684. value const& lhs,
  2685. value const& rhs) noexcept
  2686. {
  2687. return ! (lhs == rhs);
  2688. }
  2689. /** Serialize @ref value to an output stream.
  2690. This function serializes a `value` as JSON into the output stream.
  2691. @return Reference to `os`.
  2692. @par Complexity
  2693. Constant or linear in the size of `jv`.
  2694. @par Exception Safety
  2695. Strong guarantee.
  2696. Calls to `memory_resource::allocate` may throw.
  2697. @param os The output stream to serialize to.
  2698. @param jv The value to serialize.
  2699. */
  2700. BOOST_JSON_DECL
  2701. friend
  2702. std::ostream&
  2703. operator<<(
  2704. std::ostream& os,
  2705. value const& jv);
  2706. /** Parse @ref value from an input stream.
  2707. This function parses JSON from an input stream into a `value`. If
  2708. parsing fails, `std::ios_base::failbit` will be set for `is` and
  2709. `jv` will be left unchanged. Regardless of whether `skipws` flag is set
  2710. on `is`, consumes whitespace before and after JSON, because whitespace
  2711. is considered a part of JSON. Behaves as [_FormattedInputFunction_]
  2712. (https://en.cppreference.com/w/cpp/named_req/FormattedInputFunction).<br>
  2713. Note: this operator cannot assume that the stream only contains a
  2714. single JSON document, which may result in **very underwhelming
  2715. performance**, if the stream isn't cooperative. If you know that your
  2716. input consists of a single JSON document, consider using @ref parse
  2717. function instead.
  2718. @return Reference to `is`.
  2719. @par Complexity
  2720. Linear in the size of JSON data.
  2721. @par Exception Safety
  2722. Basic guarantee.
  2723. Calls to `memory_resource::allocate` may throw.
  2724. The stream may throw as configured by
  2725. [`std::ios::exceptions`](https://en.cppreference.com/w/cpp/io/basic_ios/exceptions).
  2726. @param is The input stream to parse from.
  2727. @param jv The value to parse into.
  2728. @see @ref parse.
  2729. */
  2730. BOOST_JSON_DECL
  2731. friend
  2732. std::istream&
  2733. operator>>(
  2734. std::istream& is,
  2735. value& jv);
  2736. /** Helper for `boost::hash` support
  2737. Computes a hash value for `jv`. This function is used by
  2738. `boost::hash<value>`. Similar overloads for @ref array, @ref object,
  2739. and @ref string do not exist, because those types are supported by
  2740. `boost::hash` out of the box.
  2741. @return hash value for `jv`.
  2742. @param jv `value` for which a hash is to be computed.
  2743. @see [Boost.ContainerHash](https://boost.org/libs/container_hash).
  2744. */
  2745. #ifndef BOOST_JSON_DOCS
  2746. template<
  2747. class T,
  2748. typename std::enable_if<
  2749. std::is_same< detail::remove_cvref<T>, value >::value >::type*
  2750. = nullptr>
  2751. friend
  2752. std::size_t
  2753. hash_value( T const& jv ) noexcept
  2754. #else
  2755. friend
  2756. inline
  2757. std::size_t
  2758. hash_value( value const& jv ) noexcept
  2759. #endif
  2760. {
  2761. return detail::hash_value_impl(jv);
  2762. }
  2763. private:
  2764. static
  2765. void
  2766. relocate(
  2767. value* dest,
  2768. value const& src) noexcept
  2769. {
  2770. std::memcpy(
  2771. static_cast<void*>(dest),
  2772. &src,
  2773. sizeof(src));
  2774. }
  2775. BOOST_JSON_DECL
  2776. storage_ptr
  2777. destroy() noexcept;
  2778. BOOST_JSON_DECL
  2779. bool
  2780. equal(value const& other) const noexcept;
  2781. template<class T>
  2782. auto
  2783. to_number(error& e) const noexcept ->
  2784. typename std::enable_if<
  2785. std::is_signed<T>::value &&
  2786. ! std::is_floating_point<T>::value,
  2787. T>::type
  2788. {
  2789. if(sca_.k == json::kind::int64)
  2790. {
  2791. auto const i = sca_.i;
  2792. if( i >= (std::numeric_limits<T>::min)() &&
  2793. i <= (std::numeric_limits<T>::max)())
  2794. {
  2795. e = {};
  2796. return static_cast<T>(i);
  2797. }
  2798. e = error::not_exact;
  2799. }
  2800. else if(sca_.k == json::kind::uint64)
  2801. {
  2802. auto const u = sca_.u;
  2803. if(u <= static_cast<std::uint64_t>((
  2804. std::numeric_limits<T>::max)()))
  2805. {
  2806. e = {};
  2807. return static_cast<T>(u);
  2808. }
  2809. e = error::not_exact;
  2810. }
  2811. else if(sca_.k == json::kind::double_)
  2812. {
  2813. auto const d = sca_.d;
  2814. if( d >= static_cast<double>(
  2815. (detail::to_number_limit<T>::min)()) &&
  2816. d <= static_cast<double>(
  2817. (detail::to_number_limit<T>::max)()) &&
  2818. static_cast<T>(d) == d)
  2819. {
  2820. e = {};
  2821. return static_cast<T>(d);
  2822. }
  2823. e = error::not_exact;
  2824. }
  2825. else
  2826. {
  2827. e = error::not_number;
  2828. }
  2829. return T{};
  2830. }
  2831. template<class T>
  2832. auto
  2833. to_number(error& e) const noexcept ->
  2834. typename std::enable_if<
  2835. std::is_unsigned<T>::value &&
  2836. ! std::is_same<T, bool>::value,
  2837. T>::type
  2838. {
  2839. if(sca_.k == json::kind::int64)
  2840. {
  2841. auto const i = sca_.i;
  2842. if( i >= 0 && static_cast<std::uint64_t>(i) <=
  2843. (std::numeric_limits<T>::max)())
  2844. {
  2845. e = {};
  2846. return static_cast<T>(i);
  2847. }
  2848. e = error::not_exact;
  2849. }
  2850. else if(sca_.k == json::kind::uint64)
  2851. {
  2852. auto const u = sca_.u;
  2853. if(u <= (std::numeric_limits<T>::max)())
  2854. {
  2855. e = {};
  2856. return static_cast<T>(u);
  2857. }
  2858. e = error::not_exact;
  2859. }
  2860. else if(sca_.k == json::kind::double_)
  2861. {
  2862. auto const d = sca_.d;
  2863. if( d >= 0 &&
  2864. d <= (detail::to_number_limit<T>::max)() &&
  2865. static_cast<T>(d) == d)
  2866. {
  2867. e = {};
  2868. return static_cast<T>(d);
  2869. }
  2870. e = error::not_exact;
  2871. }
  2872. else
  2873. {
  2874. e = error::not_number;
  2875. }
  2876. return T{};
  2877. }
  2878. template<class T>
  2879. auto
  2880. to_number(error& e) const noexcept ->
  2881. typename std::enable_if<
  2882. std::is_floating_point<
  2883. T>::value, T>::type
  2884. {
  2885. if(sca_.k == json::kind::int64)
  2886. {
  2887. e = {};
  2888. return static_cast<T>(sca_.i);
  2889. }
  2890. if(sca_.k == json::kind::uint64)
  2891. {
  2892. e = {};
  2893. return static_cast<T>(sca_.u);
  2894. }
  2895. if(sca_.k == json::kind::double_)
  2896. {
  2897. e = {};
  2898. return static_cast<T>(sca_.d);
  2899. }
  2900. e = error::not_number;
  2901. return {};
  2902. }
  2903. };
  2904. // Make sure things are as big as we think they should be
  2905. #if BOOST_JSON_ARCH == 64
  2906. BOOST_STATIC_ASSERT(sizeof(value) == 24);
  2907. #elif BOOST_JSON_ARCH == 32
  2908. BOOST_STATIC_ASSERT(sizeof(value) == 16);
  2909. #else
  2910. # error Unknown architecture
  2911. #endif
  2912. //----------------------------------------------------------
  2913. /** A key/value pair.
  2914. This is the type of element used by the @ref object
  2915. container.
  2916. */
  2917. class key_value_pair
  2918. {
  2919. #ifndef BOOST_JSON_DOCS
  2920. friend struct detail::access;
  2921. using access = detail::access;
  2922. #endif
  2923. BOOST_JSON_DECL
  2924. static char const empty_[1];
  2925. inline
  2926. key_value_pair(
  2927. pilfered<json::value> k,
  2928. pilfered<json::value> v) noexcept;
  2929. public:
  2930. /// Copy assignment (deleted).
  2931. key_value_pair&
  2932. operator=(key_value_pair const&) = delete;
  2933. /** Destructor.
  2934. The value is destroyed and all internally
  2935. allocated memory is freed.
  2936. */
  2937. ~key_value_pair() noexcept
  2938. {
  2939. auto const& sp = value_.storage();
  2940. if(sp.is_not_shared_and_deallocate_is_trivial())
  2941. return;
  2942. if(key_ == empty_)
  2943. return;
  2944. sp->deallocate(const_cast<char*>(key_),
  2945. len_ + 1, alignof(char));
  2946. }
  2947. /** Copy constructor.
  2948. This constructs a key/value pair with a
  2949. copy of another key/value pair, using
  2950. the same memory resource as `other`.
  2951. @par Exception Safety
  2952. Strong guarantee.
  2953. Calls to `memory_resource::allocate` may throw.
  2954. @param other The key/value pair to copy.
  2955. */
  2956. key_value_pair(
  2957. key_value_pair const& other)
  2958. : key_value_pair(other,
  2959. other.storage())
  2960. {
  2961. }
  2962. /** Copy constructor.
  2963. This constructs a key/value pair with a
  2964. copy of another key/value pair, using
  2965. the specified memory resource.
  2966. @par Exception Safety
  2967. Strong guarantee.
  2968. Calls to `memory_resource::allocate` may throw.
  2969. @param other The key/value pair to copy.
  2970. @param sp A pointer to the @ref memory_resource
  2971. to use. The element will acquire shared
  2972. ownership of the memory resource.
  2973. */
  2974. BOOST_JSON_DECL
  2975. key_value_pair(
  2976. key_value_pair const& other,
  2977. storage_ptr sp);
  2978. /** Move constructor.
  2979. The pair is constructed by acquiring
  2980. ownership of the contents of `other` and
  2981. shared ownership of `other`'s memory resource.
  2982. @note
  2983. After construction, the moved-from pair holds an
  2984. empty key, and a null value with its current
  2985. storage pointer.
  2986. @par Complexity
  2987. Constant.
  2988. @par Exception Safety
  2989. No-throw guarantee.
  2990. @param other The pair to move.
  2991. */
  2992. key_value_pair(
  2993. key_value_pair&& other) noexcept
  2994. : value_(std::move(other.value_))
  2995. , key_(detail::exchange(
  2996. other.key_, empty_))
  2997. , len_(detail::exchange(
  2998. other.len_, 0))
  2999. {
  3000. }
  3001. /** Pilfer constructor.
  3002. The pair is constructed by acquiring ownership
  3003. of the contents of `other` using pilfer semantics.
  3004. This is more efficient than move construction, when
  3005. it is known that the moved-from object will be
  3006. immediately destroyed afterwards.
  3007. @par Complexity
  3008. Constant.
  3009. @par Exception Safety
  3010. No-throw guarantee.
  3011. @param other The value to pilfer. After pilfer
  3012. construction, `other` is not in a usable state
  3013. and may only be destroyed.
  3014. @see @ref pilfer,
  3015. <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0308r0.html">
  3016. Valueless Variants Considered Harmful</a>
  3017. */
  3018. key_value_pair(
  3019. pilfered<key_value_pair> other) noexcept
  3020. : value_(pilfer(other.get().value_))
  3021. , key_(detail::exchange(
  3022. other.get().key_, empty_))
  3023. , len_(detail::exchange(
  3024. other.get().len_, 0))
  3025. {
  3026. }
  3027. /** Constructor.
  3028. This constructs a key/value pair.
  3029. @par Exception Safety
  3030. Strong guarantee.
  3031. Calls to `memory_resource::allocate` may throw.
  3032. @param key The key string to use.
  3033. @param args Optional arguments forwarded to
  3034. the @ref value constructor.
  3035. */
  3036. template<class... Args>
  3037. explicit
  3038. key_value_pair(
  3039. string_view key,
  3040. Args&&... args)
  3041. : value_(std::forward<Args>(args)...)
  3042. {
  3043. if(key.size() > string::max_size())
  3044. detail::throw_length_error( "key too large" );
  3045. auto s = reinterpret_cast<
  3046. char*>(value_.storage()->
  3047. allocate(key.size() + 1, alignof(char)));
  3048. std::memcpy(s, key.data(), key.size());
  3049. s[key.size()] = 0;
  3050. key_ = s;
  3051. len_ = static_cast<
  3052. std::uint32_t>(key.size());
  3053. }
  3054. /** Constructor.
  3055. This constructs a key/value pair. A
  3056. copy of the specified value is made,
  3057. using the specified memory resource.
  3058. @par Exception Safety
  3059. Strong guarantee.
  3060. Calls to `memory_resource::allocate` may throw.
  3061. @param p A `std::pair` with the key
  3062. string and @ref value to construct with.
  3063. @param sp A pointer to the @ref memory_resource
  3064. to use. The element will acquire shared
  3065. ownership of the memory resource.
  3066. */
  3067. explicit
  3068. key_value_pair(
  3069. std::pair<
  3070. string_view,
  3071. json::value> const& p,
  3072. storage_ptr sp = {})
  3073. : key_value_pair(
  3074. p.first,
  3075. p.second,
  3076. std::move(sp))
  3077. {
  3078. }
  3079. /** Constructor.
  3080. This constructs a key/value pair.
  3081. Ownership of the specified value is
  3082. transferred by move construction.
  3083. @par Exception Safety
  3084. Strong guarantee.
  3085. Calls to `memory_resource::allocate` may throw.
  3086. @param p A `std::pair` with the key
  3087. string and @ref value to construct with.
  3088. @param sp A pointer to the @ref memory_resource
  3089. to use. The element will acquire shared
  3090. ownership of the memory resource.
  3091. */
  3092. explicit
  3093. key_value_pair(
  3094. std::pair<
  3095. string_view,
  3096. json::value>&& p,
  3097. storage_ptr sp = {})
  3098. : key_value_pair(
  3099. p.first,
  3100. std::move(p).second,
  3101. std::move(sp))
  3102. {
  3103. }
  3104. /** Return the associated memory resource.
  3105. This returns a pointer to the memory
  3106. resource used to construct the value.
  3107. @par Complexity
  3108. Constant.
  3109. @par Exception Safety
  3110. No-throw guarantee.
  3111. */
  3112. storage_ptr const&
  3113. storage() const noexcept
  3114. {
  3115. return value_.storage();
  3116. }
  3117. /** Return the key of this element.
  3118. After construction, the key may
  3119. not be modified.
  3120. @par Complexity
  3121. Constant.
  3122. @par Exception Safety
  3123. No-throw guarantee.
  3124. */
  3125. string_view const
  3126. key() const noexcept
  3127. {
  3128. return { key_, len_ };
  3129. }
  3130. /** Return the key of this element as a null-terminated string.
  3131. @par Complexity
  3132. Constant.
  3133. @par Exception Safety
  3134. No-throw guarantee.
  3135. */
  3136. char const*
  3137. key_c_str() const noexcept
  3138. {
  3139. return key_;
  3140. }
  3141. /** Return the value of this element.
  3142. @par Complexity
  3143. Constant.
  3144. @par Exception Safety
  3145. No-throw guarantee.
  3146. */
  3147. /* @{ */
  3148. json::value const&
  3149. value() const& noexcept
  3150. {
  3151. return value_;
  3152. }
  3153. json::value&&
  3154. value() && noexcept
  3155. {
  3156. return std::move( value() );
  3157. }
  3158. json::value&
  3159. value() & noexcept
  3160. {
  3161. return value_;
  3162. }
  3163. /* @} */
  3164. private:
  3165. json::value value_;
  3166. char const* key_;
  3167. std::uint32_t len_;
  3168. std::uint32_t next_;
  3169. };
  3170. //----------------------------------------------------------
  3171. #ifdef BOOST_JSON_DOCS
  3172. /** Tuple-like element access.
  3173. This overload permits the key and value
  3174. of a `key_value_pair` to be accessed
  3175. by index. For example:
  3176. @code
  3177. key_value_pair kvp("num", 42);
  3178. string_view key = get<0>(kvp);
  3179. value& jv = get<1>(kvp);
  3180. @endcode
  3181. @par Structured Bindings
  3182. When using C++17 or greater, objects of type
  3183. @ref key_value_pair may be used to initialize
  3184. structured bindings:
  3185. @code
  3186. key_value_pair kvp("num", 42);
  3187. auto& [key, value] = kvp;
  3188. @endcode
  3189. Depending on the value of `I`, the return type will be:
  3190. @li `string_view const` if `I == 0`, or
  3191. @li `value&`, `value const&`, or `value&&` if `I == 1`.
  3192. Any other value for `I` is ill-formed.
  3193. @tparam I The element index to access.
  3194. @par Constraints
  3195. `std::is_same_v< std::remove_cvref_t<T>, key_value_pair >`
  3196. @return `kvp.key()` if `I == 0`, or `kvp.value()`
  3197. if `I == 1`.
  3198. @param kvp The @ref key_value_pair object
  3199. to access.
  3200. */
  3201. template<
  3202. std::size_t I,
  3203. class T>
  3204. __see_below__
  3205. get(T&& kvp) noexcept;
  3206. #else
  3207. template<std::size_t I>
  3208. auto
  3209. get(key_value_pair const&) noexcept ->
  3210. typename std::conditional<I == 0,
  3211. string_view const,
  3212. value const&>::type
  3213. {
  3214. static_assert(I == 0,
  3215. "key_value_pair index out of range");
  3216. }
  3217. template<std::size_t I>
  3218. auto
  3219. get(key_value_pair&) noexcept ->
  3220. typename std::conditional<I == 0,
  3221. string_view const,
  3222. value&>::type
  3223. {
  3224. static_assert(I == 0,
  3225. "key_value_pair index out of range");
  3226. }
  3227. template<std::size_t I>
  3228. auto
  3229. get(key_value_pair&&) noexcept ->
  3230. typename std::conditional<I == 0,
  3231. string_view const,
  3232. value&&>::type
  3233. {
  3234. static_assert(I == 0,
  3235. "key_value_pair index out of range");
  3236. }
  3237. /** Extracts a key_value_pair's key using tuple-like interface
  3238. */
  3239. template<>
  3240. inline
  3241. string_view const
  3242. get<0>(key_value_pair const& kvp) noexcept
  3243. {
  3244. return kvp.key();
  3245. }
  3246. /** Extracts a key_value_pair's key using tuple-like interface
  3247. */
  3248. template<>
  3249. inline
  3250. string_view const
  3251. get<0>(key_value_pair& kvp) noexcept
  3252. {
  3253. return kvp.key();
  3254. }
  3255. /** Extracts a key_value_pair's key using tuple-like interface
  3256. */
  3257. template<>
  3258. inline
  3259. string_view const
  3260. get<0>(key_value_pair&& kvp) noexcept
  3261. {
  3262. return kvp.key();
  3263. }
  3264. /** Extracts a key_value_pair's value using tuple-like interface
  3265. */
  3266. template<>
  3267. inline
  3268. value const&
  3269. get<1>(key_value_pair const& kvp) noexcept
  3270. {
  3271. return kvp.value();
  3272. }
  3273. /** Extracts a key_value_pair's value using tuple-like interface
  3274. */
  3275. template<>
  3276. inline
  3277. value&
  3278. get<1>(key_value_pair& kvp) noexcept
  3279. {
  3280. return kvp.value();
  3281. }
  3282. /** Extracts a key_value_pair's value using tuple-like interface
  3283. */
  3284. template<>
  3285. inline
  3286. value&&
  3287. get<1>(key_value_pair&& kvp) noexcept
  3288. {
  3289. return std::move(kvp.value());
  3290. }
  3291. #endif
  3292. } // namespace json
  3293. } // namespace boost
  3294. #ifdef __clang__
  3295. # pragma clang diagnostic push
  3296. # pragma clang diagnostic ignored "-Wmismatched-tags"
  3297. #endif
  3298. #ifndef BOOST_JSON_DOCS
  3299. namespace std {
  3300. /** Tuple-like size access for key_value_pair
  3301. */
  3302. template<>
  3303. struct tuple_size< ::boost::json::key_value_pair >
  3304. : std::integral_constant<std::size_t, 2>
  3305. {
  3306. };
  3307. /** Tuple-like access for the key type of key_value_pair
  3308. */
  3309. template<>
  3310. struct tuple_element<0, ::boost::json::key_value_pair>
  3311. {
  3312. using type = ::boost::json::string_view const;
  3313. };
  3314. /** Tuple-like access for the value type of key_value_pair
  3315. */
  3316. template<>
  3317. struct tuple_element<1, ::boost::json::key_value_pair>
  3318. {
  3319. using type = ::boost::json::value&;
  3320. };
  3321. /** Tuple-like access for the value type of key_value_pair
  3322. */
  3323. template<>
  3324. struct tuple_element<1, ::boost::json::key_value_pair const>
  3325. {
  3326. using type = ::boost::json::value const&;
  3327. };
  3328. } // std
  3329. #endif
  3330. // std::hash specialization
  3331. #ifndef BOOST_JSON_DOCS
  3332. namespace std {
  3333. template <>
  3334. struct hash< ::boost::json::value > {
  3335. BOOST_JSON_DECL
  3336. std::size_t
  3337. operator()(::boost::json::value const& jv) const noexcept;
  3338. };
  3339. } // std
  3340. #endif
  3341. #ifdef __clang__
  3342. # pragma clang diagnostic pop
  3343. #endif
  3344. // These are here because value, array,
  3345. // and object form cyclic references.
  3346. #include <boost/json/detail/impl/array.hpp>
  3347. #include <boost/json/impl/array.hpp>
  3348. #include <boost/json/impl/object.hpp>
  3349. #include <boost/json/impl/value.hpp>
  3350. // These must come after array and object
  3351. #include <boost/json/impl/value_ref.hpp>
  3352. #endif