opcodes.h
#ifndef WREPORT_OPCODE_H
#define WREPORT_OPCODE_H
#include <wreport/varinfo.h>
#include <wreport/error.h>
#include <vector>
#include <cstdio>
namespace wreport {
struct Opcodes
{
const Varcode* begin;
const Varcode* end;
Opcodes(const std::vector<Varcode>& vals) : begin(vals.data()), end(begin + vals.size()) {}
Opcodes(const Varcode* begin, const Varcode* end)
: begin(begin), end(end) {}
Opcodes(const Opcodes& o) = default;
Opcodes& operator=(const Opcodes& o) = default;
Varcode operator[](unsigned i) const
{
if (begin + i > end)
return 0;
else
return begin[i];
}
unsigned size() const { return end - begin; }
bool empty() const { return begin == end; }
Varcode pop_left()
{
if (empty()) throw error_consistency("cannot do pop_left on an empty opcode sequence");
return *begin++;
}
Opcodes pop_left(unsigned count)
{
if (size() < count)
error_consistency::throwf("cannot do pop_left(%u) on an opcode sequence of only %u elements", count, size());
Opcodes res(begin, begin + count);
begin += count;
return res;
}
Varcode head() const
{
if (begin == end)
return 0;
return *begin;
}
Opcodes next() const
{
if (begin == end)
return *this;
else
return Opcodes(begin+1, end);
}
Opcodes sub(unsigned skip) const
{
if (begin + skip > end)
return Opcodes(end, end);
else
return Opcodes(begin + skip, end);
}
Opcodes sub(unsigned skip, unsigned len) const
{
if (begin + skip > end)
return Opcodes(end, end);
else if (begin + skip + len >= end)
return Opcodes(begin + skip, end);
else
return Opcodes(begin + skip, begin + skip + len);
}
void print(FILE* out) const;
};
}
#endif
wreport::error_consistency
Report an error when a consistency check failed.
Definition error.h:192
wreport::error_consistency::throwf
static void throwf(const char *fmt,...) WREPORT_THROWF_ATTRS(1
Throw the exception, building the message printf-style.
error.h
wreport exceptions.
wreport
String functions.
Definition benchmark.h:13
wreport::Varcode
uint16_t Varcode
Holds the WMO variable code of a variable.
Definition fwd.h:12
wreport::Opcodes
Sequence of opcodes, as a slice of a Varcode vector.
Definition opcodes.h:20
wreport::Opcodes::end
const Varcode * end
One-past-the-last element of the varcode sequence.
Definition opcodes.h:24
wreport::Opcodes::sub
Opcodes sub(unsigned skip, unsigned len) const
Return len opcodes starting from skip.
Definition opcodes.h:107
wreport::Opcodes::head
Varcode head() const
First opcode in the list (0 if the list is empty).
Definition opcodes.h:77
wreport::Opcodes::Opcodes
Opcodes(const std::vector< Varcode > &vals)
Sequence spanning the whole vector.
Definition opcodes.h:27
wreport::Opcodes::size
unsigned size() const
Number of items in this opcode list.
Definition opcodes.h:45
wreport::Opcodes::sub
Opcodes sub(unsigned skip) const
Return the opcodes from skip until the end.
Definition opcodes.h:98
wreport::Opcodes::next
Opcodes next() const
List of all opcodes after the first one.
Definition opcodes.h:89
wreport::Opcodes::operator[]
Varcode operator[](unsigned i) const
Return the i-th varcode in the chain.
Definition opcodes.h:36
wreport::Opcodes::pop_left
Varcode pop_left()
Return the first element and advance begin to the next one.
Definition opcodes.h:55
wreport::Opcodes::pop_left
Opcodes pop_left(unsigned count)
Return the first count elements and advance begin to the first opcode after the sequence.
Definition opcodes.h:67
wreport::Opcodes::print
void print(FILE *out) const
Print the contents of this opcode list.
wreport::Opcodes::begin
const Varcode * begin
First element of the varcode sequence.
Definition opcodes.h:22
wreport::Opcodes::empty
bool empty() const
True if there are no opcodes.
Definition opcodes.h:48
wreport::Opcodes::Opcodes
Opcodes(const Varcode *begin, const Varcode *end)
Sequence from begin (inclusive) to end (excluded).
Definition opcodes.h:29
varinfo.h
Implement fast access to information about WMO variables.