libwreport  3.40
opcodes.h
1 #ifndef WREPORT_OPCODE_H
2 #define WREPORT_OPCODE_H
3 
4 #include <cstdio>
5 #include <vector>
6 #include <wreport/error.h>
7 #include <wreport/varinfo.h>
8 
9 namespace wreport {
10 
19 struct Opcodes
20 {
22  const Varcode* begin;
24  const Varcode* end;
25 
27  Opcodes(const std::vector<Varcode>& vals)
28  : begin(vals.data()), end(begin + vals.size())
29  {
30  }
32  Opcodes(const Varcode* begin, const Varcode* end) : begin(begin), end(end)
33  {
34  }
35 
36  Opcodes(const Opcodes& o) = default;
37  Opcodes& operator=(const Opcodes& o) = default;
38 
40  Varcode operator[](unsigned i) const
41  {
42  if (begin + i > end)
43  return 0;
44  else
45  return begin[i];
46  }
47 
49  unsigned size() const { return static_cast<unsigned>(end - begin); }
50 
52  bool empty() const { return begin == end; }
53 
60  {
61  if (empty())
62  throw error_consistency(
63  "cannot do pop_left on an empty opcode sequence");
64  return *begin++;
65  }
66 
73  Opcodes pop_left(unsigned count)
74  {
75  if (size() < count)
76  error_consistency::throwf("cannot do pop_left(%u) on an opcode "
77  "sequence of only %u elements",
78  count, size());
79  Opcodes res(begin, begin + count);
80  begin += count;
81  return res;
82  }
83 
85  Varcode head() const
86  {
87  if (begin == end)
88  return 0;
89  return *begin;
90  }
91 
97  Opcodes next() const
98  {
99  if (begin == end)
100  return *this;
101  else
102  return Opcodes(begin + 1, end);
103  }
104 
106  Opcodes sub(unsigned skip) const
107  {
108  if (begin + skip > end)
109  return Opcodes(end, end);
110  else
111  return Opcodes(begin + skip, end);
112  }
113 
115  Opcodes sub(unsigned skip, unsigned len) const
116  {
117  if (begin + skip > end)
118  return Opcodes(end, end);
119  else if (begin + skip + len >= end)
120  return Opcodes(begin + skip, end);
121  else
122  return Opcodes(begin + skip, begin + skip + len);
123  }
124 
126  void print(FILE* out) const;
127 };
128 
129 } // namespace wreport
130 #endif
Opcodes sub(unsigned skip) const
Return the opcodes from skip until the end.
Definition: opcodes.h:106
Opcodes pop_left(unsigned count)
Return the first count elements and advance begin to the first opcode after the sequence.
Definition: opcodes.h:73
Varcode operator[](unsigned i) const
Return the i-th varcode in the chain.
Definition: opcodes.h:40
wreport exceptions.
Opcodes sub(unsigned skip, unsigned len) const
Return len opcodes starting from skip.
Definition: opcodes.h:115
const Varcode * end
One-past-the-last element of the varcode sequence.
Definition: opcodes.h:24
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:19
Opcodes(const std::vector< Varcode > &vals)
Sequence spanning the whole vector.
Definition: opcodes.h:27
const Varcode * begin
First element of the varcode sequence.
Definition: opcodes.h:22
String functions.
Definition: benchmark.h:13
Implement fast access to information about WMO variables.
Opcodes next() const
List of all opcodes after the first one.
Definition: opcodes.h:97
bool empty() const
True if there are no opcodes.
Definition: opcodes.h:52
Varcode head() const
First opcode in the list (0 if the list is empty)
Definition: opcodes.h:85
Opcodes(const Varcode *begin, const Varcode *end)
Sequence from begin (inclusive) to end (excluded)
Definition: opcodes.h:32
Varcode pop_left()
Return the first element and advance begin to the next one.
Definition: opcodes.h:59
void print(FILE *out) const
Print the contents of this opcode list.
Report an error when a consistency check failed.
Definition: error.h:190
static void throwf(const char *fmt,...) WREPORT_THROWF_ATTRS(1
Throw the exception, building the message printf-style.
unsigned size() const
Number of items in this opcode list.
Definition: opcodes.h:49