template<typename ... Ts>
struct assign_visitor;
-<<<<<<< variant_dev_new
template<typename T>
struct complete_equals_visitor;
template<typename... Ts>
struct partial_equals_visitor;
-=======
->>>>>>> a8c63f3 Tests for Variant serialization
template<class Visitor, class Variant, typename ... Ts>
struct apply_void_visitor;
template<class Visitor, class Variant>
struct apply_void_visitor<Visitor, Variant> {
- static const unsigned int index = 0;
-
- static
- void visit(Visitor&, Variant&) {
- //won't be called
- assert(false);
- throw "";
- }
+ static const unsigned int index = 0;
+
+ static
+ void visit(Visitor&, Variant&) {
+ //won't be called
+ assert(false);
+ throw "";
+ }
};
template<class Visitor, class Variant, typename T, typename ... Ts>
struct apply_void_visitor<Visitor, Variant, T, Ts...> {
- static const unsigned int index = apply_void_visitor<Visitor, Variant,
- Ts...>::index + 1;
-
- static
- void visit(Visitor& visitor, Variant& var) {
- if (var.getValueType() == index) {
- bool b;
- visitor(var.template get<T>(b));
- } else {
- apply_void_visitor<Visitor, Variant, Ts...>::visit(visitor, var);
- }
- }
+ static const unsigned int index = apply_void_visitor<Visitor, Variant,
+ Ts...>::index + 1;
+
+ static
+ void visit(Visitor& visitor, Variant& var) {
+ if (var.getValueType() == index) {
+ bool b;
+ visitor(var.template get<T>(b));
+ } else {
+ apply_void_visitor<Visitor, Variant, Ts...>::visit(visitor, var);
+ }
+ }
};
-<<<<<<< variant_dev_new
template<class Visitor, class Variant, typename ... Ts>
struct apply_return_visitor
template<class Visitor, class Variant>
struct apply_return_visitor<Visitor, Variant> {
- static const unsigned int index = 0;
+ static const unsigned int index = 0;
- static bool visit(Visitor&, Variant&) {
- //won't be called
- assert(false);
- }
+ static bool visit(Visitor&, Variant&) {
+ //won't be called
+ assert(false);
+ }
};
template<class Visitor, class Variant, typename T, typename ... Ts>
struct apply_return_visitor<Visitor, Variant, T, Ts...> {
- static const unsigned int index = apply_return_visitor<Visitor, Variant,
- Ts...>::index + 1;
-
- static bool visit(Visitor& visitor, Variant& var) {
- if (var.getValueType() == index) {
- bool b;
- return visitor(var.template get<T>(b));
- } else {
- return apply_return_visitor<Visitor, Variant, Ts...>::visit(visitor,
- var);
- }
- }
+ static const unsigned int index = apply_return_visitor<Visitor, Variant,
+ Ts...>::index + 1;
+
+ static bool visit(Visitor& visitor, Variant& var) {
+ if (var.getValueType() == index) {
+ bool b;
+ return visitor(var.template get<T>(b));
+ } else {
+ return apply_return_visitor<Visitor, Variant, Ts...>::visit(visitor,
+ var);
+ }
+ }
};
-=======
->>>>>>> a8c63f3 Tests for Variant serialization
template<unsigned int size>
struct clear_visitor {
public:
- clear_visitor(typename std::aligned_storage<size>::type& storage) :
- storage_(storage) {
- }
+ clear_visitor(typename std::aligned_storage<size>::type& storage) :
+ storage_(storage) {
+ }
- template<typename _Type>
- void operator()(const _Type&) const {
- (reinterpret_cast<const _Type *>(&storage_))->~_Type();
- }
+ template<typename _Type>
+ void operator()(const _Type&) const {
+ (reinterpret_cast<const _Type *>(&storage_))->~_Type();
+ }
- private:
- typename std::aligned_storage<size>::type& storage_;
+ private:
+ typename std::aligned_storage<size>::type& storage_;
};
template<typename U, typename ... Ts>
//U == T
template<typename T, typename ... Ts>
struct select_type<T, T, Ts...> {
- typedef T type;
+ typedef T type;
};
//U& == T
template<typename T, typename ... Ts>
struct select_type<T, T&, Ts...> {
- typedef T& type;
+ typedef T& type;
};
//U == T&
template<typename T, typename ... Ts>
struct select_type<T&, T, Ts...> {
- typedef T type;
+ typedef T type;
};
//const U& == T
template<typename T, typename ... Ts>
struct select_type<T, const T&, Ts...> {
- typedef const T& type;
+ typedef const T& type;
};
//U == const T&
template<typename T, typename ... Ts>
struct select_type<const T&, T, Ts...> {
- typedef T type;
+ typedef T type;
};
//U == X*
//T == const X*
template<typename T, typename ... Ts>
struct select_type<T*, const T*, Ts...> {
- typedef const T* type;
+ typedef const T* type;
};
//U == X&
//T == const X&
template<typename T, typename ... Ts>
struct select_type<T&, const T&, Ts...> {
- typedef const T& type;
+ typedef const T& type;
};
//U != T, let's try to find U among Ts
template<typename U, typename T, typename ... Ts>
struct select_type<U, T, Ts...> {
- typedef typename select_type<U, Ts...>::type type;
+ typedef typename select_type<U, Ts...>::type type;
};
template<typename ... Ts>
template<>
struct type_index_getter<> {
- static const unsigned int index = 0;
+ static const unsigned int index = 0;
- template<typename U>
- static
- unsigned int get() {
- return 0;
- }
+ template<typename U>
+ static
+ unsigned int get() {
+ return 0;
+ }
};
template<typename T, typename ... Ts>
struct type_index_getter<T, Ts...> {
- static const unsigned int index = type_index_getter<Ts...>::index + 1;
-
- template<typename U>
- static
- unsigned int get(
- typename std::enable_if<std::is_same<T, U>::value >::type* = 0) {
- return index;
- }
-
- template<typename U>
- static
- unsigned int get(typename std::enable_if<!std::is_same<T, U>::value >::type* = 0) {
- return type_index_getter<Ts...>::template get<U>();
- }
+ static const unsigned int index = type_index_getter<Ts...>::index + 1;
+
+ template<typename U>
+ static
+ unsigned int get(
+ typename std::enable_if<std::is_same<T, U>::value >::type* = 0) {
+ return index;
+ }
+
+ template<typename U>
+ static
+ unsigned int get(typename std::enable_if<!std::is_same<T, U>::value >::type* = 0) {
+ return type_index_getter<Ts...>::template get<U>();
+ }
};
template<typename ... Ts>
template<>
struct max_size<> {
- static const unsigned int value = 0;
+ static const unsigned int value = 0;
};
template<typename T, typename ... Ts>
struct max_size<T, Ts...> {
- static const unsigned int current_type_size = sizeof(T);
- static const unsigned int next_type_size = max_size<Ts...>::value;
- static const unsigned int value =
- current_type_size > next_type_size ?
- current_type_size : next_type_size;
+ static const unsigned int current_type_size = sizeof(T);
+ static const unsigned int next_type_size = max_size<Ts...>::value;
+ static const unsigned int value =
+ current_type_size > next_type_size ?
+ current_type_size : next_type_size;
};
template<typename _SearchType, typename _CurrentType, typename ... _RestTypes>
Variant& operator=(const Variant& rhs)
{
- assign_visitor<_Types...> visitor(*this);
- apply_void_visitor<assign_visitor<_Types...>, Variant<_Types...>, _Types...>::visit(visitor, rhs);
- return *this;
-<<<<<<< variant_dev_new
-=======
- }
-
- /*Variant& operator=(const Variant& fromVariant) {
- // TODO
+ assign_visitor<_Types...> visitor(*this);
+ apply_void_visitor<assign_visitor<_Types...>, Variant<_Types...>, _Types...>::visit(visitor, rhs);
return *this;
->>>>>>> a8c63f3 Tests for Variant serialization
}
-<<<<<<< variant_dev_new
Variant& operator=(Variant&& rhs) {
- assign_visitor<_Types...> visitor(*this);
- apply_void_visitor<assign_visitor<_Types...>, Variant<_Types...>, _Types...>::visit(visitor, rhs);
- return *this;
+ assign_visitor<_Types...> visitor(*this);
+ apply_void_visitor<assign_visitor<_Types...>, Variant<_Types...>, _Types...>::visit(visitor, rhs);
+ return *this;
}
template<typename _Type>
typename std::enable_if<!std::is_same<_Type, Variant<_Types...>>::value, Variant<_Types...>&>::type
operator=(const _Type& value)
{
- set<typename select_type<_Type, _Types...>::type>(value);
- return *this;
-=======
-*/
- Variant& operator=(Variant&& fromVariant) {
- // TODO
+ set<typename select_type<_Type, _Types...>::type>(value);
return *this;
->>>>>>> a8c63f3 Tests for Variant serialization
}
template <typename _Type>
- const bool isType() const {
- typedef typename select_type<_Type, _Types...>::type selected_type_t;
- unsigned int cType = type_index_getter<_Types...>::template get<selected_type_t>();
- if(cType == valueType_) {
- return true;
- } else {
- return false;
- }
- }
+ const bool isType() const {
+ typedef typename select_type<_Type, _Types...>::type selected_type_t;
+ unsigned int cType = type_index_getter<_Types...>::template get<selected_type_t>();
+ if(cType == valueType_) {
+ return true;
+ } else {
+ return false;
+ }
+ }
template <typename _Type>
Variant(const _Type& value,
- typename std::enable_if<!std::is_const<_Type>::value>::type* = 0,
- typename std::enable_if<!std::is_reference<_Type>::value>::type* = 0,
- typename std::enable_if<!std::is_same<_Type, Variant>::value>::type* = 0) {
-<<<<<<< variant_dev_new
-=======
- /*typedef typename select_type<_Type, _Types...>::type selected_type_t;
- valueType_ = type_index_getter<_Types...>::template get<selected_type_t>();
- new (&valueStorage_) _Type(value);*/
->>>>>>> a8c63f3 Tests for Variant serialization
- set<typename select_type<_Type, _Types...>::type>(value, false);
+ typename std::enable_if<!std::is_const<_Type>::value>::type* = 0,
+ typename std::enable_if<!std::is_reference<_Type>::value>::type* = 0,
+ typename std::enable_if<!std::is_same<_Type, Variant>::value>::type* = 0) {
+ set<typename select_type<_Type, _Types...>::type>(value, false);
}
template <typename _Type>
Variant(_Type && value,
- typename std::enable_if<!std::is_const<_Type>::value>::type* = 0,
- typename std::enable_if<!std::is_reference<_Type>::value>::type* = 0,
- typename std::enable_if<!std::is_same<_Type, Variant>::value>::type* = 0) {
-<<<<<<< variant_dev_new
-=======
- /*typedef typename select_type<_Type, _Types...>::type selected_type_t;
- valueType_ = type_index_getter<_Types...>::template get<selected_type_t>();
- new (&valueStorage_) typename std::remove_reference<_Type>::type(std::move(value));*/
->>>>>>> a8c63f3 Tests for Variant serialization
- set2<typename select_type<_Type, _Types...>::type>(std::move(value), false);
+ typename std::enable_if<!std::is_const<_Type>::value>::type* = 0,
+ typename std::enable_if<!std::is_reference<_Type>::value>::type* = 0,
+ typename std::enable_if<!std::is_same<_Type, Variant>::value>::type* = 0) {
+ set2<typename select_type<_Type, _Types...>::type>(std::move(value), false);
}
//TODO: Return type???
- template <typename _Type>
- const typename VariantTypeSelector<_Type, _Types...>::type & get(bool& success) const {
- typedef typename select_type<_Type, _Types...>::type selected_type_t;
- unsigned int cType = type_index_getter<_Types...>::template get<selected_type_t>();
- if(cType == valueType_) {
- success = true;
- return *(reinterpret_cast<const _Type *>(&valueStorage_));
- } else {
- success = false;
- return *(reinterpret_cast<const _Type *>(&valueStorage_));
- }
- }
-
- inline size_t getValueType() const {
- return valueType_;
- }
-
- template<typename U>
- void set( const U& value, const bool clear) {
- typedef typename select_type<U, _Types...>::type selected_type_t;
-
- const selected_type_t& type_value = value;
- if(clear) {
- clear_visitor<maxSize> visitor(valueStorage_);
- apply_void_visitor<clear_visitor<maxSize>, Variant<_Types...>, _Types...>::visit(visitor, *this);
- }
- new (&valueStorage_) selected_type_t(std::move(value));
- valueType_ = type_index_getter<_Types...>::template get<selected_type_t>();
- }
-
- template<typename U>
- void set2( U&& value, const bool clear) {
- typedef typename select_type<U, _Types...>::type selected_type_t;
-
- selected_type_t&& any_container_value = std::move(value);
- if(clear)
- {
- clear_visitor<maxSize> visitor(valueStorage_);
- apply_void_visitor<clear_visitor<maxSize>, Variant<_Types...>, _Types...>::visit(visitor, *this);
- } else {
- new (&valueStorage_) selected_type_t(std::move(any_container_value));
- }
-
- valueType_ = type_index_getter<_Types...>::template get<selected_type_t>();
- }
+ template <typename _Type>
+ const typename VariantTypeSelector<_Type, _Types...>::type & get(bool& success) const {
+ typedef typename select_type<_Type, _Types...>::type selected_type_t;
+ unsigned int cType = type_index_getter<_Types...>::template get<selected_type_t>();
+ if(cType == valueType_) {
+ success = true;
+ return *(reinterpret_cast<const _Type *>(&valueStorage_));
+ } else {
+ success = false;
+ return *(reinterpret_cast<const _Type *>(&valueStorage_));
+ }
+ }
+
+ inline size_t getValueType() const {
+ return valueType_;
+ }
+
+ template<typename U>
+ void set( const U& value, const bool clear) {
+ typedef typename select_type<U, _Types...>::type selected_type_t;
+
+ const selected_type_t& type_value = value;
+ if(clear) {
+ clear_visitor<maxSize> visitor(valueStorage_);
+ apply_void_visitor<clear_visitor<maxSize>, Variant<_Types...>, _Types...>::visit(visitor, *this);
+ }
+ new (&valueStorage_) selected_type_t(std::move(value));
+ valueType_ = type_index_getter<_Types...>::template get<selected_type_t>();
+ }
+
+ template<typename U>
+ void set2( U&& value, const bool clear) {
+ typedef typename select_type<U, _Types...>::type selected_type_t;
+
+ selected_type_t&& any_container_value = std::move(value);
+ if(clear)
+ {
+ clear_visitor<maxSize> visitor(valueStorage_);
+ apply_void_visitor<clear_visitor<maxSize>, Variant<_Types...>, _Types...>::visit(visitor, *this);
+ } else {
+ new (&valueStorage_) selected_type_t(std::move(any_container_value));
+ }
+
+ valueType_ = type_index_getter<_Types...>::template get<selected_type_t>();
+ }
private:
inline bool hasValue() const {
template<typename... _Types>
bool operator==(const Variant<_Types...>& lhs, const Variant<_Types...>& rhs)
{
- partial_equals_visitor<_Types...> visitor(lhs);
- return apply_return_visitor<partial_equals_visitor<_Types...>, const Variant<_Types...>, _Types...>::visit(visitor, rhs);
+ partial_equals_visitor<_Types...> visitor(lhs);
+ return apply_return_visitor<partial_equals_visitor<_Types...>, const Variant<_Types...>, _Types...>::visit(visitor, rhs);
}
template<typename... _Types>
bool operator!=(const Variant<_Types...>& lhs, const Variant<_Types...>& rhs)
{
- return !(lhs == rhs);
+ return !(lhs == rhs);
}
template<typename T>
struct complete_equals_visitor
{
- public:
- complete_equals_visitor(const T& rhs):
- rhs_(rhs)
- {
- }
-
- bool
- operator()(const T& lhs) const
- {
- return lhs == rhs_;
- }
-
- template<typename U>
- bool
- operator()(const U&) const
- {
- return false;
- }
-
- private:
- const T& rhs_;
+ public:
+ complete_equals_visitor(const T& rhs):
+ rhs_(rhs)
+ {
+ }
+
+ bool
+ operator()(const T& lhs) const
+ {
+ return lhs == rhs_;
+ }
+
+ template<typename U>
+ bool
+ operator()(const U&) const
+ {
+ return false;
+ }
+
+ private:
+ const T& rhs_;
};
template<typename... Ts>
struct partial_equals_visitor
{
- public:
- partial_equals_visitor(const Variant<Ts...>& lhs):
- lhs_(lhs)
- {
- }
-
- template<typename T>
- bool
- operator()(const T& rhs) const
- {
- complete_equals_visitor<T> visitor(rhs);
- return apply_return_visitor<complete_equals_visitor<T>, const Variant<Ts...>, Ts...>::visit(visitor, lhs_);
- }
-
- private:
- const Variant<Ts...>& lhs_;
+ public:
+ partial_equals_visitor(const Variant<Ts...>& lhs):
+ lhs_(lhs)
+ {
+ }
+
+ template<typename T>
+ bool
+ operator()(const T& rhs) const
+ {
+ complete_equals_visitor<T> visitor(rhs);
+ return apply_return_visitor<complete_equals_visitor<T>, const Variant<Ts...>, Ts...>::visit(visitor, lhs_);
+ }
+
+ private:
+ const Variant<Ts...>& lhs_;
};
template<typename ... Ts>
struct assign_visitor {
public:
- assign_visitor(Variant<Ts...>& lhs, const bool clear = true) :
- lhs_(lhs), clear_(clear) {
- }
+ assign_visitor(Variant<Ts...>& lhs, const bool clear = true) :
+ lhs_(lhs), clear_(clear) {
+ }
- template<typename T>
- void operator()(const T& value) const {
- lhs_.template set<T>(value, clear_);
- }
+ template<typename T>
+ void operator()(const T& value) const {
+ lhs_.template set<T>(value, clear_);
+ }
- template<typename T>
- void operator()(T& value) const {
- lhs_.template set<T>(value, clear_);
- }
+ template<typename T>
+ void operator()(T& value) const {
+ lhs_.template set<T>(value, clear_);
+ }
private:
- Variant<Ts...>& lhs_;
- const bool clear_;
+ Variant<Ts...>& lhs_;
+ const bool clear_;
};
Variant<int, double, std::string> myVariant2 = myVariant;
const int& myInt2 = myVariant2.get<int>(success);
std::cout << "myInt2 = " << myInt2 << " (" << std::boolalpha << success << ")\n";
-<<<<<<< variant_dev_new
Variant<int, double, std::string> myVariant3 = fromInt;
const int& myInt3 = myVariant3.get<int>(success);
std::cout << "myIntCopy = " << myIntCopy << " (" << std::boolalpha << success << ")\n";
std::cout << "myIntCopy equals myInt= " << "(" << std::boolalpha << (myVariant == myVariantCopy) << ")\n";
-=======
->>>>>>> a8c63f3 Tests for Variant serialization
const int& myFake = myVariant.get<double>(success);
std::cout << "myFake = " << myFake << " (" << std::boolalpha << success << ")\n";
return 0;
}
-<<<<<<< variant_dev_new
-
-=======
-template<typename ... Ts>
-struct assign_visitor {
-public:
- assign_visitor(Variant<Ts...>& lhs, const bool clear = true):
- lhs_(lhs), clear_(clear) {
- }
-
- template<typename T>
- void operator()(const T& value) const {
- lhs_.template set<T>(value, clear_);
- }
- template<typename T>
- void operator()(T& value) const {
- lhs_.template set<T>(value, clear_);
- }
-
-private:
- Variant<Ts...>& lhs_;
- const bool clear_;
-};
->>>>>>> a8c63f3 Tests for Variant serialization