Advertisement
Guest User

Untitled

a guest
May 29th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.51 KB | None | 0 0
  1. #ifndef UNION_H
  2. #define UNION_H
  3.  
  4.  
  5.  
  6.  
  7.  
  8. #include <boost/mpl/placeholders.hpp>
  9. #include <boost/static_assert.hpp>
  10. #include <boost/type_traits/is_object.hpp>
  11. #include <boost/mpl/equal_to.hpp>
  12. #include <boost/mpl/less_equal.hpp>
  13. #include <boost/mpl/count_if.hpp>
  14. #include <boost/mpl/int.hpp>
  15. #include <boost/type_traits/is_same.hpp>
  16. #include <boost/mpl/distance.hpp>
  17. #include <boost/mpl/end.hpp>
  18. #include <boost/type_traits/is_class.hpp>
  19. #include <boost/mpl/less.hpp>
  20. #include <boost/mpl/sizeof.hpp>
  21. #include <boost/mpl/max_element.hpp>
  22. #include <boost/mpl/deref.hpp>
  23.  
  24.  
  25. /*using boost::mpl::deref;
  26. using boost::mpl::int_;
  27. using boost::mpl::end;
  28. using boost::mpl::bool_;
  29. using boost::mpl::equal_to;
  30. using boost::mpl::vector;*/
  31.  
  32. namespace mpl = boost::mpl;
  33. using namespace mpl::placeholders;
  34.  
  35. template<typename Elem>
  36. struct is_not_object : mpl::bool_<!boost::is_object<Elem>::value>::type { };
  37.  
  38. template <typename Seq>
  39. struct chech_and_return_max_type {
  40. //private: 
  41.     static const bool b = mpl::equal_to<mpl::count_if<Seq, is_not_object<_> >::type, mpl::int_<0>>::value;
  42.  
  43.      BOOST_STATIC_ASSERT(b);
  44. public :
  45.     typedef typename mpl::deref<
  46.                     typename mpl::max_element
  47.                                         <Seq,
  48.                                         mpl::less<mpl::sizeof_<_1>, mpl::sizeof_<_2>>
  49.                                             >::type
  50.                                 >::type max_type;
  51.  
  52. };
  53.  
  54. template <class T>
  55. T *new_alloc() {
  56.     char *pchData = reinterpret_cast <char *> (std::malloc(sizeof(T)));
  57.     return new (pchData) T();
  58. }
  59. template <class T> void delete_alloc(T *pT) {
  60.     pT->~T(); // при T == int ?
  61.     std::free(pT);
  62. }
  63.  
  64.  
  65. template<typename F, typename S>
  66. struct can_be_inserted : mpl::less_equal<mpl::sizeof_<F>, mpl::sizeof_<S>> { };
  67.  
  68. template<typename Seq>
  69. class my_union
  70. {
  71. private:
  72.      typedef chech_and_return_max_type<Seq> carmt;
  73.      typedef typename carmt::max_type type;
  74. public:
  75.      void * element;
  76.  
  77.     //int f = is_not_object<std::vector<void>>();
  78.     my_union() {
  79.         //std::malloc(sizeof(type));
  80.         std::cout << sizeof(type) << std::endl;
  81.         element = new_alloc<type>();
  82.     }
  83.     //static bool p =
  84.     //bool b = is_not_object<int>::value;//
  85.     template<typename T>
  86.         my_union & operator=(T & elem) {
  87.         //BOOST_STATIC_ASSERT(can_be_inserted<T, carmt::max_type>());
  88.         //BOOST_STATIC_ASSERT(mpl::less_equal<mpl::sizeof_<T>, mpl::sizeof_<T>>);
  89.             //element = new_alloc<T>();
  90.             //element = 444;
  91.             element = new (element) T(elem);
  92.             T * t =reinterpret_cast<T*>(element);
  93. //      t->~T();
  94.         return *this;
  95.     }
  96.  
  97.     ~my_union() {
  98.         delete_alloc(element);
  99.     };
  100.  
  101. private:
  102.  
  103. };
  104.  
  105. #endif // !UNION_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement