Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef UNION_H
- #define UNION_H
- #include <boost/mpl/placeholders.hpp>
- #include <boost/static_assert.hpp>
- #include <boost/type_traits/is_object.hpp>
- #include <boost/mpl/equal_to.hpp>
- #include <boost/mpl/less_equal.hpp>
- #include <boost/mpl/count_if.hpp>
- #include <boost/mpl/int.hpp>
- #include <boost/type_traits/is_same.hpp>
- #include <boost/mpl/distance.hpp>
- #include <boost/mpl/end.hpp>
- #include <boost/type_traits/is_class.hpp>
- #include <boost/mpl/less.hpp>
- #include <boost/mpl/sizeof.hpp>
- #include <boost/mpl/max_element.hpp>
- #include <boost/mpl/deref.hpp>
- /*using boost::mpl::deref;
- using boost::mpl::int_;
- using boost::mpl::end;
- using boost::mpl::bool_;
- using boost::mpl::equal_to;
- using boost::mpl::vector;*/
- namespace mpl = boost::mpl;
- using namespace mpl::placeholders;
- template<typename Elem>
- struct is_not_object : mpl::bool_<!boost::is_object<Elem>::value>::type { };
- template <typename Seq>
- struct chech_and_return_max_type {
- //private:
- static const bool b = mpl::equal_to<mpl::count_if<Seq, is_not_object<_> >::type, mpl::int_<0>>::value;
- BOOST_STATIC_ASSERT(b);
- public :
- typedef typename mpl::deref<
- typename mpl::max_element
- <Seq,
- mpl::less<mpl::sizeof_<_1>, mpl::sizeof_<_2>>
- >::type
- >::type max_type;
- };
- template <class T>
- T *new_alloc() {
- char *pchData = reinterpret_cast <char *> (std::malloc(sizeof(T)));
- return new (pchData) T();
- }
- template <class T> void delete_alloc(T *pT) {
- pT->~T(); // при T == int ?
- std::free(pT);
- }
- template<typename F, typename S>
- struct can_be_inserted : mpl::less_equal<mpl::sizeof_<F>, mpl::sizeof_<S>> { };
- template<typename Seq>
- class my_union
- {
- private:
- typedef chech_and_return_max_type<Seq> carmt;
- typedef typename carmt::max_type type;
- public:
- void * element;
- //int f = is_not_object<std::vector<void>>();
- my_union() {
- //std::malloc(sizeof(type));
- std::cout << sizeof(type) << std::endl;
- element = new_alloc<type>();
- }
- //static bool p =
- //bool b = is_not_object<int>::value;//
- template<typename T>
- my_union & operator=(T & elem) {
- //BOOST_STATIC_ASSERT(can_be_inserted<T, carmt::max_type>());
- //BOOST_STATIC_ASSERT(mpl::less_equal<mpl::sizeof_<T>, mpl::sizeof_<T>>);
- //element = new_alloc<T>();
- //element = 444;
- element = new (element) T(elem);
- T * t =reinterpret_cast<T*>(element);
- // t->~T();
- return *this;
- }
- ~my_union() {
- delete_alloc(element);
- };
- private:
- };
- #endif // !UNION_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement