File: uses-allocator-construction-example.md

package info (click to toggle)
jsoncons 1.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 18,276 kB
  • sloc: cpp: 143,266; sh: 34; makefile: 8
file content (223 lines) | stat: -rw-r--r-- 7,096 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
### Uses-allocator construction example (requires 1.4.0)

This example illustrates [uses-allocator construction](https://en.cppreference.com/w/cpp/memory/uses_allocator.html) 
for types with [json_conv_traits](json_conv_traits.md) traits defined. In the
example, a user supplied stateful allocator is provided when invoking `jsoncons::try_decode_json`. This
allocator is used for all result allocations, including the allocated memory for `std::shared_ptr<Employee<Alloc>>`
and the string type member of `Employee<Alloc>`. A second allocator is provided for allocating memory for 
temporary storage. 

```cpp
#include <jsoncons/json.hpp>
#include <common/mock_stateful_allocator.hpp>
#include <iostream>
#include <scoped_allocator>
#include <cassert>

// This example requires jsoncons 1.4.0

namespace ns {

template <typename Alloc>
class Employee
{
    using char_allocator_type = typename std::allocator_traits<Alloc>:: template rebind_alloc<char>;
    using string_type = std::basic_string<char, std::char_traits<char>, char_allocator_type>;

    string_type firstName_;
    string_type lastName_;
public:
    using allocator_type = Alloc;

    Employee(const Alloc& alloc)
        : firstName_(alloc), lastName_(alloc)
    {
    }
    Employee(const string_type& firstName, const string_type& lastName, const Alloc& alloc)
        : firstName_(firstName, alloc), lastName_(lastName, alloc)
    {
    }
    Employee(const Employee&) = default;
    Employee(Employee&&) = default;
    Employee& operator=(const Employee&) = default;
    Employee& operator=(Employee&&) = default;

    Employee(const Employee& other, const Alloc& alloc)
        : firstName_(other.firstName_, alloc), lastName_(other.lastName_, alloc)
    {
    }

    Employee(const Employee&& other, const Alloc& alloc)
        : firstName_(std::move(other.firstName_), alloc), lastName_(std::move(other.lastName_), alloc)
    {
    }

    virtual ~Employee() noexcept = default;

    virtual double calculatePay() const = 0;

    const string_type& firstName() const { return firstName_; }
    const string_type& lastName() const { return lastName_; }
};

template <typename Alloc>
class HourlyEmployee : public Employee<Alloc>
{
    using char_allocator_type = typename std::allocator_traits<Alloc>:: template rebind_alloc<char>;
    using string_type = std::basic_string<char, std::char_traits<char>, char_allocator_type>;

    double wage_;
    unsigned hours_;
public:
    using allocator_type = typename Employee<Alloc>::allocator_type;

    HourlyEmployee(const string_type& firstName, const string_type& lastName,
        double wage, unsigned hours, const Alloc& alloc)
        : Employee<Alloc>(firstName, lastName, alloc),
        wage_(wage), hours_(hours)
    {
    }
    HourlyEmployee(const HourlyEmployee&) = default;
    HourlyEmployee(HourlyEmployee&&) = default;
    HourlyEmployee& operator=(const HourlyEmployee&) = default;
    HourlyEmployee& operator=(HourlyEmployee&&) = default;

    HourlyEmployee(const HourlyEmployee& other, const Alloc& alloc)
        : Employee<Alloc>(other, alloc),
        wage_(other, wage_), hours_(other.hours_)
    {
    }

    HourlyEmployee(HourlyEmployee&& other, const Alloc& alloc)
        : Employee<Alloc>(std::move(other), alloc),
        wage_(other.wage_), hours_(other.hours_)
    {
    }

    double wage() const { return wage_; }

    unsigned hours() const { return hours_; }

    double calculatePay() const override
    {
        return wage_ * hours_;
    }
};

template <typename Alloc>
class CommissionedEmployee : public Employee<Alloc>
{
    using char_allocator_type = typename std::allocator_traits<Alloc>:: template rebind_alloc<char>;
    using string_type = std::basic_string<char, std::char_traits<char>, char_allocator_type>;

    double baseSalary_;
    double commission_;
    unsigned sales_;
public:
    using allocator_type = typename Employee<Alloc>::allocator_type;

    CommissionedEmployee(const string_type& firstName, const string_type& lastName,
        double baseSalary, double commission, unsigned sales, const Alloc& alloc)
        : Employee<Alloc>(firstName, lastName, alloc),
        baseSalary_(baseSalary), commission_(commission), sales_(sales)
    {
    }
    CommissionedEmployee(const CommissionedEmployee&) = default;
    CommissionedEmployee(CommissionedEmployee&&) = default;

    CommissionedEmployee(const CommissionedEmployee& other, const Alloc& alloc)
        : Employee<Alloc>(other, alloc),
        baseSalary_(other, baseSalary_), commission_(other.commission_), sales_(other.sales_)
    {
    }

    CommissionedEmployee(CommissionedEmployee&& other, const Alloc& alloc)
        : Employee<Alloc>(std::move(other), alloc),
        baseSalary_(other.baseSalary_), commission_(other.commission_), sales_(other.sales_)
    {
    }

    CommissionedEmployee& operator=(const CommissionedEmployee&) = default;
    CommissionedEmployee& operator=(CommissionedEmployee&&) = default;

    double baseSalary() const
    {
        return baseSalary_;
    }

    double commission() const
    {
        return commission_;
    }

    unsigned sales() const
    {
        return sales_;
    }

    double calculatePay() const override
    {
        return baseSalary_ + commission_ * sales_;
    }
};

} // namespace ns

template <typename T>
using cust_allocator = std::scoped_allocator_adaptor<mock_stateful_allocator<T>>;

JSONCONS_TPL_ALL_CTOR_GETTER_TRAITS(1, ns::HourlyEmployee, firstName, lastName, wage, hours)
JSONCONS_TPL_ALL_CTOR_GETTER_TRAITS(1, ns::CommissionedEmployee, firstName, lastName, baseSalary, commission, sales)
JSONCONS_POLYMORPHIC_TRAITS(ns::Employee<cust_allocator<char>>, ns::HourlyEmployee<cust_allocator<char>>, ns::CommissionedEmployee<cust_allocator<char>>)

int main()
{
    std::string input = R"(
[
    {
        "firstName": "John",
        "hours": 1000,
        "lastName": "Smith",
        "wage": 40.0
    },
    {
        "baseSalary": 30000.0,
        "commission": 0.25,
        "firstName": "Jane",
        "lastName": "Doe",
        "sales": 1000
    }
]
    )";

    using element_type = std::shared_ptr<ns::Employee<cust_allocator<char>>>;
    using value_type = std::vector<element_type, cust_allocator<element_type>>;

    cust_allocator<char> alloc(1);
    cust_allocator<char> alloc_temp(2);
    auto aset = jsoncons::make_alloc_set(alloc, alloc_temp);
    auto r = jsoncons::try_decode_json<value_type>(aset, input);
    if (!r)
    {
        std::cout << r.error().message() << "\n";
    }

    value_type& v{*r};

    assert(2 == v.size());
    assert(v[0]->firstName().c_str() == "John");
    assert(v[0]->lastName().c_str() == "Smith");
    assert(v[0]->calculatePay() == 40000);
    assert(v[1]->firstName().c_str() == "Jane");
    assert(v[1]->lastName().c_str() == "Doe");
    assert(v[1]->calculatePay() == 30250);
}
```

### See also

[allocator_set](../allocator_set.md)  
[decode_json, try_decode_json](../decode_json.md)  
[encode_json, encode_json_pretty, try_encode_json, try_encode_json_pretty](../encode_json.md)  
[basic_json](../basic_json.md)