Bond
 
Loading...
Searching...
No Matches
output_counter.h
1// Copyright (c) Microsoft. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
4#pragma once
5
6#include <bond/core/config.h>
7
8#include <bond/core/blob.h>
9
10namespace bond
11{
12
13template <typename T, uint32_t N>
14struct VariableUnsigned
15{
16 static void Write(uint32_t& count, T value)
17 {
18 BOOST_STATIC_ASSERT(N < 10);
19
20 if (value)
21 VariableUnsigned<T, N+1>::Write(count, value >> 7);
22 else
23 count += N;
24 }
25};
26
27template <>
28struct VariableUnsigned<uint64_t, 10>
29{
30 static void Write(uint32_t& count, uint64_t /*value*/)
31 {
32 count += 10;
33 }
34};
35
36template <>
37struct VariableUnsigned<uint32_t, 5>
38{
39 static void Write(uint32_t& count, uint32_t /*value*/)
40 {
41 count += 5;
42 }
43};
44
45template <>
46struct VariableUnsigned<uint16_t, 3>
47{
48 static void Write(uint32_t& count, uint16_t /*value*/)
49 {
50 count += 3;
51 }
52};
53
54class OutputCounter
55{
56
57 struct Buffer
58 {
59 uint32_t _size;
60
61 uint32_t size() const
62 {
63 return _size;
64 }
65 };
66
67public:
68 OutputCounter()
69 : _count(0)
70 {}
71
72 uint32_t GetCount() const
73 {
74 return _count;
75 }
76
77 template<typename T>
78 void Write(const T&)
79 {
80 _count += sizeof(T);
81 }
82
83 void Write(const void*, uint32_t size)
84 {
85 _count += size;
86 }
87
88 void Write(const blob& buffer)
89 {
90 _count += buffer.size();
91 }
92
93 void Write(const Buffer& buffer)
94 {
95 _count += buffer.size();
96 }
97
98 template<typename T>
99 void WriteVariableUnsigned(T value)
100 {
101 VariableUnsigned<T, 1>::Write(_count, value >> 7);
102 }
103
104 Buffer GetBuffer() const
105 {
106 return { GetCount() };
107 }
108
109private:
110 uint32_t _count;
111};
112
113
114inline OutputCounter CreateOutputBuffer(const OutputCounter& /*other*/)
115{
116 return OutputCounter();
117}
118
119
120} // namespace bond
namespace bond
Definition: apply.h:17