forked from chronolaw/cpp_study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSalesData.hpp
More file actions
125 lines (97 loc) · 2.41 KB
/
SalesData.hpp
File metadata and controls
125 lines (97 loc) · 2.41 KB
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
// Copyright (c) 2020 by Chrono
#ifndef _SALES_DATA_HPP
#define _SALES_DATA_HPP
#include "cpplang.hpp"
#include <msgpack.hpp>
#if MSGPACK_VERSION_MAJOR < 2
# error "msgpack is too old"
#endif
BEGIN_NAMESPACE(cpp_study)
// demo oop in C++
class SalesData final
{
public:
using this_type = SalesData;
public:
using string_type = std::string;
using string_view_type = const std::string&;
using uint_type = unsigned int;
using currency_type = double;
STATIC_ASSERT(sizeof(uint_type) >= 4);
STATIC_ASSERT(sizeof(currency_type) >= 4);
public:
SalesData(string_view_type id, uint_type s, currency_type r) noexcept
: m_id(id), m_sold(s), m_revenue(r)
{}
SalesData(string_view_type id) noexcept
: SalesData(id, 0, 0)
{}
public:
#if 0
SalesData(SalesData&& s) noexcept
: m_id(std::move(s.m_id)),
m_sold(std::move(s.m_sold)),
m_revenue(std::move(s.m_revenue))
{}
SalesData& operator=(SalesData&& s) noexcept
{
m_id = std::move(s.m_id);
m_sold = std::move(s.m_sold);
m_revenue = std::move(s.m_revenue);
return *this;
}
#endif
SalesData() = default;
~SalesData() = default;
SalesData(const this_type&) = default;
SalesData& operator=(const this_type&) = default;
SalesData(this_type&& s) = default;
SalesData& operator=(this_type&& s) = default;
private:
string_type m_id = "";
uint_type m_sold = 0;
uint_type m_revenue = 0;
public:
MSGPACK_DEFINE(m_id, m_sold, m_revenue);
msgpack::sbuffer pack() const
{
msgpack::sbuffer sbuf;
msgpack::pack(sbuf, *this);
return sbuf;
}
SalesData(const msgpack::sbuffer& sbuf)
{
auto obj = msgpack::unpack(
sbuf.data(), sbuf.size()).get();
obj.convert(*this);
}
public:
void inc_sold(uint_type s) noexcept
{
m_sold += s;
}
void inc_revenue(currency_type r) noexcept
{
m_revenue += r;
}
public:
string_view_type id() const noexcept
{
return m_id;
}
uint_type sold() const noexcept
{
return m_sold;
}
currency_type revenue() const noexcept
{
return m_revenue;
}
CPP_DEPRECATED
currency_type average() const
{
return m_revenue / m_sold;
}
};
END_NAMESPACE(cpp_study)
#endif //_SALES_DATA_HPP