ruby_allocator.hh
Go to the documentation of this file.
00001 #ifndef RUBY_ALLOCATOR_HH
00002 #define RUBY_ALLOCATOR_HH
00003 
00004 #include <ruby.h>
00005 
00006 template <class T> class ruby_allocator
00007 {
00008 public:
00009   typedef T                 value_type;
00010   typedef value_type*       pointer;
00011   typedef const value_type* const_pointer;
00012   typedef value_type&       reference;
00013   typedef const value_type& const_reference;
00014   typedef std::size_t       size_type;
00015   typedef std::ptrdiff_t    difference_type;
00016   
00017   template <class U> 
00018   struct rebind { typedef ruby_allocator<U> other; };
00019 
00020   ruby_allocator() {}
00021   ruby_allocator(const ruby_allocator&) {}
00022   template <class U> 
00023   ruby_allocator(const ruby_allocator<U>&) {}
00024   ~ruby_allocator() {}
00025 
00026   pointer address(reference x) const { return &x; }
00027   const_pointer address(const_reference x) const { 
00028     return x;
00029   }
00030 
00031   pointer allocate(size_type n, const_pointer = 0) {
00032     void* p = ruby_xmalloc(n * sizeof(T));
00033     if (!p)
00034       throw std::bad_alloc();
00035     return static_cast<pointer>(p);
00036   }
00037 
00038   void deallocate(pointer p, size_type) { ruby_xfree(p); }
00039 
00040   size_type max_size() const { 
00041     return static_cast<size_type>(-1) / sizeof(T);
00042   }
00043 
00044   void construct(pointer p, const value_type& x) { 
00045     new(p) value_type(x); 
00046   }
00047   void destroy(pointer p) { p->~value_type(); }
00048 
00049 private:
00050   void operator=(const ruby_allocator&);
00051 };
00052 
00053 template<> class ruby_allocator<void>
00054 {
00055   typedef void        value_type;
00056   typedef void*       pointer;
00057   typedef const void* const_pointer;
00058 
00059   template <class U> 
00060   struct rebind { typedef ruby_allocator<U> other; };
00061 };
00062 
00063 
00064 template <class T>
00065 inline bool operator==(const ruby_allocator<T>&, 
00066                        const ruby_allocator<T>&) {
00067   return true;
00068 }
00069 
00070 template <class T>
00071 inline bool operator!=(const ruby_allocator<T>&, 
00072                        const ruby_allocator<T>&) {
00073   return false;
00074 }
00075 
00076 #endif


utilrb
Author(s): Sylvain Joyeux/sylvain.joyeux@m4x.org
autogenerated on Sat Jun 8 2019 18:49:20