summaryrefslogtreecommitdiff
path: root/inc/Standard_StdAllocator.hxx
blob: 9a20bc8554c7171fe97625662f2a955464ea5c68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Author: Roman Lygin, 2011.
// This file is put into Public Domain and thus can freely be used for any purpose.
// The author disclaims any rights and liabilities.

#ifndef _Standard_StdAllocator_HeaderFile
#define _Standard_StdAllocator_HeaderFile

#include <Standard.hxx>

//! Implements allocator requirements as defined in ISO C++ Standard 2003, section 20.1.5.
/*! The allocator uses Open CASCADE Technology memory management API (
    Standard::Allocate() and Standard::Free()). It can be used with standard
    containers (std::vector, std::map, etc) to use centralized Open CASCADE
    memory management and hence decrease memory footprint and/or increase
    performance.

    Example of use:
    \code
    std::vector<TopoDS_Shape, Standard_StdAllocator<TopoDS_Shape> > aVec;
    TopoDS_Solid aSolid = BRepPrimAPI_MakeBox (10., 20., 30.);
    aVec.push_back (aSolid);
    \endcode
*/
template<typename T>
class Standard_StdAllocator {
public:
    typedef T value_type;
    typedef value_type* pointer;
    typedef const value_type* const_pointer;
    typedef value_type& reference;
    typedef const value_type& const_reference;
    typedef size_t size_type;
    typedef ptrdiff_t difference_type;
    template<typename U> struct rebind {
        typedef Standard_StdAllocator<U> other;
    };

    //! Constructor.
    /*! Creates an empty object.*/
    Standard_StdAllocator() throw() {}

    //! Constructor.
    /*! Creates an empty object.*/
    Standard_StdAllocator( const Standard_StdAllocator& ) throw() {}

    //! Destructor.
    /*! Empty implementation.*/
    ~Standard_StdAllocator() throw() {}

    //! Constructor.
    /*! Creates an empty object.*/
    template<typename U> Standard_StdAllocator( const Standard_StdAllocator<U>& ) throw() {}

    //! Returns an object address.
    /*! Returns &x.*/
    pointer address( reference x ) const { return &x; }

    //! Returns an object address.
    /*! Returns &x.*/
    const_pointer address( const_reference x ) const { return &x; }
    
    //! Allocates memory for \a n objects.
    /*! Uses Standard::Allocate().*/
    pointer allocate( size_type n, const void* /*hint*/ = 0 )
    { return pointer( Standard::Allocate( n * sizeof( value_type ))); }

    //! Frees previously allocated memory.
    /*! Uses Standard::Free().*/
    void deallocate( pointer p, size_type )
    {
        Standard_Address a = p;
        Standard::Free (a); //Standard::Free() requires Standard_Address&
    }

    //! Returns the largest value for which method allocate might succeed.
    size_type max_size() const throw()
    {
        size_type aMax = static_cast<size_type>( -1 ) / sizeof( value_type );
        return (aMax > 0 ? aMax : 1);
    }

    //! Constructs an object.
    /*! Uses the placement new operator and copy constructor to construct an object.*/
    void construct( pointer p, const_reference val )
    { new( static_cast<void*>( p )) value_type( val ); }

    //! Destroys the object.
    /*! Uses object destructor.*/
    /*  Destructor name must match the class name for __BORLANDC__.*/
#ifdef __BORLANDC__
    void destroy( pointer p ) { p->~T(); }
#else
    void destroy( pointer p ) { p->~value_type(); }
#endif
};

//! Implements specialization Standard_StdAllocator<void>.
/*! Specialization is similar to std::allocator<void>, as defined in ISO C++
    Standard 2003, section 20.4.1.

    This specialization is interchangeable with other ones.

    Example of use:
    \code
    std::vector<TopoDS_Shape, Standard_StdAllocator<void> > aVec;
    TopoDS_Solid aSolid = BRepPrimAPI_MakeBox (10., 20., 30.);
    aVec.push_back (aSolid);
    \endcode
*/
template<> 
class Standard_StdAllocator<void> {
public:
    typedef void* pointer;
    typedef const void* const_pointer;
    typedef void value_type;
    template<typename U> struct rebind {
        typedef Standard_StdAllocator<U> other;
    };
};

template<typename T, typename U>
inline bool operator==( const Standard_StdAllocator<T>&, const Standard_StdAllocator<U>& )
{ return true; }

template<typename T, typename U>
inline bool operator!=( const Standard_StdAllocator<T>&, const Standard_StdAllocator<U>& )
{ return false; }


#endif