JsonCpp project page JsonCpp home page

allocator.h
Go to the documentation of this file.
1 // Copyright 2007-2010 Baptiste Lepilleur
2 // Distributed under MIT license, or public domain if desired and
3 // recognized in your jurisdiction.
4 // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
5 
6 #ifndef CPPTL_JSON_ALLOCATOR_H_INCLUDED
7 #define CPPTL_JSON_ALLOCATOR_H_INCLUDED
8 
9 #include <cstring>
10 #include <memory>
11 
12 namespace Json {
13 template<typename T>
15  public:
16  // Type definitions
17  using value_type = T;
18  using pointer = T*;
19  using const_pointer = const T*;
20  using reference = T&;
21  using const_reference = const T&;
22  using size_type = std::size_t;
23  using difference_type = std::ptrdiff_t;
24 
29  // allocate using "global operator new"
30  return static_cast<pointer>(::operator new(n * sizeof(T)));
31  }
32 
40  void deallocate(volatile pointer p, size_type n) {
41  std::memset(p, 0, n * sizeof(T));
42  // free using "global operator delete"
43  ::operator delete(p);
44  }
45 
49  template<typename... Args>
50  void construct(pointer p, Args&&... args) {
51  // construct using "placement new" and "perfect forwarding"
52  ::new (static_cast<void*>(p)) T(std::forward<Args>(args)...);
53  }
54 
55  size_type max_size() const {
56  return size_t(-1) / sizeof(T);
57  }
58 
59  pointer address( reference x ) const {
60  return std::addressof(x);
61  }
62 
64  return std::addressof(x);
65  }
66 
70  void destroy(pointer p) {
71  // destroy using "explicit destructor"
72  p->~T();
73  }
74 
75  // Boilerplate
77  template<typename U> SecureAllocator(const SecureAllocator<U>&) {}
78  template<typename U> struct rebind { using other = SecureAllocator<U>; };
79 };
80 
81 
82 template<typename T, typename U>
84  return true;
85 }
86 
87 template<typename T, typename U>
89  return false;
90 }
91 
92 } //namespace Json
93 
94 #endif // CPPTL_JSON_ALLOCATOR_H_INCLUDED
void construct(pointer p, Args &&...args)
Construct an item in-place at pointer P.
Definition: allocator.h:50
pointer address(reference x) const
Definition: allocator.h:59
const T & const_reference
Definition: allocator.h:21
const_pointer address(const_reference x) const
Definition: allocator.h:63
pointer allocate(size_type n)
Allocate memory for N items using the standard allocator.
Definition: allocator.h:28
SecureAllocator(const SecureAllocator< U > &)
Definition: allocator.h:77
void destroy(pointer p)
Destroy an item in-place at pointer P.
Definition: allocator.h:70
JSON (JavaScript Object Notation).
Definition: allocator.h:12
bool operator==(const SecureAllocator< T > &, const SecureAllocator< U > &)
Definition: allocator.h:83
const T * const_pointer
Definition: allocator.h:19
size_type max_size() const
Definition: allocator.h:55
std::ptrdiff_t difference_type
Definition: allocator.h:23
bool operator!=(const SecureAllocator< T > &, const SecureAllocator< U > &)
Definition: allocator.h:88
std::size_t size_type
Definition: allocator.h:22
void deallocate(volatile pointer p, size_type n)
Release memory which was allocated for N items at pointer P.
Definition: allocator.h:40