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
Json::SecureAllocator::const_pointer
const T * const_pointer
Definition: allocator.h:19
Json::SecureAllocator::allocate
pointer allocate(size_type n)
Allocate memory for N items using the standard allocator.
Definition: allocator.h:28
Json::SecureAllocator::destroy
void destroy(pointer p)
Destroy an item in-place at pointer P.
Definition: allocator.h:70
Json::CharReaderBuilder::newCharReader
CharReader * newCharReader() const
Allocate a CharReader via operator new().
Definition: json_reader.cpp:1926
Json::Value::size
ArrayIndex size() const
Number of values in array or object.
Definition: json_value.cpp:911
Json::operator==
bool operator==(const SecureAllocator< T > &, const SecureAllocator< U > &)
Definition: allocator.h:83
Json::CharReaderBuilder::settings_
Json::Value settings_
Configuration of this builder.
Definition: reader.h:333
Json::SecureAllocator::SecureAllocator
SecureAllocator(const SecureAllocator< U > &)
Definition: allocator.h:77
Json::operator!=
bool operator!=(const SecureAllocator< T > &, const SecureAllocator< U > &)
Definition: allocator.h:88
Json::SecureAllocator
Definition: allocator.h:14
Json::SecureAllocator::max_size
size_type max_size() const
Definition: allocator.h:55
Json::Value::asString
std::string asString() const
Embedded zeroes are possible.
Definition: json_value.cpp:673
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:40
Json::SecureAllocator::pointer
T * pointer
Definition: allocator.h:18
Json::SecureAllocator::address
pointer address(reference x) const
Definition: allocator.h:59
Json::Value
Represents a JSON value.
Definition: value.h:175
Json::SecureAllocator::SecureAllocator
SecureAllocator()
Definition: allocator.h:76
Json::SecureAllocator::construct
void construct(pointer p, Args &&... args)
Construct an item in-place at pointer P.
Definition: allocator.h:50
Json::SecureAllocator::size_type
std::size_t size_type
Definition: allocator.h:22
Json
JSON (JavaScript Object Notation).
Definition: allocator.h:12
Json::SecureAllocator::rebind
Definition: allocator.h:78
Json::SecureAllocator::difference_type
std::ptrdiff_t difference_type
Definition: allocator.h:23
Json::SecureAllocator::const_reference
const T & const_reference
Definition: allocator.h:21
Json::SecureAllocator::value_type
T value_type
Definition: allocator.h:17
Json::StreamWriterBuilder
Build a StreamWriter implementation.
Definition: writer.h:87
Json::SecureAllocator::reference
T & reference
Definition: allocator.h:20
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:2011
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:1069
Json::writeString
std::string writeString(StreamWriter::Factory const &factory, Value const &root)
Write into stringstream, then return string, for convenience.
Definition: json_writer.cpp:1204
Json::SecureAllocator::address
const_pointer address(const_reference x) const
Definition: allocator.h:63
Json::CharReaderBuilder
Build a CharReader implementation.
Definition: reader.h:293