From 2269a611da40bd7242dbd1a3204c212ac6091fd7 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 6 Feb 2024 05:22:12 +0200 Subject: Add experimental support for JSON value types --- libbuild2/variable.ixx | 107 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) (limited to 'libbuild2/variable.ixx') diff --git a/libbuild2/variable.ixx b/libbuild2/variable.ixx index 51c35fd..b8f80e3 100644 --- a/libbuild2/variable.ixx +++ b/libbuild2/variable.ixx @@ -906,6 +906,113 @@ namespace build2 new (&v.data_) map (move (x)); } + // json + // + inline bool value_traits:: + empty (const json_value& v) + { + // Note: should be consistent with $json.size(). + // + switch (v.type) + { + case json_type::null: return true; + case json_type::boolean: + case json_type::signed_number: + case json_type::unsigned_number: + case json_type::hexadecimal_number: + case json_type::string: break; + case json_type::array: return v.array.empty (); + case json_type::object: return v.object.empty (); + } + + return false; + } + + inline void value_traits:: + assign (value& v, json_value&& x) + { + if (v) + v.as () = move (x); + else + new (&v.data_) json_value (move (x)); + } + + inline void value_traits:: + append (value& v, json_value&& x) + { + if (v) + v.as ().append (move (x)); + else + new (&v.data_) json_value (move (x)); + } + + inline void value_traits:: + prepend (value& v, json_value&& x) + { + if (v) + v.as ().prepend (move (x)); + else + new (&v.data_) json_value (move (x)); + } + + // json_array + // + inline void value_traits:: + assign (value& v, json_array&& x) + { + if (v) + v.as () = move (x); + else + new (&v.data_) json_array (move (x)); + } + + inline void value_traits:: + append (value& v, json_value&& x) + { + if (!v) + new (&v.data_) json_array (); + + v.as ().append (move (x)); + } + + inline void value_traits:: + prepend (value& v, json_value&& x) + { + if (!v) + new (&v.data_) json_array (); + + v.as ().prepend (move (x)); + } + + // json_object + // + inline void value_traits:: + assign (value& v, json_object&& x) + { + if (v) + v.as () = move (x); + else + new (&v.data_) json_object (move (x)); + } + + inline void value_traits:: + append (value& v, json_value&& x) + { + if (!v) + new (&v.data_) json_object (); + + v.as ().append (move (x)); + } + + inline void value_traits:: + prepend (value& v, json_value&& x) + { + if (!v) + new (&v.data_) json_object (); + + v.as ().prepend (move (x)); + } + // variable_pool // inline const variable* variable_pool:: -- cgit v1.1