libwreport 3.38
opcodes.h
1#ifndef WREPORT_OPCODE_H
2#define WREPORT_OPCODE_H
3
4#include <wreport/varinfo.h>
5#include <wreport/error.h>
6#include <vector>
7#include <cstdio>
8
9namespace wreport {
10
19struct Opcodes
20{
22 const Varcode* begin;
24 const Varcode* end;
25
27 Opcodes(const std::vector<Varcode>& vals) : begin(vals.data()), end(begin + vals.size()) {}
30 : begin(begin), end(end) {}
31
32 Opcodes(const Opcodes& o) = default;
33 Opcodes& operator=(const Opcodes& o) = default;
34
36 Varcode operator[](unsigned i) const
37 {
38 if (begin + i > end)
39 return 0;
40 else
41 return begin[i];
42 }
43
45 unsigned size() const { return static_cast<unsigned>(end - begin); }
46
48 bool empty() const { return begin == end; }
49
56 {
57 if (empty()) throw error_consistency("cannot do pop_left on an empty opcode sequence");
58 return *begin++;
59 }
60
67 Opcodes pop_left(unsigned count)
68 {
69 if (size() < count)
70 error_consistency::throwf("cannot do pop_left(%u) on an opcode sequence of only %u elements", count, size());
71 Opcodes res(begin, begin + count);
72 begin += count;
73 return res;
74 }
75
77 Varcode head() const
78 {
79 if (begin == end)
80 return 0;
81 return *begin;
82 }
83
89 Opcodes next() const
90 {
91 if (begin == end)
92 return *this;
93 else
94 return Opcodes(begin+1, end);
95 }
96
98 Opcodes sub(unsigned skip) const
99 {
100 if (begin + skip > end)
101 return Opcodes(end, end);
102 else
103 return Opcodes(begin + skip, end);
104 }
105
107 Opcodes sub(unsigned skip, unsigned len) const
108 {
109 if (begin + skip > end)
110 return Opcodes(end, end);
111 else if (begin + skip + len >= end)
112 return Opcodes(begin + skip, end);
113 else
114 return Opcodes(begin + skip, begin + skip + len);
115 }
116
118 void print(FILE* out) const;
119};
120
121}
122#endif
Report an error when a consistency check failed.
Definition error.h:192
static void throwf(const char *fmt,...) WREPORT_THROWF_ATTRS(1
Throw the exception, building the message printf-style.
wreport exceptions.
String functions.
Definition benchmark.h:13
uint16_t Varcode
Holds the WMO variable code of a variable.
Definition fwd.h:12
Sequence of opcodes, as a slice of a Varcode vector.
Definition opcodes.h:20
const Varcode * end
One-past-the-last element of the varcode sequence.
Definition opcodes.h:24
Opcodes sub(unsigned skip, unsigned len) const
Return len opcodes starting from skip.
Definition opcodes.h:107
Varcode head() const
First opcode in the list (0 if the list is empty)
Definition opcodes.h:77
Opcodes(const std::vector< Varcode > &vals)
Sequence spanning the whole vector.
Definition opcodes.h:27
unsigned size() const
Number of items in this opcode list.
Definition opcodes.h:45
Opcodes sub(unsigned skip) const
Return the opcodes from skip until the end.
Definition opcodes.h:98
Opcodes next() const
List of all opcodes after the first one.
Definition opcodes.h:89
Varcode operator[](unsigned i) const
Return the i-th varcode in the chain.
Definition opcodes.h:36
Varcode pop_left()
Return the first element and advance begin to the next one.
Definition opcodes.h:55
Opcodes pop_left(unsigned count)
Return the first count elements and advance begin to the first opcode after the sequence.
Definition opcodes.h:67
void print(FILE *out) const
Print the contents of this opcode list.
const Varcode * begin
First element of the varcode sequence.
Definition opcodes.h:22
bool empty() const
True if there are no opcodes.
Definition opcodes.h:48
Opcodes(const Varcode *begin, const Varcode *end)
Sequence from begin (inclusive) to end (excluded)
Definition opcodes.h:29
Implement fast access to information about WMO variables.