summaryrefslogtreecommitdiff
path: root/modules/codec/c++/forst.hpp
blob: 8dd3615fcd00eb655ebedd67ee58fd0e10800ab6 (plain)
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
#pragma once

#include <forstio/buffer.hpp>

#include "data.hpp"
#include "stream_value.hpp"


namespace saw {
namespace encode {
struct KelForst {};
}
}

#include "forst.tmpl.hpp"

namespace saw {
template <>
class data<schema::String, encode::KelForst> {
private:
	own<buffer> buff_;
public:
	data(own<buffer> buff):
		buff_{std::move(buff)}
	{}
};

template<typename T, uint64_t N>
class data<schema::Primitive<T,N>, encode::KelForst> {
public:
	using Schema = schema::Primitive<T,N>;
private:
	our<buffer> buff_;
	uint64_t displacement_;
public:
	data():
		buff_{share<array_buffer>(N)},
		displacement_{0u}
	{
		buff_->write_advance(N);
	}

	/**
	 * Constructor for root elements
	 */
	data(our<buffer> buff):
		buff_{std::move(buff)},
		displacement_{0u}
	{
		buff_->write_advance(N);
	}
	
	/**
	 * Constructor for child elements
	 */
	data(our<buffer> buff, uint64_t dsp):
		buff_{std::move(buff)},
		displacement_{dsp}
	{
		buff_->write_advance(N);
	}

	/**
	 * Get values
	 */
	typename native_data_type<Schema>::type get(){
		typename native_data_type<Schema>::type val;

		typename native_data_type<schema::Primitive<schema::UnsignedInteger,N>>::type raw = 0;

		for (size_t i = 0; i < N; ++i) {
			raw |= (static_cast<typename native_data_type<Schema>::type>(buff_->read(i+displacement_)) << (i * 8));
		}

		memcpy(&val, &raw, N);
		return val;
	}

	/**
	 * Set values
	 */
	void set(typename native_data_type<Schema>::type val){
		typename native_data_type<schema::Primitive<schema::UnsignedInteger,N>>::type raw{};
		memcpy(&raw, &val, N);

		for (size_t i = 0; i < N; ++i) {
			buff_->read(i+displacement_) = raw >> (i * 8);
		}	
	}
};

template<typename... T, string_literal... Keys>
class data<schema::Struct<schema::Member<T,Keys>...>, encode::KelForst> {
public:
	using Schema = schema::Struct<schema::Member<T,Keys>...>;
private:
	/**
	 * Buffer holding the whole packet
	 */
	our<buffer> buff_;
	/**
	 * Displacement to show the starting point
	 */
	uint64_t displacement_;
public:
	data():
		buff_{share<array_buffer>(impl::forst_codec_info<Schema>::static_size)},
		displacement_{0u}
	{
	}
	/**
	 * Constructor for root elements
	 */
	data(our<buffer> buff):
		buff_{std::move(buff)},
		displacement_{0u}
	{}

	/**
	 * Constructor for child elements
	 */
	data(our<buffer> buff, uint64_t dsp):
		buff_{std::move(buff)},
		displacement_{dsp}
	{}
};
}