libdballe  7.29
codec.h
Go to the documentation of this file.
1 #ifndef DBA_MSG_CODEC_H
2 #define DBA_MSG_CODEC_H
3 
4 #include <dballe/file.h>
5 #include <dballe/message.h>
6 #include <memory>
7 #include <string>
8 #include <cstdio>
9 #include <functional>
10 
16 namespace wreport {
17 struct Bulletin;
18 }
19 
20 namespace dballe {
21 struct Messages;
22 struct Message;
23 
24 namespace msg {
25 
33 class Importer
34 {
35 public:
36  struct Options
37  {
38  bool simplified;
39 
42  : simplified(true) {}
43 
44  bool operator==(const Options& o) const { return simplified == o.simplified; }
45  bool operator!=(const Options& o) const { return simplified != o.simplified; }
46 
48  void print(FILE* out);
49 
51  std::string to_string() const;
52 
54  static Options from_string(const std::string& s);
55  };
56 
57 protected:
58  Options opts;
59 
60 public:
61  Importer(const Options& opts);
62  virtual ~Importer();
63 
72  Messages from_binary(const BinaryMessage& msg) const;
73 
86  virtual bool foreach_decoded(const BinaryMessage& msg, std::function<bool(std::unique_ptr<Message>&&)> dest) const = 0;
87 
91  virtual Messages from_bulletin(const wreport::Bulletin& msg) const = 0;
92 
93 
95  static std::unique_ptr<Importer> create(File::Encoding type, const Options& opts=Options());
96 };
97 
105 class Exporter
106 {
107 public:
108  struct Options
109  {
111  std::string template_name;
113  int centre;
118 
121  : centre(MISSING_INT), subcentre(MISSING_INT), application(MISSING_INT) {}
122 
124  void print(FILE* out);
125 
127  std::string to_string() const;
128  };
129 
130 protected:
131  Options opts;
132 
133 public:
134  Exporter(const Options& opts);
135  virtual ~Exporter();
136 
145  virtual std::string to_binary(const Messages& msgs) const = 0;
146 
150  virtual std::unique_ptr<wreport::Bulletin> to_bulletin(const Messages& msgs) const = 0;
151 
158  virtual std::unique_ptr<wreport::Bulletin> make_bulletin() const;
159 
160 
162  static std::unique_ptr<Exporter> create(File::Encoding type, const Options& opts=Options());
163 };
164 
165 }
166 }
167 
168 #endif
static std::unique_ptr< Exporter > create(File::Encoding type, const Options &opts=Options())
Instantiate the right importer for the given type.
Encoding
Supported encodings.
Definition: file.h:21
Definition: codec.h:108
virtual std::unique_ptr< wreport::Bulletin > to_bulletin(const Messages &msgs) const =0
Export to a Bulletin.
virtual Messages from_bulletin(const wreport::Bulletin &msg) const =0
Import a decoded BUFR/CREX message.
static Options from_string(const std::string &s)
Opposite of to_string: create an Options from a string.
Options()
Create new Options initialised with default values.
Definition: codec.h:120
virtual bool foreach_decoded(const BinaryMessage &msg, std::function< bool(std::unique_ptr< Message > &&)> dest) const =0
Decode a message from its raw encoded representation, calling dest on each resulting Message...
Definition: codec.h:36
virtual std::string to_binary(const Messages &msgs) const =0
Encode a message.
Message importer.
Definition: codec.h:33
std::string to_string() const
Generate a string summary of export options.
void print(FILE *out)
Print a summary of the options to out.
virtual std::unique_ptr< wreport::Bulletin > make_bulletin() const
Create a bulletin that works with this exporter.
int centre
Originating centre.
Definition: codec.h:113
std::string template_name
Name of template to use for output (leave empty to autodetect)
Definition: codec.h:111
int subcentre
Originating subcentre.
Definition: codec.h:115
static std::unique_ptr< Importer > create(File::Encoding type, const Options &opts=Options())
Instantiate the right importer for the given type.
void print(FILE *out)
Print a summary of the options to out.
Ordered collection of messages.
Definition: message.h:67
int application
Originating application ID.
Definition: codec.h:117
Options()
Create new Options initialised with default values.
Definition: codec.h:41
Message exporter.
Definition: codec.h:105
std::string to_string() const
Generate a string summary of import options.
Messages from_binary(const BinaryMessage &msg) const
Decode a message from its raw encoded representation.