libwreport  2.9
opcode.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005--2010 ARPA-SIM <urpsim@smr.arpa.emr.it>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16  *
17  * Author: Enrico Zini <enrico@enricozini.com>
18  */
19 
20 #ifndef WREPORT_OPCODE_H
21 #define WREPORT_OPCODE_H
22 
29 #include <wreport/varinfo.h>
30 #include <vector>
31 #include <cstdio>
32 
33 namespace wreport {
34 
35 namespace opcode {
36 struct Visitor;
37 }
38 
39 struct Vartable;
40 struct DTable;
41 
50 struct Opcodes
51 {
53  const std::vector<Varcode>& vals;
55  unsigned begin;
57  unsigned end;
58 
60  Opcodes(const std::vector<Varcode>& vals) : vals(vals), begin(0), end(vals.size()) {}
62  Opcodes(const std::vector<Varcode>& vals, unsigned begin, unsigned end)
63  : vals(vals), begin(begin), end(end) {}
65  Opcodes(const Opcodes& o) : vals(o.vals), begin(o.begin), end(o.end) {}
66 
73  {
74  begin = o.begin;
75  end = o.end;
76  return *this;
77  }
78 
80  Varcode operator[](unsigned i) const
81  {
82  if (begin + i > end)
83  return 0;
84  else
85  return vals[begin + i];
86  }
87 
89  unsigned size() const { return end - begin; }
90 
92  bool empty() const { return begin == end; }
93 
95  Varcode head() const
96  {
97  if (begin == end)
98  return 0;
99  return vals[begin];
100  }
101 
107  Opcodes next() const
108  {
109  if (begin == end)
110  return *this;
111  else
112  return Opcodes(vals, begin+1, end);
113  }
114 
116  Opcodes sub(unsigned skip) const
117  {
118  if (begin + skip > end)
119  return Opcodes(vals, end, end);
120  else
121  return Opcodes(vals, begin + skip, end);
122  }
123 
125  Opcodes sub(unsigned skip, unsigned len) const
126  {
127  if (begin + skip > end)
128  return Opcodes(vals, end, end);
129  else if (begin + skip + len > end)
130  return Opcodes(vals, begin + skip, end);
131  else
132  return Opcodes(vals, begin + skip, begin + skip + len);
133  }
134 
140  void visit(opcode::Visitor& e, const DTable& dtable) const;
141 
147  void visit(opcode::Visitor& e) const;
148 
150  void print(FILE* out) const;
151 };
152 
153 namespace opcode
154 {
155 
167 struct Visitor
168 {
174  const DTable* dtable;
175 
176  Visitor();
177  virtual ~Visitor();
178 
185  virtual void b_variable(Varcode code);
186 
196  virtual void c_modifier(Varcode code);
197 
206  virtual void c_change_data_width(Varcode code, int change);
207 
216  virtual void c_change_data_scale(Varcode code, int change);
217 
229  virtual void c_associated_field(Varcode code, Varcode sig_code, unsigned nbits);
230 
237  virtual void c_char_data(Varcode code);
238 
247  virtual void c_char_data_override(Varcode code, unsigned new_length);
248 
255  virtual void c_quality_information_bitmap(Varcode code);
256 
263  virtual void c_substituted_value_bitmap(Varcode code);
264 
271  virtual void c_substituted_value(Varcode code);
272 
283  virtual void c_local_descriptor(Varcode code, Varcode desc_code, unsigned nbits);
284 
296  virtual void r_replication(Varcode code, Varcode delayed_code, const Opcodes& ops);
297 
304  virtual void d_group_begin(Varcode code);
305 
312  virtual void d_group_end(Varcode code);
313 
322  virtual void c_increase_scale_ref_width(Varcode code, int change);
323 };
324 
329 class Printer : public Visitor
330 {
331 protected:
338  void print_lead(Varcode code);
339 
340 public:
346  FILE* out;
347 
354  const Vartable* btable;
355 
362  unsigned indent;
363 
365  unsigned indent_step;
366 
367  Printer();
368  virtual void b_variable(Varcode code);
369  virtual void c_modifier(Varcode code);
370  virtual void c_change_data_width(Varcode code, int change);
371  virtual void c_change_data_scale(Varcode code, int change);
372  virtual void c_associated_field(Varcode code, Varcode sig_code, unsigned nbits);
373  virtual void c_char_data(Varcode code);
374  virtual void c_char_data_override(Varcode code, unsigned new_length);
375  virtual void c_quality_information_bitmap(Varcode code);
376  virtual void c_substituted_value_bitmap(Varcode code);
377  virtual void c_substituted_value(Varcode code);
378  virtual void c_local_descriptor(Varcode code, Varcode desc_code, unsigned nbits);
379  virtual void r_replication(Varcode code, Varcode delayed_code, const Opcodes& ops);
380  virtual void d_group_begin(Varcode code);
381  virtual void d_group_end(Varcode code);
382 };
383 
384 }
385 
386 }
387 
388 #endif
Opcodes(const std::vector< Varcode > &vals, unsigned begin, unsigned end)
Sequence from begin (inclusive) to end (excluded)
Definition: opcode.h:62
virtual void c_substituted_value(Varcode code)
Notify a substituted value.
virtual void d_group_end(Varcode code)
Notify the end of a D group.
virtual void c_change_data_width(Varcode code, int change)
Notify a change of data width.
virtual void d_group_begin(Varcode code)
Notify the start of a D group.
virtual void c_char_data(Varcode code)
Notify raw character data encoded via a C modifier.
unsigned end
One-past-the-last element of the varcode sequence in Opcodes::vals.
Definition: opcode.h:57
const std::vector< Varcode > & vals
Reference to the vector with all the expanded varcodes.
Definition: opcode.h:53
virtual void b_variable(Varcode code)
Notify of a B variable entry.
void visit(opcode::Visitor &e, const DTable &dtable) const
Walk the structure of the opcodes sending events to an opcode::Visitor.
Opcodes sub(unsigned skip, unsigned len) const
Return len opcodes starting from skip.
Definition: opcode.h:125
virtual void c_substituted_value(Varcode code)
Notify a substituted value.
virtual void c_modifier(Varcode code)
Notify of a C modifier.
FILE * out
Output stream.
Definition: opcode.h:346
virtual void c_associated_field(Varcode code, Varcode sig_code, unsigned nbits)
Notify the declaration of an associated field for the next values.
virtual void d_group_begin(Varcode code)
Notify the start of a D group.
virtual void c_change_data_width(Varcode code, int change)
Notify a change of data width.
virtual void c_substituted_value_bitmap(Varcode code)
Notify a bitmap for substituted values.
virtual void d_group_end(Varcode code)
Notify the end of a D group.
virtual void c_local_descriptor(Varcode code, Varcode desc_code, unsigned nbits)
Notify the length of the following local descriptor.
unsigned indent
Current indent level.
Definition: opcode.h:362
const Vartable * btable
Table used to get variable descriptions (optional).
Definition: opcode.h:354
virtual void b_variable(Varcode code)
Notify of a B variable entry.
Holds a variable information table.
Definition: vartable.h:84
Visitor-style interface for scanning the contents of a data descriptor section.
Definition: opcode.h:167
unsigned size() const
Number of items in this opcode list.
Definition: opcode.h:89
virtual void r_replication(Varcode code, Varcode delayed_code, const Opcodes &ops)
Notify a replicated section.
Varcode head() const
First opcode in the list (0 if the list is empty)
Definition: opcode.h:95
virtual void c_local_descriptor(Varcode code, Varcode desc_code, unsigned nbits)
Notify the length of the following local descriptor.
Opcodes(const Opcodes &o)
Copy constructor.
Definition: opcode.h:65
virtual void c_quality_information_bitmap(Varcode code)
Notify a bitmap for quality information data.
Sequence of opcodes, as a slice of a Varcode vector.
Definition: opcode.h:50
D-table with Dxxyyy aggregate code expansions.
Definition: dtable.h:59
Opcodes(const std::vector< Varcode > &vals)
Sequence spanning the whole vector.
Definition: opcode.h:60
short unsigned int Varcode
Holds the WMO variable code of a variable.
Definition: varinfo.h:78
Implement fast access to information about WMO variables.
virtual void c_char_data_override(Varcode code, unsigned new_length)
Notify an override of character data length.
const DTable * dtable
D table to use to expand D groups.
Definition: opcode.h:174
virtual void c_char_data(Varcode code)
Notify raw character data encoded via a C modifier.
virtual void c_substituted_value_bitmap(Varcode code)
Notify a bitmap for substituted values.
virtual void c_increase_scale_ref_width(Varcode code, int change)
Notify an increase of scale, reference value and data width.
void print(FILE *out) const
Print the contents of this opcode list.
void print_lead(Varcode code)
Print line lead (indentation and formatted code)
virtual void c_change_data_scale(Varcode code, int change)
Notify a change of data scale.
Opcodes & operator=(const Opcodes &o)
Assignment only works if the Opcodes share the same vector.
Definition: opcode.h:72
virtual void c_modifier(Varcode code)
Notify of a C modifier.
virtual void c_quality_information_bitmap(Varcode code)
Notify a bitmap for quality information data.
unsigned begin
First element of the varcode sequence in Opcodes::vals.
Definition: opcode.h:55
Varcode operator[](unsigned i) const
Return the i-th varcode in the chain.
Definition: opcode.h:80
opcode::Visitor that pretty-prints the opcodes using indentation to show structure ...
Definition: opcode.h:329
virtual void c_associated_field(Varcode code, Varcode sig_code, unsigned nbits)
Notify the declaration of an associated field for the next values.
virtual void r_replication(Varcode code, Varcode delayed_code, const Opcodes &ops)
Notify a replicated section.
Opcodes sub(unsigned skip) const
Return the opcodes from skip until the end.
Definition: opcode.h:116
virtual void c_char_data_override(Varcode code, unsigned new_length)
Notify an override of character data length.
bool empty() const
True if there are no opcodes.
Definition: opcode.h:92
virtual void c_change_data_scale(Varcode code, int change)
Notify a change of data scale.
unsigned indent_step
How many spaces in an indentation level.
Definition: opcode.h:365
Opcodes next() const
List of all opcodes after the first one.
Definition: opcode.h:107