| 1 |
### Uses-allocator construction example (requires 1.4.0) |
| 2 |
|
| 3 |
This example illustrates [uses-allocator construction](https://en.cppreference.com/w/cpp/memory/uses_allocator.html) |
| 4 |
for types with [json_conv_traits](json_conv_traits.md) traits defined. In the |
| 5 |
example, a user supplied stateful allocator is provided when invoking `jsoncons::try_decode_json`. This |
| 6 |
allocator is used for all result allocations, including the allocated memory for `std::shared_ptr<Employee<Alloc>>` |
| 7 |
and the string type member of `Employee<Alloc>`. A second allocator is provided for allocating memory for |
| 8 |
temporary storage. |
| 9 |
|
| 10 |
```cpp |
| 11 |
#include <jsoncons/json.hpp> |
| 12 |
#include <common/mock_stateful_allocator.hpp> |
| 13 |
#include <iostream> |
| 14 |
#include <scoped_allocator> |
| 15 |
#include <cassert> |
| 16 |
|
| 17 |
// This example requires jsoncons 1.4.0 |
| 18 |
|
| 19 |
namespace ns { |
| 20 |
|
| 21 |
template <typename Alloc> |
| 22 |
class Employee |
| 23 |
{ |
| 24 |
using char_allocator_type = typename std::allocator_traits<Alloc>:: template rebind_alloc<char>; |
| 25 |
using string_type = std::basic_string<char, std::char_traits<char>, char_allocator_type>; |
| 26 |
|
| 27 |
string_type firstName_; |
| 28 |
string_type lastName_; |
| 29 |
public: |
| 30 |
using allocator_type = Alloc; |
| 31 |
|
| 32 |
Employee(const Alloc& alloc) |
| 33 |
: firstName_(alloc), lastName_(alloc) |
| 34 |
{ |
| 35 |
} |
| 36 |
Employee(const string_type& firstName, const string_type& lastName, const Alloc& alloc) |
| 37 |
: firstName_(firstName, alloc), lastName_(lastName, alloc) |
| 38 |
{ |
| 39 |
} |
| 40 |
Employee(const Employee&) = default; |
| 41 |
Employee(Employee&&) = default; |
| 42 |
Employee& operator=(const Employee&) = default; |
| 43 |
Employee& operator=(Employee&&) = default; |
| 44 |
|
| 45 |
Employee(const Employee& other, const Alloc& alloc) |
| 46 |
: firstName_(other.firstName_, alloc), lastName_(other.lastName_, alloc) |
| 47 |
{ |
| 48 |
} |
| 49 |
|
| 50 |
Employee(const Employee&& other, const Alloc& alloc) |
| 51 |
: firstName_(std::move(other.firstName_), alloc), lastName_(std::move(other.lastName_), alloc) |
| 52 |
{ |
| 53 |
} |
| 54 |
|
| 55 |
virtual ~Employee() noexcept = default; |
| 56 |
|
| 57 |
virtual double calculatePay() const = 0; |
| 58 |
|
| 59 |
const string_type& firstName() const { return firstName_; } |
| 60 |
const string_type& lastName() const { return lastName_; } |
| 61 |
}; |
| 62 |
|
| 63 |
template <typename Alloc> |
| 64 |
class HourlyEmployee : public Employee<Alloc> |
| 65 |
{ |
| 66 |
using char_allocator_type = typename std::allocator_traits<Alloc>:: template rebind_alloc<char>; |
| 67 |
using string_type = std::basic_string<char, std::char_traits<char>, char_allocator_type>; |
| 68 |
|
| 69 |
double wage_; |
| 70 |
unsigned hours_; |
| 71 |
public: |
| 72 |
using allocator_type = typename Employee<Alloc>::allocator_type; |
| 73 |
|
| 74 |
HourlyEmployee(const string_type& firstName, const string_type& lastName, |
| 75 |
double wage, unsigned hours, const Alloc& alloc) |
| 76 |
: Employee<Alloc>(firstName, lastName, alloc), |
| 77 |
wage_(wage), hours_(hours) |
| 78 |
{ |
| 79 |
} |
| 80 |
HourlyEmployee(const HourlyEmployee&) = default; |
| 81 |
HourlyEmployee(HourlyEmployee&&) = default; |
| 82 |
HourlyEmployee& operator=(const HourlyEmployee&) = default; |
| 83 |
HourlyEmployee& operator=(HourlyEmployee&&) = default; |
| 84 |
|
| 85 |
HourlyEmployee(const HourlyEmployee& other, const Alloc& alloc) |
| 86 |
: Employee<Alloc>(other, alloc), |
| 87 |
wage_(other, wage_), hours_(other.hours_) |
| 88 |
{ |
| 89 |
} |
| 90 |
|
| 91 |
HourlyEmployee(HourlyEmployee&& other, const Alloc& alloc) |
| 92 |
: Employee<Alloc>(std::move(other), alloc), |
| 93 |
wage_(other.wage_), hours_(other.hours_) |
| 94 |
{ |
| 95 |
} |
| 96 |
|
| 97 |
double wage() const { return wage_; } |
| 98 |
|
| 99 |
unsigned hours() const { return hours_; } |
| 100 |
|
| 101 |
double calculatePay() const override |
| 102 |
{ |
| 103 |
return wage_ * hours_; |
| 104 |
} |
| 105 |
}; |
| 106 |
|
| 107 |
template <typename Alloc> |
| 108 |
class CommissionedEmployee : public Employee<Alloc> |
| 109 |
{ |
| 110 |
using char_allocator_type = typename std::allocator_traits<Alloc>:: template rebind_alloc<char>; |
| 111 |
using string_type = std::basic_string<char, std::char_traits<char>, char_allocator_type>; |
| 112 |
|
| 113 |
double baseSalary_; |
| 114 |
double commission_; |
| 115 |
unsigned sales_; |
| 116 |
public: |
| 117 |
using allocator_type = typename Employee<Alloc>::allocator_type; |
| 118 |
|
| 119 |
CommissionedEmployee(const string_type& firstName, const string_type& lastName, |
| 120 |
double baseSalary, double commission, unsigned sales, const Alloc& alloc) |
| 121 |
: Employee<Alloc>(firstName, lastName, alloc), |
| 122 |
baseSalary_(baseSalary), commission_(commission), sales_(sales) |
| 123 |
{ |
| 124 |
} |
| 125 |
CommissionedEmployee(const CommissionedEmployee&) = default; |
| 126 |
CommissionedEmployee(CommissionedEmployee&&) = default; |
| 127 |
|
| 128 |
CommissionedEmployee(const CommissionedEmployee& other, const Alloc& alloc) |
| 129 |
: Employee<Alloc>(other, alloc), |
| 130 |
baseSalary_(other, baseSalary_), commission_(other.commission_), sales_(other.sales_) |
| 131 |
{ |
| 132 |
} |
| 133 |
|
| 134 |
CommissionedEmployee(CommissionedEmployee&& other, const Alloc& alloc) |
| 135 |
: Employee<Alloc>(std::move(other), alloc), |
| 136 |
baseSalary_(other.baseSalary_), commission_(other.commission_), sales_(other.sales_) |
| 137 |
{ |
| 138 |
} |
| 139 |
|
| 140 |
CommissionedEmployee& operator=(const CommissionedEmployee&) = default; |
| 141 |
CommissionedEmployee& operator=(CommissionedEmployee&&) = default; |
| 142 |
|
| 143 |
double baseSalary() const |
| 144 |
{ |
| 145 |
return baseSalary_; |
| 146 |
} |
| 147 |
|
| 148 |
double commission() const |
| 149 |
{ |
| 150 |
return commission_; |
| 151 |
} |
| 152 |
|
| 153 |
unsigned sales() const |
| 154 |
{ |
| 155 |
return sales_; |
| 156 |
} |
| 157 |
|
| 158 |
double calculatePay() const override |
| 159 |
{ |
| 160 |
return baseSalary_ + commission_ * sales_; |
| 161 |
} |
| 162 |
}; |
| 163 |
|
| 164 |
} // namespace ns |
| 165 |
|
| 166 |
template <typename T> |
| 167 |
using cust_allocator = std::scoped_allocator_adaptor<mock_stateful_allocator<T>>; |
| 168 |
|
| 169 |
JSONCONS_TPL_ALL_CTOR_GETTER_TRAITS(1, ns::HourlyEmployee, firstName, lastName, wage, hours) |
| 170 |
JSONCONS_TPL_ALL_CTOR_GETTER_TRAITS(1, ns::CommissionedEmployee, firstName, lastName, baseSalary, commission, sales) |
| 171 |
JSONCONS_POLYMORPHIC_TRAITS(ns::Employee<cust_allocator<char>>, ns::HourlyEmployee<cust_allocator<char>>, ns::CommissionedEmployee<cust_allocator<char>>) |
| 172 |
|
| 173 |
int main() |
| 174 |
{ |
| 175 |
std::string input = R"( |
| 176 |
[ |
| 177 |
{ |
| 178 |
"firstName": "John", |
| 179 |
"hours": 1000, |
| 180 |
"lastName": "Smith", |
| 181 |
"wage": 40.0 |
| 182 |
}, |
| 183 |
{ |
| 184 |
"baseSalary": 30000.0, |
| 185 |
"commission": 0.25, |
| 186 |
"firstName": "Jane", |
| 187 |
"lastName": "Doe", |
| 188 |
"sales": 1000 |
| 189 |
} |
| 190 |
] |
| 191 |
)"; |
| 192 |
|
| 193 |
using element_type = std::shared_ptr<ns::Employee<cust_allocator<char>>>; |
| 194 |
using value_type = std::vector<element_type, cust_allocator<element_type>>; |
| 195 |
|
| 196 |
cust_allocator<char> alloc(1); |
| 197 |
cust_allocator<char> alloc_temp(2); |
| 198 |
auto aset = jsoncons::make_alloc_set(alloc, alloc_temp); |
| 199 |
auto r = jsoncons::try_decode_json<value_type>(aset, input); |
| 200 |
if (!r) |
| 201 |
{ |
| 202 |
std::cout << r.error().message() << "\n"; |
| 203 |
} |
| 204 |
|
| 205 |
value_type& v{*r}; |
| 206 |
|
| 207 |
assert(2 == v.size()); |
| 208 |
assert(v[0]->firstName().c_str() == "John"); |
| 209 |
assert(v[0]->lastName().c_str() == "Smith"); |
| 210 |
assert(v[0]->calculatePay() == 40000); |
| 211 |
assert(v[1]->firstName().c_str() == "Jane"); |
| 212 |
assert(v[1]->lastName().c_str() == "Doe"); |
| 213 |
assert(v[1]->calculatePay() == 30250); |
| 214 |
} |
| 215 |
``` |
| 216 |
|
| 217 |
### See also |
| 218 |
|
| 219 |
[allocator_set](../allocator_set.md) |
| 220 |
[decode_json, try_decode_json](../decode_json.md) |
| 221 |
[encode_json, encode_json_pretty, try_encode_json, try_encode_json_pretty](../encode_json.md) |
| 222 |
[basic_json](../basic_json.md) |
| 223 |
|