ADTF  3.18.2
allocator.h
1 // Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors
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 #pragma pack(push, 8)
13 
14 namespace Json {
15 template<typename T>
17  public:
18  // Type definitions
19  using value_type = T;
20  using pointer = T*;
21  using const_pointer = const T*;
22  using reference = T&;
23  using const_reference = const T&;
24  using size_type = std::size_t;
25  using difference_type = std::ptrdiff_t;
26 
30  pointer allocate(size_type n) {
31  // allocate using "global operator new"
32  return static_cast<pointer>(::operator new(n * sizeof(T)));
33  }
34 
42  void deallocate(volatile pointer p, size_type n) {
43  std::memset(p, 0, n * sizeof(T));
44  // free using "global operator delete"
45  ::operator delete(p);
46  }
47 
51  template<typename... Args>
52  void construct(pointer p, Args&&... args) {
53  // construct using "placement new" and "perfect forwarding"
54  ::new (static_cast<void*>(p)) T(std::forward<Args>(args)...);
55  }
56 
57  size_type max_size() const {
58  return size_t(-1) / sizeof(T);
59  }
60 
61  pointer address( reference x ) const {
62  return std::addressof(x);
63  }
64 
65  const_pointer address( const_reference x ) const {
66  return std::addressof(x);
67  }
68 
72  void destroy(pointer p) {
73  // destroy using "explicit destructor"
74  p->~T();
75  }
76 
77  // Boilerplate
78  SecureAllocator() {}
79  template<typename U> SecureAllocator(const SecureAllocator<U>&) {}
80  template<typename U> struct rebind { using other = SecureAllocator<U>; };
81 };
82 
83 
84 template<typename T, typename U>
86  return true;
87 }
88 
89 template<typename T, typename U>
90 bool operator!=(const SecureAllocator<T>&, const SecureAllocator<U>&) {
91  return false;
92 }
93 
94 } //namespace Json
95 
96 #pragma pack(pop)
97 
98 #endif // CPPTL_JSON_ALLOCATOR_H_INCLUDED
tBool operator!=(const tErrorCode &lhs, const tErrorCode &rhs)
Compare two POD error code types for inequality.
tBool operator==(const tErrorCode &lhs, const tErrorCode &rhs)
Compare two POD error code types for equality.
pointer allocate(size_type n)
Allocate memory for N items using the standard allocator.
Definition: allocator.h:30
void deallocate(volatile pointer p, size_type n)
Release memory which was allocated for N items at pointer P.
Definition: allocator.h:42
void construct(pointer p, Args &&... args)
Construct an item in-place at pointer P.
Definition: allocator.h:52
void destroy(pointer p)
Destroy an item in-place at pointer P.
Definition: allocator.h:72
JSON (JavaScript Object Notation).
Definition: allocator.h:14