• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef BOOST_STATECHART_EXAMPLE_UNIQUE_OBJECT_ALLOCATOR_HPP_INCLUDED
2 #define BOOST_STATECHART_EXAMPLE_UNIQUE_OBJECT_ALLOCATOR_HPP_INCLUDED
3 //////////////////////////////////////////////////////////////////////////////
4 // Copyright 2002-2006 Andreas Huber Doenni
5 // Distributed under the Boost Software License, Version 1.0. (See accompany-
6 // ing file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 //////////////////////////////////////////////////////////////////////////////
8 
9 
10 
11 #include <boost/statechart/detail/avoid_unused_warning.hpp>
12 
13 #include <boost/config.hpp>
14 
15 #ifdef BOOST_MSVC
16 #  pragma warning( push )
17 #  pragma warning( disable: 4511 ) // copy constructor could not be generated
18 #  pragma warning( disable: 4512 ) // assignment operator could not be generated
19 #endif
20 
21 #include <boost/type_traits/alignment_of.hpp>
22 
23 #ifdef BOOST_MSVC
24 #  pragma warning( pop )
25 #endif
26 
27 #include <boost/type_traits/type_with_alignment.hpp>
28 #include <boost/assert.hpp>
29 
30 #include <cstddef> // size_t
31 
32 
33 
34 //////////////////////////////////////////////////////////////////////////////
35 template< class T >
36 class UniqueObjectAllocator
37 {
38   public:
39     //////////////////////////////////////////////////////////////////////////
allocate(std::size_t size)40     static void * allocate( std::size_t size )
41     {
42       boost::statechart::detail::avoid_unused_warning( size );
43       BOOST_ASSERT( !constructed_ && ( size == sizeof( T ) ) );
44       constructed_ = true;
45       return &storage_.data_[ 0 ];
46     }
47 
deallocate(void * p,std::size_t size)48     static void deallocate( void * p, std::size_t size )
49     {
50       boost::statechart::detail::avoid_unused_warning( p );
51       boost::statechart::detail::avoid_unused_warning( size );
52       BOOST_ASSERT( constructed_ &&
53         ( p == &storage_.data_[ 0 ] ) && ( size == sizeof( T ) ) );
54       constructed_ = false;
55     }
56 
57   private:
58     //////////////////////////////////////////////////////////////////////////
59     union storage
60     {
61       char data_[ sizeof( T ) ];
62       typename boost::type_with_alignment<
63         boost::alignment_of< T >::value >::type aligner_;
64     };
65 
66     static bool constructed_;
67     static storage storage_;
68 };
69 
70 template< class T >
71 bool UniqueObjectAllocator< T >::constructed_ = false;
72 template< class T >
73 typename UniqueObjectAllocator< T >::storage
74   UniqueObjectAllocator< T >::storage_;
75 
76 
77 
78 #endif
79