summaryrefslogtreecommitdiff
path: root/src/codec/data.h
blob: e181dc4849cc444b103d0057f1c236a09c74900c (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
#pragma once

#include <forstio/core/common.h>
#include "schema.h"

namespace saw {
namespace encode {
struct Native {};
}
/*
 * Helper for the basic message container, so the class doesn't have to be
 * specialized 10 times.
 */
template <class T> struct native_data_type;

template <>
struct native_data_type<schema::Primitive<schema::SignedInteger, 1>> {
	using type = int8_t;
};

template <>
struct native_data_type<schema::Primitive<schema::SignedInteger, 2>> {
	using type = int16_t;
};

template <>
struct native_data_type<schema::Primitive<schema::SignedInteger, 4>> {
	using type = int32_t;
};

template <>
struct native_data_type<schema::Primitive<schema::SignedInteger, 8>> {
	using type = int64_t;
};

template <>
struct native_data_type<schema::Primitive<schema::UnsignedInteger, 1>> {
	using type = uint8_t;
};

template <>
struct native_data_type<schema::Primitive<schema::UnsignedInteger, 2>> {
	using type = uint16_t;
};

template <>
struct native_data_type<schema::Primitive<schema::UnsignedInteger, 4>> {
	using type = uint32_t;
};

template <>
struct native_data_type<schema::Primitive<schema::UnsignedInteger, 8>> {
	using type = uint64_t;
};

template <>
struct native_data_type<schema::Primitive<schema::FloatingPoint, 4>> {
	using type = float;
};

template <>
struct native_data_type<schema::Primitive<schema::FloatingPoint, 8>> {
	using type = double;
};

template<typename T, typename Encoding = encode::Native>
class data {
private:
	static_assert(always_false<T>, "Type not supported");
};

template<typename T>
class data<schema::Array<T>, encode::Native> {
	private:
		std::vector<data<T, encode::Native>> value_;
	public:
		data(size_t size_){
			value_.resize(size_);
		}

		data<T>& at(size_t i){
			return value_.at(i);
		}

		size_t size() const { return value_;}
};

template<>
class data<schema::String, encode::Native> {
private:
	std::string value_;
public:
	SAW_FORBID_COPY(data);

	data() = default;
	data(std::string value__):value_{std::move(value__)}{}

	std::size_t size() const {
		return value_.size();
	}

	bool operator==(const data<schema::String, encode::Native>& data){
		return value_ == data.value_;
	}
};

template<typename T, size_t N>
class data<schema::Primitive<T,N>, encode::Native> {
private:
	typename native_data_type<schema::Primitive<T,N>>::type value_;
public:
	SAW_FORBID_COPY(data);

	data(typename native_data_type<schema::Primitive<T,N>::type value__):
		value_{std::move(value__)}{}

	void set(typename native_data_type<schema::Primitive<T,N>>::type val){
		value_ = val;
	}

	typename native_data_type<schema::Primitive<T,N>>::type get() const {return value_;}
};


}