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 JSON_ALLOCATOR_H_INCLUDED
7 #define 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> class SecureAllocator {
16 public:
17  // Type definitions
18  using value_type = T;
19  using pointer = T*;
20  using const_pointer = const T*;
21  using reference = T&;
22  using const_reference = const T&;
23  using size_type = std::size_t;
24  using difference_type = std::ptrdiff_t;
25 
30  // allocate using "global operator new"
31  return static_cast<pointer>(::operator new(n * sizeof(T)));
32  }
33 
40  // memset_s is used because memset may be optimized away by the compiler
41  memset_s(p, n * sizeof(T), 0, n * sizeof(T));
42  // free using "global operator delete"
43  ::operator delete(p);
44  }
45 
49  template <typename... Args> void construct(pointer p, Args&&... args) {
50  // construct using "placement new" and "perfect forwarding"
51  ::new (static_cast<void*>(p)) T(std::forward<Args>(args)...);
52  }
53 
54  size_type max_size() const { return size_t(-1) / sizeof(T); }
55 
56  pointer address(reference x) const { return std::addressof(x); }
57 
58  const_pointer address(const_reference x) const { return std::addressof(x); }
59 
63  void destroy(pointer p) {
64  // destroy using "explicit destructor"
65  p->~T();
66  }
67 
68  // Boilerplate
70  template <typename U> SecureAllocator(const SecureAllocator<U>&) {}
71  template <typename U> struct rebind { using other = SecureAllocator<U>; };
72 };
73 
74 template <typename T, typename U>
76  return true;
77 }
78 
79 template <typename T, typename U>
81  return false;
82 }
83 
84 } // namespace Json
85 
86 #pragma pack(pop)
87 
88 #endif // JSON_ALLOCATOR_H_INCLUDED
Json::SecureAllocator::const_pointer
const T * const_pointer
Definition: allocator.h:20
Json::SecureAllocator::allocate
pointer allocate(size_type n)
Allocate memory for N items using the standard allocator.
Definition: allocator.h:29
Json::SecureAllocator::destroy
void destroy(pointer p)
Destroy an item in-place at pointer P.
Definition: allocator.h:63
Json::CharReaderBuilder::newCharReader
CharReader * newCharReader() const override
Allocate a CharReader via operator new().
Definition: json_reader.cpp:1883
Json::Value::size
ArrayIndex size() const
Number of values in array or object.
Definition: json_value.cpp:859
Json::operator==
bool operator==(const SecureAllocator< T > &, const SecureAllocator< U > &)
Definition: allocator.h:75
Json::CharReaderBuilder::settings_
Json::Value settings_
Configuration of this builder.
Definition: reader.h:332
Json::SecureAllocator::SecureAllocator
SecureAllocator(const SecureAllocator< U > &)
Definition: allocator.h:70
Json::operator!=
bool operator!=(const SecureAllocator< T > &, const SecureAllocator< U > &)
Definition: allocator.h:80
Json::SecureAllocator
Definition: allocator.h:15
Json::SecureAllocator::deallocate
void deallocate(pointer p, size_type n)
Release memory which was allocated for N items at pointer P.
Definition: allocator.h:39
Json::SecureAllocator::max_size
size_type max_size() const
Definition: allocator.h:54
Json::SecureAllocator::pointer
T * pointer
Definition: allocator.h:19
Json::SecureAllocator::address
pointer address(reference x) const
Definition: allocator.h:56
Json::Value
Represents a JSON value.
Definition: value.h:193
Json::SecureAllocator::SecureAllocator
SecureAllocator()
Definition: allocator.h:69
Json::SecureAllocator::construct
void construct(pointer p, Args &&... args)
Construct an item in-place at pointer P.
Definition: allocator.h:49
Json::SecureAllocator::size_type
std::size_t size_type
Definition: allocator.h:23
Json
JSON (JavaScript Object Notation).
Definition: allocator.h:14
Json::Value::asString
String asString() const
Embedded zeroes are possible.
Definition: json_value.cpp:628
Json::SecureAllocator::rebind
Definition: allocator.h:71
Json::SecureAllocator::difference_type
std::ptrdiff_t difference_type
Definition: allocator.h:24
Json::writeString
String writeString(StreamWriter::Factory const &factory, Value const &root)
Write into stringstream, then return string, for convenience.
Definition: json_writer.cpp:1245
Json::SecureAllocator::const_reference
const T & const_reference
Definition: allocator.h:22
Json::SecureAllocator::value_type
T value_type
Definition: allocator.h:18
Json::parseFromStream
bool parseFromStream(CharReader::Factory const &, IStream &, Value *root, String *errs)
Consume entire stream and use its begin/end.
Definition: json_reader.cpp:1971
Json::StreamWriterBuilder
Build a StreamWriter implementation.
Definition: writer.h:89
Json::SecureAllocator::reference
T & reference
Definition: allocator.h:21
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:1075
Json::SecureAllocator::address
const_pointer address(const_reference x) const
Definition: allocator.h:58
Json::CharReaderBuilder
Build a CharReader implementation.
Definition: reader.h:289