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,8 +2703,10 @@ 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())
{
@ -2743,10 +2745,10 @@ static bool GetInt(const json&o, int& val)
return false;
#endif
}
}
static bool GetDouble(const json&o, double& val)
{
bool GetDouble(const json&o, double& val)
{
#ifdef TINYGLTF_USE_RAPIDJSON
if (o.IsDouble())
{
@ -2764,10 +2766,10 @@ static bool GetDouble(const json&o, double& val)
return false;
#endif
}
}
static bool GetNumber(const json&o, double& val)
{
bool GetNumber(const json&o, double& val)
{
#ifdef TINYGLTF_USE_RAPIDJSON
if (o.IsNumber())
{
@ -2785,10 +2787,10 @@ static bool GetNumber(const json&o, double& val)
return false;
#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())
{
@ -2806,69 +2808,91 @@ static bool GetString(const json&o, std::string& val)
return false;
#endif
}
}
static bool IsArray(const json& o)
{
bool IsArray(const json& o)
{
#ifdef TINYGLTF_USE_RAPIDJSON
return o.IsArray();
#else
return o.is_array();
#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();
#else
return o.begin();
#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();
#else
return o.end();
#endif
}
}
static bool IsObject(const json& o)
{
bool IsObject(const json& o)
{
#ifdef TINYGLTF_USE_RAPIDJSON
return o.IsObject();
#else
return o.is_object();
#endif
}
}
static json_const_iterator ObjectBegin(const json& o)
{
json_const_iterator ObjectBegin(const json& o)
{
#ifdef TINYGLTF_USE_RAPIDJSON
return o.MemberBegin();
#else
return o.begin();
#endif
}
}
static json_const_iterator ObjectEnd(const json& o)
{
json_const_iterator ObjectEnd(const json& o)
{
#ifdef TINYGLTF_USE_RAPIDJSON
return o.MemberEnd();
#else
return o.end();
#endif
}
}
static const char* GetKey(json_const_iterator& it)
{
const char* GetKey(json_const_iterator& it)
{
#ifdef TINYGLTF_USE_RAPIDJSON
return it->name.GetString();
#else
return it.key().c_str();
#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) {
@ -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)) {