From: Soumya AR <soum...@nvidia.com> This patch adds a get_map () method to the JSON object class to provide access to the underlying hash map that stores the JSON key-value pairs.
It also reorganizes the private and public sections of the class to expose the map_t typedef, which is the return type of get_map(). This change is needed to allow traversal of key-value pairs when parsing user-provided JSON tuning data. Additionally, is_a_helper template specializations for json::literal * and const json::literal * were added to make dynamic casting in the next patch easier. This patch was bootstrapped and regtested on aarch64-linux-gnu, no regression. Signed-off-by: Soumya AR <soum...@nvidia.com> gcc/ChangeLog: * json.h (class object): Add get_map () method. (is_a_helper<json::literal *>, is_a_helper<const json::literal *>): New template specializations. --- gcc/json.h | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/gcc/json.h b/gcc/json.h index c706f2a4fe9..1ed4e82182b 100644 --- a/gcc/json.h +++ b/gcc/json.h @@ -147,7 +147,15 @@ class value class object : public value { - public: + typedef hash_map<char *, value *, + simple_hashmap_traits<nofree_string_hash, value *> > + map_t; + map_t m_map; + + /* Keep track of order in which keys were inserted. */ + auto_vec<const char *> m_keys; + +public: ~object (); enum kind get_kind () const final override { return JSON_OBJECT; } @@ -176,6 +184,8 @@ class object : public value value *get (const char *key) const; + const map_t &get_map () const { return m_map; } + void set_string (const char *key, const char *utf8_value); void set_integer (const char *key, long v); void set_float (const char *key, double v); @@ -189,14 +199,6 @@ class object : public value const char *get_key (size_t i) const { return m_keys[i]; } std::unique_ptr<object> clone_as_object () const; - - private: - typedef hash_map <char *, value *, - simple_hashmap_traits<nofree_string_hash, value *> > map_t; - map_t m_map; - - /* Keep track of order in which keys were inserted. */ - auto_vec <const char *> m_keys; }; /* Subclass of value for arrays. */ @@ -415,6 +417,26 @@ is_a_helper <const json::string *>::test (const json::value *jv) return jv->get_kind () == json::JSON_STRING; } +template <> +template <> +inline bool +is_a_helper<json::literal *>::test (json::value *jv) +{ + return jv->get_kind () == json::JSON_TRUE + || jv->get_kind () == json::JSON_FALSE + || jv->get_kind () == json::JSON_NULL; +} + +template <> +template <> +inline bool +is_a_helper<const json::literal *>::test (const json::value *jv) +{ + return jv->get_kind () == json::JSON_TRUE + || jv->get_kind () == json::JSON_FALSE + || jv->get_kind () == json::JSON_NULL; +} + #if CHECKING_P namespace selftest { -- 2.44.0