C++ (Qt)CData data(CData::type_Coord);if (leftEggWants) data = CData(CData::type_Value);double test = data[0];
C++ (Qt)private: struct holder_base { holder_base(size_t _id) : id(_id) {} virtual ~holder_base() {} size_t id; }; template <class T> struct holder : holder_base { holder(const T & _data) : holder_base(detail::type_id<T, Args...>::value), data(_data) {} T data; }; std::shared_ptr<holder_base> _holder_ptr;
C++ (Qt) template <class T> const T& get() const throw (std::bad_cast) { if (!is_type<T>()) throw std::bad_cast(); return std::static_pointer_cast<holder<T>>(_holder_ptr)->data; } template <class T> T& get() throw (std::bad_cast) { if (!is_type<T>()) throw std::bad_cast(); if (!_holder_ptr.unique()) _holder_ptr = std::make_shared<holder<T>>(std::static_pointer_cast<holder<T>>(_holder_ptr)->data); return std::static_pointer_cast<holder<T>>(_holder_ptr)->data; } template <class T> variant& operator=(const T& val) { static_assert(detail::type_id<T, Args...>::value != detail::type_id<T, Args...>::bad_id, "variant::operator=(const T&): unknown type T"); _holder_ptr = std::make_shared<holder<T>>(val); return *this; }
C++ (Qt)#include <iostream>#include <array>#include <exception>#include <boost/variant.hpp> typedef double value_t;typedef std::array<double, 3> coord_t;typedef std::array<float, 4> color_t;typedef boost::variant<value_t, coord_t, color_t> data_t; class getter_visitor : public boost::static_visitor<value_t>{public: getter_visitor(size_t index) : m_index(index) {} value_t operator()(const value_t & x) const { if (m_index != 0) throw std::out_of_range("out of range!"); return x; } template <class T> value_t operator()(const T & x) const { return x.at(m_index); } private: size_t m_index;}; class setter_visitor : public boost::static_visitor<void>{public: setter_visitor(const value_t & data, size_t index) : m_data(data), m_index(index) {} void operator()(value_t & x) const { if (m_index != 0) throw std::out_of_range("out of range!"); x = m_data; } template <class T> void operator()(T & x) const { x.at(m_index) = m_data; } private: const value_t & m_data; size_t m_index;}; struct CData{ data_t data; value_t get(size_t index) const { return boost::apply_visitor(getter_visitor(index), data); } void set(const value_t & x, size_t index) { boost::apply_visitor(setter_visitor(x, index), data); }}; int main(){ CData data1; CData data2; data1.data = 3.14; data1.set(2.71, 0); data2.data = color_t{.5f, .5f, .5f, .0f}; data2.set(5.0, 1); try { std::cout << data1.get(0) << std::endl; std::cout << data2.get(1) << std::endl; } catch (const std::out_of_range & exc) { std::cout << exc.what() << std::endl; } return 0;}
C++ (Qt)friend CData operator * ( const CData & a, const CData & b ) { CData dst(a); for (size_t i = 0; i < dst.Size(); ++i) dst.SetNth(i, dst.GetNth(i) * b.GetNth(i)); return dst; }
C++ (Qt)friend CData operator * ( const CData & a, const CData & b ) { Q_ASSERT(a.Size() == b.Size()); CData dst(a); for (size_t i = 0; i < dst.Size(); ++i) dst.SetNth(i, dst.GetNth(i) * b.GetNth(i)); return dst; }
C++ (Qt)Container<double> c1; Container<coordinate> c2; Container<color> c3;
C++ (Qt)template<class T>T Container<T>::GetElement( size_t index ) const;
C++ (Qt)CData CBaseContainer::GetElement( size_t index ) const;void CBaseContainer::SetElement( size_t index, const CData & data );