JsonCpp project page Classes Namespace JsonCpp home page

allocator.h
Go to the documentation of this file.
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 
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 
66  return std::addressof(x);
67  }
68 
72  void destroy(pointer p) {
73  // destroy using "explicit destructor"
74  p->~T();
75  }
76 
77  // Boilerplate
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>
91  return false;
92 }
93 
94 } //namespace Json
95 
96 #pragma pack(pop)
97 
98 #endif // CPPTL_JSON_ALLOCATOR_H_INCLUDED
Json::SecureAllocator::const_pointer
const T * const_pointer
Definition: allocator.h:21
Json::SecureAllocator::allocate
pointer allocate(size_type n)
Allocate memory for N items using the standard allocator.
Definition: allocator.h:30
Json::SecureAllocator::destroy
void destroy(pointer p)
Destroy an item in-place at pointer P.
Definition: allocator.h:72
Json::CharReaderBuilder::newCharReader
CharReader * newCharReader() const
Allocate a CharReader via operator new().
Definition: json_reader.cpp:1951
Json::Value::size
ArrayIndex size() const
Number of values in array or object.
Definition: json_value.cpp:935
Json::operator==
bool operator==(const SecureAllocator< T > &, const SecureAllocator< U > &)
Definition: allocator.h:85
Json::CharReaderBuilder::settings_
Json::Value settings_
Configuration of this builder.
Definition: reader.h:338
Json::SecureAllocator::SecureAllocator
SecureAllocator(const SecureAllocator< U > &)
Definition: allocator.h:79
Json::operator!=
bool operator!=(const SecureAllocator< T > &, const SecureAllocator< U > &)
Definition: allocator.h:90
Json::SecureAllocator
Definition: allocator.h:16
Json::SecureAllocator::max_size
size_type max_size() const
Definition: allocator.h:57
Json::Value::asString
std::string asString() const
Embedded zeroes are possible.
Definition: json_value.cpp:697
Json::SecureAllocator::deallocate
void deallocate(volatile pointer p, size_type n)
Release memory which was allocated for N items at pointer P.
Definition: allocator.h:42
Json::SecureAllocator::pointer
T * pointer
Definition: allocator.h:20
Json::SecureAllocator::address
pointer address(reference x) const
Definition: allocator.h:61
Json::Value
Represents a JSON value.
Definition: value.h:177
Json::SecureAllocator::SecureAllocator
SecureAllocator()
Definition: allocator.h:78
Json::SecureAllocator::construct
void construct(pointer p, Args &&... args)
Construct an item in-place at pointer P.
Definition: allocator.h:52
Json::SecureAllocator::size_type
std::size_t size_type
Definition: allocator.h:24
Json
JSON (JavaScript Object Notation).
Definition: allocator.h:14
Json::SecureAllocator::rebind
Definition: allocator.h:80
Json::SecureAllocator::difference_type
std::ptrdiff_t difference_type
Definition: allocator.h:25
Json::SecureAllocator::const_reference
const T & const_reference
Definition: allocator.h:23
Json::SecureAllocator::value_type
T value_type
Definition: allocator.h:19
Json::StreamWriterBuilder
Build a StreamWriter implementation.
Definition: writer.h:89
Json::SecureAllocator::reference
T & reference
Definition: allocator.h:22
Json::parseFromStream
bool parseFromStream(CharReader::Factory const &, std::istream &, Value *root, std::string *errs)
Consume entire stream and use its begin/end.
Definition: json_reader.cpp:2036
Json::Value::get
Value get(ArrayIndex index, const Value &defaultValue) const
If the array contains at least index+1 elements, returns the element value, otherwise returns default...
Definition: json_value.cpp:1093
Json::writeString
std::string writeString(StreamWriter::Factory const &factory, Value const &root)
Write into stringstream, then return string, for convenience.
Definition: json_writer.cpp:1219
Json::SecureAllocator::address
const_pointer address(const_reference x) const
Definition: allocator.h:65
Json::CharReaderBuilder
Build a CharReader implementation.
Definition: reader.h:298