From c6e072bfe685826cfb8dc3958c9d4eccbad82de0 Mon Sep 17 00:00:00 2001 From: David Siegel Date: Sat, 15 Apr 2023 17:47:15 +0200 Subject: [PATCH 1/2] Feature: Add entries to existing tinygltf::Values MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add subscript operator for array and object type tinygltf::Values. Add a method Push(…) to append values to existing arrays. --- tiny_gltf.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tiny_gltf.h b/tiny_gltf.h index 46e3495..3e9b5d4 100644 --- a/tiny_gltf.h +++ b/tiny_gltf.h @@ -411,6 +411,29 @@ class Value { bool operator==(const tinygltf::Value &other) const; + Value & operator[](std::string const& key) { + assert(type_ == OBJECT_TYPE); + return object_value_[key]; + } + + void Push(Value const& val) { + assert(type_ == ARRAY_TYPE); + array_value_.push_back(val); + } + + Value & operator[](size_t index) { + assert(type_ == ARRAY_TYPE); + assert(index < array_value_.size()); + return array_value_[index]; + } + + const Value & operator[](size_t index) const { + assert(type_ == ARRAY_TYPE); + assert(index < array_value_.size()); + return array_value_[index]; + } + + protected: int type_ = NULL_TYPE; From c2cdd6dd2a13b20454dc2f523514ac453fba1c62 Mon Sep 17 00:00:00 2001 From: David Siegel Date: Sat, 15 Apr 2023 18:34:26 +0200 Subject: [PATCH 2/2] Fix style: follow local conventions --- tiny_gltf.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tiny_gltf.h b/tiny_gltf.h index 3e9b5d4..473a9ca 100644 --- a/tiny_gltf.h +++ b/tiny_gltf.h @@ -411,12 +411,12 @@ class Value { bool operator==(const tinygltf::Value &other) const; - Value & operator[](std::string const& key) { + Value & operator[](const std::string &key) { assert(type_ == OBJECT_TYPE); return object_value_[key]; } - void Push(Value const& val) { + void Push(const Value &val) { assert(type_ == ARRAY_TYPE); array_value_.push_back(val); }