antlr3memory.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #ifndef _ANTLR3MEMORY_HPP
  2. #define _ANTLR3MEMORY_HPP
  3. // [The "BSD licence"]
  4. // Copyright (c) 2005-2009 Gokulakannan Somasundaram, ElectronDB
  5. //
  6. // All rights reserved.
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions
  10. // are met:
  11. // 1. Redistributions of source code must retain the above copyright
  12. // notice, this list of conditions and the following disclaimer.
  13. // 2. Redistributions in binary form must reproduce the above copyright
  14. // notice, this list of conditions and the following disclaimer in the
  15. // documentation and/or other materials provided with the distribution.
  16. // 3. The name of the author may not be used to endorse or promote products
  17. // derived from this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  20. // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  21. // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  23. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  24. // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  28. // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. namespace antlr3 {
  30. class DefaultAllocPolicy
  31. {
  32. public:
  33. //limitation of c++. unable to write a typedef
  34. template <class TYPE>
  35. class AllocatorType : public std::allocator<TYPE>
  36. {
  37. public:
  38. typedef TYPE value_type;
  39. typedef value_type* pointer;
  40. typedef const value_type* const_pointer;
  41. typedef value_type& reference;
  42. typedef const value_type& const_reference;
  43. typedef size_t size_type;
  44. typedef ptrdiff_t difference_type;
  45. template<class U> struct rebind {
  46. typedef AllocatorType<U> other;
  47. };
  48. AllocatorType() noexcept {}
  49. AllocatorType( const AllocatorType& ) noexcept {}
  50. template<typename U> AllocatorType(const AllocatorType<U>& ) noexcept{}
  51. };
  52. template<class TYPE>
  53. class VectorType : public std::vector< TYPE, AllocatorType<TYPE> >
  54. {
  55. };
  56. template<class TYPE>
  57. class ListType : public std::deque< TYPE, AllocatorType<TYPE> >
  58. {
  59. };
  60. template<class TYPE>
  61. class StackType : public std::deque< TYPE, AllocatorType<TYPE> >
  62. {
  63. public:
  64. void push( const TYPE& elem ) { this->push_back(elem); }
  65. void pop() { this->pop_back(); }
  66. TYPE& peek() { return this->back(); }
  67. TYPE& top() { return this->back(); }
  68. const TYPE& peek() const { return this->back(); }
  69. const TYPE& top() const { return this->back(); }
  70. };
  71. template<class TYPE>
  72. class OrderedSetType : public std::set< TYPE, std::less<TYPE>, AllocatorType<TYPE> >
  73. {
  74. };
  75. template<class TYPE>
  76. class UnOrderedSetType : public std::set< TYPE, std::less<TYPE>, AllocatorType<TYPE> >
  77. {
  78. };
  79. template<class KeyType, class ValueType>
  80. class UnOrderedMapType : public std::map< KeyType, ValueType, std::less<KeyType>,
  81. AllocatorType<std::pair<const KeyType, ValueType> > >
  82. {
  83. };
  84. template<class KeyType, class ValueType>
  85. class OrderedMapType : public std::map< KeyType, ValueType, std::less<KeyType>,
  86. AllocatorType<std::pair<KeyType, ValueType> > >
  87. {
  88. };
  89. template<class TYPE>
  90. class SmartPtrType : public std::unique_ptr<TYPE, std::default_delete<TYPE> >
  91. {
  92. typedef typename std::unique_ptr<TYPE, std::default_delete<TYPE> > BaseType;
  93. public:
  94. SmartPtrType() {};
  95. SmartPtrType( SmartPtrType&& other )
  96. : BaseType(other)
  97. {};
  98. SmartPtrType & operator=(SmartPtrType&& other) //= default;
  99. {
  100. BaseType::swap(other);
  101. //return std::move((BaseType&)other);
  102. return *this;
  103. }
  104. private:
  105. SmartPtrType & operator=(const SmartPtrType&) /*= delete*/;
  106. SmartPtrType(const SmartPtrType&) /*= delete*/;
  107. };
  108. ANTLR_INLINE static void* operator new (std::size_t bytes)
  109. {
  110. void* p = alloc(bytes);
  111. return p;
  112. }
  113. ANTLR_INLINE static void* operator new (std::size_t , void* p) { return p; }
  114. ANTLR_INLINE static void* operator new[]( std::size_t bytes)
  115. {
  116. void* p = alloc(bytes);
  117. return p;
  118. }
  119. ANTLR_INLINE static void operator delete(void* p)
  120. {
  121. DefaultAllocPolicy::free(p);
  122. }
  123. ANTLR_INLINE static void operator delete(void* , void* ) {} //placement delete
  124. ANTLR_INLINE static void operator delete[](void* p)
  125. {
  126. DefaultAllocPolicy::free(p);
  127. }
  128. ANTLR_INLINE static void* alloc( std::size_t bytes )
  129. {
  130. void* p = malloc(bytes);
  131. if( p== NULL )
  132. throw std::bad_alloc();
  133. return p;
  134. }
  135. ANTLR_INLINE static void* alloc0( std::size_t bytes )
  136. {
  137. void* p = calloc(1, bytes);
  138. if( p== NULL )
  139. throw std::bad_alloc();
  140. return p;
  141. }
  142. ANTLR_INLINE static void free( void* p )
  143. {
  144. return ::free(p);
  145. }
  146. ANTLR_INLINE static void* realloc(void *ptr, size_t size)
  147. {
  148. return ::realloc( ptr, size );
  149. }
  150. };
  151. }
  152. #endif /* _ANTLR3MEMORY_H */