c++ - 高效值类型

标签 c++ c++11 boost variant

我想创建高效易用的值类型。 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;
    };

最佳答案

我觉得没问题。

如果您的标准库实现允许为不完整类型实例化容器类,您可以不用递归变体。

我会注意到,由于所有内容都公开地绑定(bind)到基类,所以在不更改(二进制)接口(interface)的情况下,没有任何可以更改的实现。因此,我肯定会内联实现所有成员,这样即使没有 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 代替它,它 promise 这一点,以及非分配构造

² 只需确保您不允许对右值进行操作,以消除可预测的错误类别,例如

template <typename T> optional<T const&> get()&& = delete;

关于c++ - 高效值类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45780791/

相关文章:

c++ - decltype 和 typeof 的区别?

c++ - Boost.MPI 和 Boost.Interprocess 之间有什么区别?

c++ - 类模板的友元函数

C++使用转换(绑定(bind))除数

c++ - 为什么无法在 C++ 中撤消 'using'?

C++ 错误代码 C2784 无法推断模板参数

c# - 从 C++ native 插件更新 float 组

c++ - 如何使用模板函数作为 Boost::Unit-test 的自定义谓词

boost - 使用 Cuda 并行读取多个文本文件

c++ - 选择什么容器