Compare commits

..
2 Commits
3 changed files with 59 additions and 7 deletions
+6
View File
@@ -9,3 +9,9 @@ Utility library for C++.
- Formatting library - Formatting library
- JSON parsing - JSON parsing
- Unit test macros - Unit test macros
** Formatting library
The format specification has been heavily inspired by the ~{fmt}~ library, most of the [[https://fmt.dev/latest/syntax.html#format-specification-mini-language][mini-language]] is supported.
*Note*: Development was previously done at: [[https://github.com/riyyi/manafiles/tree/6f0e3d6063ab75ad81899135689569e440ddb813/src/util][manafiles/src/util]] and [[https://github.com/riyyi/manafiles/tree/6f0e3d6063ab75ad81899135689569e440ddb813/test][manafiles/test]].
+25 -3
View File
@@ -7,7 +7,8 @@
#pragma once #pragma once
#include <algorithm> // transform #include <algorithm> // transform
#include <cstddef> // nullptr_t #include <cstddef> // nullptr_t, size_t
#include <cstdint> // int32_t, int64_t, uint32_t
#include <map> #include <map>
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
@@ -45,10 +46,31 @@ void fromJson(const Json& json, bool& boolean)
} }
template<typename Json> template<typename Json>
void fromJson(const Json& json, int& number) void fromJson(const Json& json, int32_t& number)
{ {
VERIFY(json.type() == Json::Type::Number); VERIFY(json.type() == Json::Type::Number);
number = (int)json.asDouble(); number = static_cast<int32_t>(json.asDouble());
}
template<typename Json>
void fromJson(const Json& json, uint32_t& number)
{
VERIFY(json.type() == Json::Type::Number);
number = static_cast<uint32_t>(json.asDouble());
}
template<typename Json>
void fromJson(const Json& json, int64_t& number)
{
VERIFY(json.type() == Json::Type::Number);
number = static_cast<int64_t>(json.asDouble());
}
template<typename Json>
void fromJson(const Json& json, size_t& number) // uint64_t
{
VERIFY(json.type() == Json::Type::Number);
number = static_cast<size_t>(json.asDouble());
} }
template<typename Json> template<typename Json>
+28 -4
View File
@@ -6,8 +6,8 @@
#pragma once #pragma once
#include <cassert> // assert #include <cstddef> // nullptr_t, size_t
#include <cstddef> // nullptr_t #include <cstdint> // int32_t, int64_t, uint32_t
#include <map> #include <map>
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
@@ -31,11 +31,35 @@ struct jsonConstructor {
} }
template<typename Json> template<typename Json>
static void construct(Json& json, int number) static void construct(Json& json, int32_t number)
{ {
json.destroy(); json.destroy();
json.m_type = Json::Type::Number; json.m_type = Json::Type::Number;
json.m_value.number = (double)number; json.m_value.number = static_cast<double>(number);
}
template<typename Json>
static void construct(Json& json, int64_t number)
{
json.destroy();
json.m_type = Json::Type::Number;
json.m_value.number = static_cast<double>(number);
}
template<typename Json>
static void construct(Json& json, uint32_t number)
{
json.destroy();
json.m_type = Json::Type::Number;
json.m_value.number = static_cast<double>(number);
}
template<typename Json>
static void construct(Json& json, size_t number)
{
json.destroy();
json.m_type = Json::Type::Number;
json.m_value.number = static_cast<double>(number);
} }
template<typename Json> template<typename Json>