More cleanup

1. Reserve array memory when converting JSON to Value for heap efficiency.
2. Grouped more JSON abstraction functions together and put in anonymous namespace instead of making each static.
This commit is contained in:
jrkoonce 2019-08-29 11:45:04 -05:00
parent 208c3058bf
commit 5cecc41db9

View File

@ -2703,7 +2703,9 @@ bool DecodeDataURI(std::vector<unsigned char> *out, std::string &mime_type,
return true;
}
static bool GetInt(const json&o, int& val)
namespace
{
bool GetInt(const json&o, int& val)
{
#ifdef TINYGLTF_USE_RAPIDJSON
if (!o.IsDouble())
@ -2745,7 +2747,7 @@ static bool GetInt(const json&o, int& val)
#endif
}
static bool GetDouble(const json&o, double& val)
bool GetDouble(const json&o, double& val)
{
#ifdef TINYGLTF_USE_RAPIDJSON
if (o.IsDouble())
@ -2766,7 +2768,7 @@ static bool GetDouble(const json&o, double& val)
#endif
}
static bool GetNumber(const json&o, double& val)
bool GetNumber(const json&o, double& val)
{
#ifdef TINYGLTF_USE_RAPIDJSON
if (o.IsNumber())
@ -2787,7 +2789,7 @@ static bool GetNumber(const json&o, double& val)
#endif
}
static bool GetString(const json&o, std::string& val)
bool GetString(const json&o, std::string& val)
{
#ifdef TINYGLTF_USE_RAPIDJSON
if (o.IsString())
@ -2808,7 +2810,7 @@ static bool GetString(const json&o, std::string& val)
#endif
}
static bool IsArray(const json& o)
bool IsArray(const json& o)
{
#ifdef TINYGLTF_USE_RAPIDJSON
return o.IsArray();
@ -2817,7 +2819,7 @@ static bool IsArray(const json& o)
#endif
}
static json_const_array_iterator ArrayBegin(const json& o)
json_const_array_iterator ArrayBegin(const json& o)
{
#ifdef TINYGLTF_USE_RAPIDJSON
return o.Begin();
@ -2826,7 +2828,7 @@ static json_const_array_iterator ArrayBegin(const json& o)
#endif
}
static json_const_array_iterator ArrayEnd(const json& o)
json_const_array_iterator ArrayEnd(const json& o)
{
#ifdef TINYGLTF_USE_RAPIDJSON
return o.End();
@ -2835,7 +2837,7 @@ static json_const_array_iterator ArrayEnd(const json& o)
#endif
}
static bool IsObject(const json& o)
bool IsObject(const json& o)
{
#ifdef TINYGLTF_USE_RAPIDJSON
return o.IsObject();
@ -2844,7 +2846,7 @@ static bool IsObject(const json& o)
#endif
}
static json_const_iterator ObjectBegin(const json& o)
json_const_iterator ObjectBegin(const json& o)
{
#ifdef TINYGLTF_USE_RAPIDJSON
return o.MemberBegin();
@ -2853,7 +2855,7 @@ static json_const_iterator ObjectBegin(const json& o)
#endif
}
static json_const_iterator ObjectEnd(const json& o)
json_const_iterator ObjectEnd(const json& o)
{
#ifdef TINYGLTF_USE_RAPIDJSON
return o.MemberEnd();
@ -2862,7 +2864,7 @@ static json_const_iterator ObjectEnd(const json& o)
#endif
}
static const char* GetKey(json_const_iterator& it)
const char* GetKey(json_const_iterator& it)
{
#ifdef TINYGLTF_USE_RAPIDJSON
return it->name.GetString();
@ -2871,6 +2873,28 @@ static const char* GetKey(json_const_iterator& it)
#endif
}
bool FindMember(const json& o, const char* member, json_const_iterator& it)
{
#ifdef TINYGLTF_USE_RAPIDJSON
it = o.FindMember(member);
return it != o.MemberEnd();
#else
it = o.find(member);
return it != o.end();
#endif
}
const json& GetValue(json_const_iterator& it)
{
#ifdef TINYGLTF_USE_RAPIDJSON
return it->value;
#else
return it.value();
#endif
}
}
static bool ParseJsonAsValue(Value *ret, const json &o) {
Value val{};
#ifdef TINYGLTF_USE_RAPIDJSON
@ -2887,6 +2911,7 @@ static bool ParseJsonAsValue(Value *ret, const json &o) {
} break;
case Type::kArrayType: {
Value::Array value_array;
value_array.reserve(o.Size());
for (auto it = o.Begin(); it != o.End(); ++it) {
Value entry;
ParseJsonAsValue(&entry, *it);
@ -2933,6 +2958,7 @@ static bool ParseJsonAsValue(Value *ret, const json &o) {
} break;
case json::value_t::array: {
Value::Array value_array;
value_array.reserve(o.size());
for (auto it = o.begin(); it != o.end(); it++) {
Value entry;
ParseJsonAsValue(&entry, it.value());
@ -2964,26 +2990,6 @@ static bool ParseJsonAsValue(Value *ret, const json &o) {
return val.Type() != NULL_TYPE;
}
static bool FindMember(const json& o, const char* member, json_const_iterator& it)
{
#ifdef TINYGLTF_USE_RAPIDJSON
it = o.FindMember(member);
return it != o.MemberEnd();
#else
it = o.find(member);
return it != o.end();
#endif
}
const json& GetValue(json_const_iterator& it)
{
#ifdef TINYGLTF_USE_RAPIDJSON
return it->value;
#else
return it.value();
#endif
}
static bool ParseExtrasProperty(Value *ret, const json &o) {
json_const_iterator it;
if (!FindMember(o, "extras", it)) {