1 /*
2  * Copyright (C) 2008 Vaclav Slavik (vslavik@fastmail.fm)
3  * All Rights Reserved
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in
13  *    the documentation and/or other materials provided with the
14  *    distribution.
15  * 3. Neither the name of the Author nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR
23  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 /*
34  * $Id: pimpl_base.hpp 154446 2009-03-11 17:11:29Z satskyse $
35  * NOTE: This file was modified from its original version 0.6.0
36  *       to fit the NCBI C++ Toolkit build framework and
37  *       API and functionality requirements.
38  */
39 
40 #ifndef _xmlwrapp_pimpl_base_h_
41 #define _xmlwrapp_pimpl_base_h_
42 
43 #ifdef HAVE_BOOST_POOL_SINGLETON_POOL_HPP
44     #include <cassert>
45     #include <boost/pool/singleton_pool.hpp>
46 #endif // HAVE_BOOST_POOL_SINGLETON_POOL_HPP
47 
48 namespace xml
49 {
50 
51 namespace impl
52 {
53 
54 // Base class for all pimpl classes. Uses custom pool allocator for better
55 // performance. Usage: derive your class FooImpl from pimpl_base<FooImpl>.
56 template<typename T>
57 class pimpl_base
58 {
59 #ifdef HAVE_BOOST_POOL_SINGLETON_POOL_HPP
60 public:
61     struct xmlwrapp_pool_tag {};
62 
63     // NB: we can't typedef the pool type as pimpl_base<T> subtype,
64     //     because sizeof(T) is unknown (incomplete type) at this point,
65     //     but it's OK to use the type in implementation of operators
66     //     (compiled only when T, and so sizeof(T), is known)
67     #define XMLWRAPP_PIMPL_ALLOCATOR_TYPE(T) \
68         boost::singleton_pool<xmlwrapp_pool_tag, sizeof(T)>
69 
operator new(size_t size)70     static void* operator new(size_t size)
71     {
72         assert( size == sizeof(T) );
73         return XMLWRAPP_PIMPL_ALLOCATOR_TYPE(T)::malloc();
74     }
75 
operator delete(void * ptr,size_t size)76     static void operator delete(void *ptr, size_t size)
77     {
78         assert( size == sizeof(T) );
79         if ( ptr )
80             XMLWRAPP_PIMPL_ALLOCATOR_TYPE(T)::free(ptr);
81     }
82 #endif // HAVE_BOOST_POOL_SINGLETON_POOL_HPP
83 };
84 
85 } // namespace impl
86 
87 } // namespace xml
88 
89 #endif // _xmlwrapp_pimpl_base_h_
90