From 24b759b827a67d28a8767127e8afa8839be01d9d Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Thu, 20 Apr 2017 14:22:22 +0900 Subject: [PATCH] add operator for toml::value --- toml/toml.hpp | 91 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 80 insertions(+), 11 deletions(-) diff --git a/toml/toml.hpp b/toml/toml.hpp index 57f8aa6..697e18c 100644 --- a/toml/toml.hpp +++ b/toml/toml.hpp @@ -50,17 +50,17 @@ using Datetime = std::chrono::system_clock::time_point; using Array = std::vector; using Table = std::unordered_map; -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 @@ -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 struct is_toml_array : std::false_type{}; template<> struct is_toml_array : std::true_type {}; template struct is_toml_table : std::false_type{}; @@ -270,7 +275,7 @@ template struct value_traits { constexpr static value_t type_index = detail::check_type(); - 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 type; }; @@ -749,6 +754,70 @@ value::cast() return switch_cast::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() == rhs.cast(); + case value_t::Integer : + return lhs.cast() == rhs.cast(); + case value_t::Float : + return lhs.cast() == rhs.cast(); + case value_t::String : + return lhs.cast() == rhs.cast(); + case value_t::Datetime: + return lhs.cast() == rhs.cast(); + case value_t::Array : + return lhs.cast() == rhs.cast(); + case value_t::Table : + return lhs.cast() == rhs.cast(); + 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() < rhs.cast(); + case value_t::Integer : + return lhs.cast() < rhs.cast(); + case value_t::Float : + return lhs.cast() < rhs.cast(); + case value_t::String : + return lhs.cast() < rhs.cast(); + case value_t::Datetime: + return lhs.cast() < rhs.cast(); + case value_t::Array : + return lhs.cast() < rhs.cast(); + case value_t::Table : + return lhs.cast() < rhs.cast(); + 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(),