Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to get rid of std::variant in json_container.hpp #7041

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 141 additions & 10 deletions include/util/json_container.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,32 +91,163 @@ struct Null
};

/**
* Typed Value sum-type implemented as a variant able to represent tree-like JSON structures.
* Typed Object.
*
* Dispatch on its type by either by using apply_visitor or its get function.
* Unwrap the key-value pairs holding type via its values member attribute.
*/
using Value = std::variant<String, Number, Object, Array, True, False, Null>;
struct Object;

/**
* Typed Object.
* Typed Array.
*
* Unwrap the key-value pairs holding type via its values member attribute.
* Unwrap the Value holding type via its values member attribute.
*/
struct Array;

struct Value;

// Definitions of Object and Array (must come after Value is defined)
struct Object
{
std::unordered_map<std::string, Value> values;
};

/**
* Typed Array.
*
* Unwrap the Value holding type via its values member attribute.
*/
struct Array
{
std::vector<Value> values;
};

struct Value
{
enum class Type
{
Invalid,
String,
Number,
Object,
Array,
True,
False,
Null
};
String string;
Number number;
Object object;
Array array;
Type type;

Value() noexcept : type(Type::Invalid) {}
Value(const Null &) noexcept : type(Type::Null) {}
Value(const True &) noexcept : type(Type::True) {}
Value(const False &) noexcept : type(Type::False) {}
Value(String &&string_) noexcept : string(std::move(string_)), type(Type::String) {}
Value(Number &&number_) noexcept : number(number_), type(Type::Number) {}
Value(Object &&object_) noexcept : object(std::move(object_)), type(Type::Object) {}
Value(Array &&array_) noexcept : array(std::move(array_)), type(Type::Array) {}
Value(const String &string_) noexcept : string(string_), type(Type::String) {}
Value(const Number &number_) noexcept : number(number_), type(Type::Number) {}
Value(const Object &object_) noexcept : object(object_), type(Type::Object) {}
Value(const Array &array_) noexcept : array(array_), type(Type::Array) {}

Value(double number) noexcept : number(number), type(Type::Number) {}
Value(std::string string) noexcept : string(std::move(string)), type(Type::String) {}
Value(const char *string) noexcept : string(string), type(Type::String) {}
};

} // namespace osrm::util::json

namespace std
{
template <typename T> inline T &get(osrm::util::json::Value &value) noexcept;

template <>
inline osrm::util::json::String &
get<osrm::util::json::String>(osrm::util::json::Value &value) noexcept
{
return value.string;
}

template <>
inline osrm::util::json::Number &
get<osrm::util::json::Number>(osrm::util::json::Value &value) noexcept
{
return value.number;
}

template <>
inline osrm::util::json::Object &
get<osrm::util::json::Object>(osrm::util::json::Value &value) noexcept
{
return value.object;
}

template <>
inline osrm::util::json::Array &
get<osrm::util::json::Array>(osrm::util::json::Value &value) noexcept
{
return value.array;
}

template <typename T> inline const T &get(const osrm::util::json::Value &value) noexcept;

template <>
inline const osrm::util::json::String &
get<osrm::util::json::String>(const osrm::util::json::Value &value) noexcept
{
return value.string;
}

template <>
inline const osrm::util::json::Number &
get<osrm::util::json::Number>(const osrm::util::json::Value &value) noexcept
{
return value.number;
}

template <>
inline const osrm::util::json::Object &
get<osrm::util::json::Object>(const osrm::util::json::Value &value) noexcept
{
return value.object;
}

template <>
inline const osrm::util::json::Array &
get<osrm::util::json::Array>(const osrm::util::json::Value &value) noexcept
{
return value.array;
}

template <typename Visitor>
inline void visit(Visitor &&visitor, const osrm::util::json::Value &value)
{
switch (value.type)
{
case osrm::util::json::Value::Type::String:
visitor(value.string);
break;
case osrm::util::json::Value::Type::Number:
visitor(value.number);
break;
case osrm::util::json::Value::Type::Object:
visitor(value.object);
break;
case osrm::util::json::Value::Type::Array:
visitor(value.array);
break;
case osrm::util::json::Value::Type::True:
visitor(osrm::util::json::True{});
break;
case osrm::util::json::Value::Type::False:
visitor(osrm::util::json::False{});
break;
case osrm::util::json::Value::Type::Null:
visitor(osrm::util::json::Null{});
break;
case osrm::util::json::Value::Type::Invalid:
break;
}
}

} // namespace std
#endif // JSON_CONTAINER_HPP
Loading
Loading