mirror of
https://github.com/ToruNiina/toml11.git
synced 2025-09-18 18:56:23 +08:00
add operator for toml::value
This commit is contained in:
@@ -50,17 +50,17 @@ using Datetime = std::chrono::system_clock::time_point;
|
||||
using Array = std::vector<value>;
|
||||
using Table = std::unordered_map<key, value>;
|
||||
|
||||
enum class value_t
|
||||
enum class value_t : std::uint8_t
|
||||
{
|
||||
Boolean,
|
||||
Integer,
|
||||
Float,
|
||||
String,
|
||||
Datetime,
|
||||
Array,
|
||||
Table,
|
||||
Empty,
|
||||
Unknown,
|
||||
Boolean = 1,
|
||||
Integer = 2,
|
||||
Float = 3,
|
||||
String = 4,
|
||||
Datetime = 5,
|
||||
Array = 6,
|
||||
Table = 7,
|
||||
Empty = 0,
|
||||
Unknown = 255,
|
||||
};
|
||||
|
||||
template<typename charT, typename traits>
|
||||
@@ -121,6 +121,11 @@ constexpr inline value_t check_type()
|
||||
value_t::Unknown;
|
||||
}
|
||||
|
||||
constexpr inline bool is_valid(value_t vt)
|
||||
{
|
||||
return vt != value_t::Unknown;
|
||||
}
|
||||
|
||||
template<typename T> struct is_toml_array : std::false_type{};
|
||||
template<> struct is_toml_array<toml::Array> : std::true_type {};
|
||||
template<typename T> struct is_toml_table : std::false_type{};
|
||||
@@ -270,7 +275,7 @@ template<typename T>
|
||||
struct value_traits
|
||||
{
|
||||
constexpr static value_t type_index = detail::check_type<T>();
|
||||
constexpr static bool is_toml_type = (type_index != value_t::Unknown);
|
||||
constexpr static bool is_toml_type = detail::is_valid(type_index);
|
||||
typedef typename detail::toml_default_type<type_index>::type type;
|
||||
};
|
||||
|
||||
@@ -749,6 +754,70 @@ value::cast()
|
||||
return switch_cast<T>::invoke(*this);
|
||||
}
|
||||
|
||||
inline bool operator==(const toml::value& lhs, const toml::value& rhs)
|
||||
{
|
||||
if(lhs.type() != rhs.type()) return false;
|
||||
switch(lhs.type())
|
||||
{
|
||||
case value_t::Boolean :
|
||||
return lhs.cast<value_t::Boolean >() == rhs.cast<value_t::Boolean >();
|
||||
case value_t::Integer :
|
||||
return lhs.cast<value_t::Integer >() == rhs.cast<value_t::Integer >();
|
||||
case value_t::Float :
|
||||
return lhs.cast<value_t::Float >() == rhs.cast<value_t::Float >();
|
||||
case value_t::String :
|
||||
return lhs.cast<value_t::String >() == rhs.cast<value_t::String >();
|
||||
case value_t::Datetime:
|
||||
return lhs.cast<value_t::Datetime>() == rhs.cast<value_t::Datetime>();
|
||||
case value_t::Array :
|
||||
return lhs.cast<value_t::Array >() == rhs.cast<value_t::Array >();
|
||||
case value_t::Table :
|
||||
return lhs.cast<value_t::Table >() == rhs.cast<value_t::Table >();
|
||||
case value_t::Empty : return true;
|
||||
case value_t::Unknown : return false;
|
||||
}
|
||||
}
|
||||
inline bool operator<(const toml::value& lhs, const toml::value& rhs)
|
||||
{
|
||||
if(lhs.type() != rhs.type()) return (lhs.type() < rhs.type());
|
||||
switch(lhs.type())
|
||||
{
|
||||
case value_t::Boolean :
|
||||
return lhs.cast<value_t::Boolean >() < rhs.cast<value_t::Boolean >();
|
||||
case value_t::Integer :
|
||||
return lhs.cast<value_t::Integer >() < rhs.cast<value_t::Integer >();
|
||||
case value_t::Float :
|
||||
return lhs.cast<value_t::Float >() < rhs.cast<value_t::Float >();
|
||||
case value_t::String :
|
||||
return lhs.cast<value_t::String >() < rhs.cast<value_t::String >();
|
||||
case value_t::Datetime:
|
||||
return lhs.cast<value_t::Datetime>() < rhs.cast<value_t::Datetime>();
|
||||
case value_t::Array :
|
||||
return lhs.cast<value_t::Array >() < rhs.cast<value_t::Array >();
|
||||
case value_t::Table :
|
||||
return lhs.cast<value_t::Table >() < rhs.cast<value_t::Table >();
|
||||
case value_t::Empty : return false;
|
||||
case value_t::Unknown : return false;
|
||||
}
|
||||
}
|
||||
|
||||
inline bool operator!=(const toml::value& lhs, const toml::value& rhs)
|
||||
{
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
inline bool operator<=(const toml::value& lhs, const toml::value& rhs)
|
||||
{
|
||||
return (lhs < rhs) || (lhs == rhs);
|
||||
}
|
||||
inline bool operator>(const toml::value& lhs, const toml::value& rhs)
|
||||
{
|
||||
return !(lhs <= rhs);
|
||||
}
|
||||
inline bool operator>=(const toml::value& lhs, const toml::value& rhs)
|
||||
{
|
||||
return !(lhs < rhs);
|
||||
}
|
||||
|
||||
/* ------------------------------- to_toml ---------------------------------- */
|
||||
|
||||
template<typename T, toml::value_t vT = toml::detail::check_type<T>(),
|
||||
|
Reference in New Issue
Block a user