comservatory
Strict validation of CSV files in C++
Loading...
Searching...
No Matches
Field.hpp
Go to the documentation of this file.
1#ifndef COMSERVATORY_FIELD_HPP
2#define COMSERVATORY_FIELD_HPP
3
4#include <vector>
5#include <string>
6#include <complex>
7#include <numeric>
8#include "Type.hpp"
9
16namespace comservatory {
17
23struct Field {
27 virtual ~Field() {}
35 virtual size_t size() const = 0;
36
40 virtual Type type() const = 0;
41
45 virtual void add_missing() = 0;
46
51 virtual bool filled() const {
52 return true;
53 }
54};
55
67 size_t nrecords = 0;
72 size_t size() const {
73 return nrecords;
74 }
75
76 Type type() const {
77 return UNKNOWN;
78 }
79
80 void add_missing() {
81 ++nrecords;
82 return;
83 }
84};
85
91template<typename T, Type tt>
92struct TypedField : public Field {
93 Type type() const { return tt; }
94
98 virtual void push_back(T x) = 0;
99};
100
106template<typename T, Type tt>
107struct FilledField : public TypedField<T, tt> {
112 FilledField(size_t n = 0) : missing(n), values(n) {
113 if (n) {
114 std::iota(missing.begin(), missing.end(), 0);
115 }
116 }
117
121 std::vector<size_t> missing;
122 std::vector<T> values;
127 size_t size() const {
128 return values.size();
129 }
130
131 void push_back(T x) {
132 values.emplace_back(std::move(x));
133 return;
134 }
135
136 void add_missing() {
137 size_t i = values.size();
138 missing.push_back(i);
139 values.resize(i + 1);
140 return;
141 }
142};
143
149template<typename T, Type tt>
150struct DummyField : public TypedField<T, tt> {
155 DummyField(size_t n = 0) : nrecords(n) {}
156
160 size_t nrecords;
165 size_t size() const {
166 return nrecords;
167 }
168
169 void push_back(T) {
170 ++nrecords;
171 return;
172 }
173
174 void add_missing() {
175 ++nrecords;
176 return;
177 }
178
179 bool filled() const {
180 return false;
181 }
182};
183
190
195
200
205
210
215
220
225
230
235
240
245
246}
247
248#endif
Defines the Type enumeration for field types.
Contains all comservatory functions and classes.
Definition: Field.hpp:16
TypedField< std::string, STRING > StringField
Definition: Field.hpp:189
TypedField< bool, BOOLEAN > BooleanField
Virtual class for a Field of booleans.
Definition: Field.hpp:219
DummyField< std::complex< double >, COMPLEX > DummyComplexField
Definition: Field.hpp:244
Type
Definition: Type.hpp:15
TypedField< std::complex< double >, COMPLEX > ComplexField
Definition: Field.hpp:234
FilledField< double, NUMBER > FilledNumberField
Definition: Field.hpp:209
FilledField< std::string, STRING > FilledStringField
Definition: Field.hpp:194
FilledField< std::complex< double >, COMPLEX > FilledComplexField
Definition: Field.hpp:239
FilledField< bool, BOOLEAN > FilledBooleanField
Definition: Field.hpp:224
DummyField< bool, BOOLEAN > DummyBooleanField
Definition: Field.hpp:229
DummyField< std::string, STRING > DummyStringField
Definition: Field.hpp:199
TypedField< double, NUMBER > NumberField
Definition: Field.hpp:204
DummyField< double, NUMBER > DummyNumberField
Definition: Field.hpp:214
Template class for a typed dummy field that only pretends to store its values.
Definition: Field.hpp:150
bool filled() const
Definition: Field.hpp:179
void push_back(T)
Definition: Field.hpp:169
DummyField(size_t n=0)
Definition: Field.hpp:155
size_t size() const
Definition: Field.hpp:165
void add_missing()
Definition: Field.hpp:174
Virtual base class for a describing a field.
Definition: Field.hpp:23
virtual Type type() const =0
virtual bool filled() const
Definition: Field.hpp:51
virtual void add_missing()=0
virtual size_t size() const =0
Template class for a typed field that uses a std::vector to store its values.
Definition: Field.hpp:107
void push_back(T x)
Definition: Field.hpp:131
void add_missing()
Definition: Field.hpp:136
size_t size() const
Definition: Field.hpp:127
FilledField(size_t n=0)
Definition: Field.hpp:112
Template class for a field of a known type.
Definition: Field.hpp:92
virtual void push_back(T x)=0
Type type() const
Definition: Field.hpp:93
Field of an unknown type.
Definition: Field.hpp:63
void add_missing()
Definition: Field.hpp:80
Type type() const
Definition: Field.hpp:76
size_t size() const
Definition: Field.hpp:72