value.hpp 91 KB

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