libdballe  7.7
message.h
1 #ifndef DBALLE_MESSAGE_H
2 #define DBALLE_MESSAGE_H
3 
4 #include <dballe/types.h>
5 #include <wreport/varinfo.h>
6 #include <vector>
7 #include <memory>
8 #include <iterator>
9 
10 namespace wreport {
11 struct Var;
12 }
13 
14 namespace dballe {
15 
25 struct Message
26 {
27  virtual ~Message();
28 
30  virtual Datetime get_datetime() const = 0;
31 
33  virtual std::unique_ptr<Message> clone() const = 0;
34 
41  virtual const wreport::Var* get(wreport::Varcode code, const Level& lev, const Trange& tr) const = 0;
42 
44  virtual void print(FILE* out) const = 0;
45 
55  virtual unsigned diff(const Message& msg) const = 0;
56 };
57 
64 class Messages
65 {
66 protected:
67  std::vector<Message*> msgs;
68 
69  // Convert an iterator over pointers to an iterator over values
70  template<typename VAL, typename WRAPPED>
71  class base_iterator : public std::iterator<std::random_access_iterator_tag, VAL, typename WRAPPED::difference_type, VAL*, VAL&>
72  {
73  protected:
74  WRAPPED current;
75 
76  public:
77  base_iterator() {}
78  explicit base_iterator(const WRAPPED& o) : current(o) {}
79 
80  // Forward iterator requirements
81  inline VAL& operator*() const { return **current; }
82  inline VAL* operator->() const { return *current; }
83  inline base_iterator& operator++() { ++current; return *this; }
84  inline base_iterator operator++(int) { return base_iterator(current++); }
85 
86  // Bidirectional iterator requirements
87  inline base_iterator& operator--() { --current; return *this; }
88  inline base_iterator operator--(int) { return base_iterator(current--); }
89 
90  // Random access iterator requirements
91  inline VAL& operator[](typename WRAPPED::difference_type n) const { return *current[n]; }
92  inline base_iterator& operator+=(typename WRAPPED::difference_type n) { current += n; return *this; }
93  inline base_iterator operator+(typename WRAPPED::difference_type n) const { return base_iterator(current + n); }
94  inline base_iterator& operator-=(typename WRAPPED::difference_type n) { current -= n; return *this; }
95  inline base_iterator operator-(typename WRAPPED::difference_type n) const { return base_iterator(current - n); }
96 
97  // Forward iterator requirements
98  template<typename O> inline bool operator==(const O& o) const { return current == o.current; }
99  template<typename O> inline bool operator!=(const O& o) const { return current != o.current; }
100  template<typename O> inline bool operator<(const O& o) const { return current < o.current; }
101  template<typename O> inline bool operator<=(const O& o) const { return current <= o.current; }
102  template<typename O> inline bool operator>(const O& o) const { return current > o.current; }
103  template<typename O> inline bool operator>=(const O& o) const { return current >= o.current; }
104  template<typename O> inline typename WRAPPED::difference_type operator-(const O& o) const { return current - o.current; }
105  };
106 
107 public:
110 
111  Messages();
112  Messages(const Messages& o);
113  Messages(Messages&& o);
114  ~Messages();
115 
116  Messages& operator=(const Messages& o);
117  Messages& operator=(Messages&& o);
118 
119  iterator begin() { return iterator(msgs.begin()); }
120  iterator end() { return iterator(msgs.end()); }
121  const_iterator begin() const { return const_iterator(msgs.begin()); }
122  const_iterator end() const { return const_iterator(msgs.end()); }
123 
124  Message& operator[](size_t pos) { return *msgs[pos]; }
125  const Message& operator[](size_t pos) const { return *msgs[pos]; }
126 
128  bool empty() const;
129 
131  size_t size() const;
132 
134  void append(const Message& msg);
135 
137  void append(std::unique_ptr<Message>&& msg);
138 
140  void clear();
141 
143  void print(FILE* out) const;
144 
156  unsigned diff(const Messages& msgs) const;
157 };
158 
159 }
160 #endif
void append(const Message &msg)
Append a copy of the message.
Common base types used by most of DB-All.e code.
size_t size() const
Return the number of messages.
Information on how a value has been sampled or computed with regards to time.
Definition: types.h:565
A bulletin that has been decoded and physically interpreted.
Definition: message.h:25
virtual std::unique_ptr< Message > clone() const =0
Return a copy of this message.
Copyright (C) 2008–2010 ARPA-SIM urpsim@smr.arpa.emr.it
Definition: cmdline.h:17
Definition: message.h:71
Vertical level or layer.
Definition: types.h:515
virtual Datetime get_datetime() const =0
Get the reference Datetime for this message.
void clear()
Remove all messages.
Ordered collection of messages.
Definition: message.h:64
bool empty() const
Check if the collection is empty.
Definition: conversion.h:6
virtual void print(FILE *out) const =0
Print all the contents of this message to an output stream.
Date and time.
Definition: types.h:147
void print(FILE *out) const
Print all the contents of all the messages to an output stream.
unsigned diff(const Messages &msgs) const
Compute the differences between two Messages.
virtual unsigned diff(const Message &msg) const =0
Compute the differences between two Messages.