我想创建高效且易于使用的值类型。
Value
的基础是boost::variant
(以及将来std::variant
),但我是新的。
我有一些问题:
boost::variant
?也许存在更有效的方式?
class Value;
typedef std::string String;
typedef std::vector<char> BinData;
typedef String URL;
typedef unsigned long long UID;
TSW_STRONG_TYPEDEF(std::time_t, Time)
typedef std::vector<Value> ValueArray;
typedef std::vector<String> StringArray;
//typedef std::pair<String, Value> NameValue;
typedef std::list<Value> ValueList;
typedef std::list<String> StringList;
typedef std::map<String, String> StringStringMap;
typedef std::map<String, Value> NameValueMap;
struct monostate
{
monostate() = default;
};
constexpr bool operator<(monostate, monostate) noexcept { return false; }
constexpr bool operator>(monostate, monostate) noexcept { return false; }
constexpr bool operator<=(monostate, monostate) noexcept { return true; }
constexpr bool operator>=(monostate, monostate) noexcept { return true; }
constexpr bool operator==(monostate, monostate) noexcept { return true; }
constexpr bool operator!=(monostate, monostate) noexcept { return false; }
typedef monostate Null;
class Object
{
public:
Object() = delete;
Object(const Object &other) = default;
Object(Object &&other);
Object(const String &name);
Object(String &&name);
Object(const String &name, const NameValueMap &fields);
Object(String &&name, const NameValueMap &fields);
Object(const String &name, NameValueMap &&fields);
Object(String &&name, NameValueMap &&fields);
Object &operator=(const Object &other) = default;
Object &operator=(Object &&other);
public:
const String &get_name() const;
const NameValueMap &get_fields() const;
public:
bool operator<(const Object &other) const noexcept;
bool operator>(const Object &other) const noexcept;
bool operator<=(const Object &other) const noexcept;
bool operator>=(const Object &other) const noexcept;
bool operator==(const Object &other) const noexcept;
bool operator!=(const Object &other) const noexcept;
private:
String name_;
NameValueMap fields_;
};
enum class ValueType
{
Undefined, Null, Array, BinData, Boolean, DoubleNumber, Int64Number, String, Time, Object
};
// Types ordnung need to be same with ValueType ordnung.
/// Base for the Value class
typedef boost::variant<monostate, Null, ValueArray, BinData, bool, double, int64_t, String, Time, Object> ValueBase;
/**
* @brief The Value class, implements common framework value.
*
* This class is a container, which can store multiple values, including Values containers.
*
* @note
* Class based on a variant class. It may be either boost::variant or std::variant in C++17 and higher.
*/
class Value : public ValueBase
{
public:
using ValueBase::ValueBase;
Value() = default;
Value(const String::value_type *v) : ValueBase(String(v)) {}
public:
bool is_array() const { return static_cast<ValueType>(which()) == ValueType::Array; }
bool is_bool() const { return static_cast<ValueType>(which()) == ValueType::Boolean; }
bool is_bindata() const { return static_cast<ValueType>(which()) == ValueType::BinData; }
bool is_double() const { return static_cast<ValueType>(which()) == ValueType::DoubleNumber; }
bool is_int64() const { return static_cast<ValueType>(which()) == ValueType::Int64Number; }
bool is_null() const { return static_cast<ValueType>(which()) == ValueType::Null; }
bool is_object() const { return static_cast<ValueType>(which()) == ValueType::Object; }
bool is_string() const { return static_cast<ValueType>(which()) == ValueType::String; }
bool is_time() const { return static_cast<ValueType>(which()) == ValueType::Time; }
bool is_undefined() const { return static_cast<ValueType>(which()) == ValueType::Undefined; }
public:
bool as_bool() const { return as<bool>(); }
BinData &as_bindata() { return as<BinData>(); }
double as_double() const { return as<double>(); }
int64_t as_int64() const { return as<int64_t>(); }
Object &as_object() { return as<Object>(); }
String &as_string() { return as<String>(); }
Time &as_time() { return as<Time>(); }
ValueArray &as_array() { return as<ValueArray>(); }
public:
ValueType value_type() const { return static_cast<ValueType>(which()); }
public:
template <typename T>
const T& as() const { return boost::get<T>(*this); }
template <typename T>
T& as() { return boost::get<T>(*this); }
template <typename T>
const T& as(const T& default_value) const { return type() == typeid(T) ? boost::get<T>(*this) : default_value; }
template <typename T>
T& as(const T& default_value) { return type() == typeid(T) ? boost::get<T>(*this) : default_value; }
template <typename T> boost::optional<T> as_optional() { return boost::make_optional(type() == typeid(T), as<T>()); }
public:
bool operator==(const ValueBase &other) const { return ValueBase::operator==(other); }
bool operator<(const ValueBase &other) const { return ValueBase::operator<(other); }
bool operator>(const ValueBase &other) const { return !((*this) < other || (*this) == other); }
bool operator<=(const ValueBase &other) const { return ((*this) < other || (*this) == other); }
bool operator>=(const ValueBase &other) const { return !((*this) < other); }
bool operator!=(const ValueBase &other) const { return !((*this) == other); }
private:
// Force compile error, prevent Variant(bool) to be called
Value(void *) = delete;
};
答案 0 :(得分:2)
对我来说没问题。
您可以不使用递归变体IFF您的标准库实现允许实例化不完整类型的容器类。
我注意到,由于所有内容都公开地与基类相关联,因此没有任何关于可以在不更改(二进制)接口的情况下更改的实现。因此,我肯定会实现所有内联成员,这样即使没有LTO,编译器也会优化它们。
我不清楚to_X
成员做了什么(可能只是a<X>
,但可能还有其他内容取决于can_convert()
?)。如果它只是一个环绕as_<>
我将它们重命名为as_X()
等。
您可能还想添加optional<>
- 像
template <typename T> T const& get_value_or(T const& default_value) const;
可能
template <typename T> optional<T> get() const;
// with boost optional you can prevent a copy²:
template <typename T> optional<T const&> get() const;
这样可以启用以下代码:
if (auto& s = value.get<String>()) {
std::cout << "The string value is '" << *s << "'\n";
} else {
std::cout << "Value has no string value\n";
}
¹这不是 - 尚未 - 标准规定。您总是可以使用Boost Container来承诺这个,以及非分配构造
²只是确保你不允许对右值进行操作,以消除可预测的错误类别,例如。
template <typename T> optional<T const&> get()&& = delete;