construct.h
Go to the documentation of this file.
00001 /* construct.h
00002  */
00003 #ifndef OSL_CONSTRUCT_H
00004 #define OSL_CONSTRUCT_H
00005 
00006 #include <boost/type_traits/has_trivial_destructor.hpp>
00007 #include <boost/type_traits/is_pod.hpp>
00008 #include <boost/utility/enable_if.hpp>
00009 #include <iterator>
00010 #include <memory>
00011 #include <cassert>
00012 namespace osl
00013 {
00014   class Piece;
00015   class Move;
00016   class Square;
00017   namespace rating
00018   {
00019     class RatedMove;
00020   }
00021   namespace misc
00022   {
00023     namespace detail
00024     {
00026       template <typename T>
00027       struct BitCopyTraits 
00028       {
00029         static const bool value=boost::is_pod<T>::value; 
00030       };
00031 
00032       template <> struct BitCopyTraits<Move> { static const bool value=true; };
00033       template <> struct BitCopyTraits<Piece> { static const bool value=true; };
00034       template <> struct BitCopyTraits<Square> { static const bool value=true; };
00035       template <> struct BitCopyTraits<rating::RatedMove> { static const bool value=true; };
00036     }
00037 
00038     template <typename T1, typename T2>
00039     inline
00040     void construct(T1* ptr, const T2& value, 
00041                    typename boost::enable_if<detail::BitCopyTraits<T1> >::type * =0)
00042     {
00043       assert(ptr);
00044       *ptr = T1(value);
00045     }
00046 
00047     template <typename T1, typename T2>
00048     inline
00049     void construct(T1* ptr, const T2& value, 
00050                    typename boost::disable_if<detail::BitCopyTraits<T1> >::type * =0)
00051     {
00052       assert(ptr);
00053       ::new(ptr) T1(value);
00054     }
00055 
00056     template <typename T>
00057     inline void destroy(T *ptr) 
00058     {
00059       ptr->~T(); 
00060     }
00061 
00062     template <typename ForwardIterator>
00063     inline void destroy(ForwardIterator first, ForwardIterator last)
00064     {
00065       typedef typename std::iterator_traits<ForwardIterator>::value_type
00066         value_type;
00067       if (boost::has_trivial_destructor<value_type>::value)
00068         return;
00069       for (; first != last; ++first)
00070         destroy(&*first);
00071     }
00072   }
00073 }
00074 
00075 
00076 #endif /* OSL_CONSTRUCT_H */
00077 // ;;; Local Variables:
00078 // ;;; mode:c++
00079 // ;;; c-basic-offset:2
00080 // ;;; End:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines