pp_character (pp, '}');
}
+std::unique_ptr<value>
+object::clone () const
+{
+ return clone_as_object ();
+}
+
+std::unique_ptr<object>
+object::clone_as_object () const
+{
+ auto result = std::make_unique<object> ();
+
+ /* Iterate in the order that the keys were inserted. */
+ unsigned i;
+ const char *key;
+ FOR_EACH_VEC_ELT (m_keys, i, key)
+ {
+ map_t &mut_map = const_cast<map_t &> (m_map);
+ value *value = *mut_map.get (key);
+ result->set (key, value->clone ());
+ }
+
+ return result;
+}
+
/* Set the json::value * for KEY, taking ownership of V
(and taking a copy of KEY if necessary). */
pp_character (pp, ']');
}
+std::unique_ptr<value>
+array::clone () const
+{
+ auto result = std::make_unique<array> ();
+ unsigned i;
+ value *v;
+ FOR_EACH_VEC_ELT (m_elements, i, v)
+ result->append (v->clone ());
+ return result;
+}
+
/* Append non-NULL value V to a json::array, taking ownership of V. */
void
pp_string (pp, tmp);
}
+std::unique_ptr<value>
+float_number::clone () const
+{
+ return std::make_unique<float_number> (m_value);
+}
+
/* class json::integer_number, a subclass of json::value, wrapping a long. */
/* Implementation of json::value::print for json::integer_number. */
pp_string (pp, tmp);
}
+std::unique_ptr<value>
+integer_number::clone () const
+{
+ return std::make_unique<integer_number> (m_value);
+}
/* class json::string, a subclass of json::value. */
print_escaped_json_string (pp, m_utf8, m_len);
}
+std::unique_ptr<value>
+string::clone () const
+{
+ return std::make_unique<string> (m_utf8, m_len);
+}
+
/* class json::literal, a subclass of json::value. */
/* Implementation of json::value::print for json::literal. */
}
}
+std::unique_ptr<value>
+literal::clone () const
+{
+ return std::make_unique<literal> (m_kind);
+}
+
\f
#if CHECKING_P
ASSERT_EQ (strcmp (str.get_string (), "foo"), 0);
}
+static void
+test_cloning ()
+{
+ // Objects
+ {
+ object obj;
+ obj.set_string ("foo", "bar");
+
+ auto obj_clone = obj.clone ();
+ ASSERT_JSON_EQ (obj, *obj_clone);
+ }
+
+ // Arrays
+ {
+ array arr;
+ arr.append (std::make_unique<string> ("foo"));
+
+ auto arr_clone = arr.clone ();
+ ASSERT_JSON_EQ (arr, *arr_clone);
+ }
+
+ // float_number
+ {
+ float_number f_one (1.0);
+ auto f_clone = f_one.clone ();
+ ASSERT_JSON_EQ (f_one, *f_clone);
+ }
+
+ // integer_number
+ {
+ integer_number num (42);
+ auto num_clone = num.clone ();
+ ASSERT_JSON_EQ (num, *num_clone);
+ }
+
+ // string
+ {
+ string str ("foo");
+ auto str_clone = str.clone ();
+ ASSERT_JSON_EQ (str, *str_clone);
+ }
+
+ // literal
+ {
+ literal lit (JSON_TRUE);
+ auto lit_clone = lit.clone ();
+ ASSERT_JSON_EQ (lit, *lit_clone);
+ }
+}
+
/* Run all of the selftests within this file. */
void
test_formatting ();
test_comparisons ();
test_strcmp ();
+ test_cloning ();
}
} // namespace selftest
virtual ~value () {}
virtual enum kind get_kind () const = 0;
virtual void print (pretty_printer *pp, bool formatted) const = 0;
+ virtual std::unique_ptr<value> clone () const = 0;
void dump (FILE *, bool formatted) const;
void DEBUG_FUNCTION dump () const;
enum kind get_kind () const final override { return JSON_OBJECT; }
void print (pretty_printer *pp, bool formatted) const final override;
+ std::unique_ptr<value> clone () const final override;
object *dyn_cast_object () final override { return this; }
static int compare (const json::object &obj_a, const json::object &obj_b);
+ std::unique_ptr<object> clone_as_object () const;
+
private:
typedef hash_map <char *, value *,
simple_hashmap_traits<nofree_string_hash, value *> > map_t;
enum kind get_kind () const final override { return JSON_ARRAY; }
void print (pretty_printer *pp, bool formatted) const final override;
+ std::unique_ptr<value> clone () const final override;
void append (value *v);
void append_string (const char *utf8_value);
enum kind get_kind () const final override { return JSON_FLOAT; }
void print (pretty_printer *pp, bool formatted) const final override;
+ std::unique_ptr<value> clone () const final override;
double get () const { return m_value; }
enum kind get_kind () const final override { return JSON_INTEGER; }
void print (pretty_printer *pp, bool formatted) const final override;
+ std::unique_ptr<value> clone () const final override;
long get () const { return m_value; }
enum kind get_kind () const final override { return JSON_STRING; }
void print (pretty_printer *pp, bool formatted) const final override;
+ std::unique_ptr<value> clone () const final override;
const char *get_string () const { return m_utf8; }
size_t get_length () const { return m_len; }
enum kind get_kind () const final override { return m_kind; }
void print (pretty_printer *pp, bool formatted) const final override;
+ std::unique_ptr<value> clone () const final override;
private:
enum kind m_kind;