-
Notifications
You must be signed in to change notification settings - Fork 0
/
message_optimization.h
88 lines (76 loc) · 3.2 KB
/
message_optimization.h
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
// Copyright © 2016 zendo (734181759@qq.com). All rights reserved.
#pragma once
#include <type_traits>
namespace zendo
{
namespace details
{
#pragma pack(push, 1)
template<typename Header, typename Body, bool HeaderEmpty, bool BodyEmpty> class message_optimization;
template<typename Header, typename Body>
class message_optimization<Header, Body, false, false>
{
public:
using header_type = Header;
using body_type = Body;
private:
header_type header_;
body_type body_;
public:
static constexpr bool header_empty() { return false; }
static constexpr bool body_empty() { return false; }
header_type* header() noexcept { return &header_; }
header_type const* header() const noexcept { return &header_; }
body_type* body() noexcept { return &body_; }
body_type const* body() const noexcept { return &body_; }
};
template<typename Header, typename Body>
class message_optimization<Header, Body, false, true>
{
public:
using header_type = Header;
using body_type = Body;
private:
header_type header_;
public:
static constexpr bool header_empty() { return false; }
static constexpr bool body_empty() { return true; }
header_type* header() noexcept { return &header_; }
header_type const* header() const noexcept { return &header_; }
body_type* body() noexcept { return nullptr; };
body_type const* body() const noexcept { return nullptr; };
};
template<typename Header, typename Body>
class message_optimization<Header, Body, true, false>
{
public:
using header_type = Header;
using body_type = Body;
private:
body_type body_;
public:
static constexpr bool header_empty() { return true; }
static constexpr bool body_empty() { return false; }
header_type* header() noexcept { return nullptr; };
header_type const* header() const noexcept { return nullptr; }
body_type* body() noexcept { return &body_; }
body_type const* body() const noexcept { return &body_; }
};
template<typename Header, typename Body>
class message_optimization<Header, Body, true, true>
{
public:
using header_type = Header;
using body_type = Body;
static constexpr bool header_empty() { return true; }
static constexpr bool body_empty() { return true; }
header_type* header() noexcept { return nullptr; };
header_type const* header() const noexcept { return nullptr; }
body_type* body() noexcept { return nullptr; }
body_type const* body() const noexcept { return nullptr; }
};
#pragma pack(pop)
}
template<typename Header, typename Body>
using message_optimization = details::message_optimization<Header, Body, std::is_empty<Header>::value, std::is_empty<Body>::value>;
}