libwreport  2.14
varinfo.h
Go to the documentation of this file.
1 /*
2  * wreport/varinfo - Variable information
3  *
4  * Copyright (C) 2005--2011 ARPA-SIM <urpsim@smr.arpa.emr.it>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * Author: Enrico Zini <enrico@enricozini.com>
20  */
21 
22 #ifndef WREPORT_VARINFO_H
23 #define WREPORT_VARINFO_H
24 
25 #include <stdint.h>
26 #include <string>
27 
28 namespace wreport {
29 
78 typedef short unsigned int Varcode;
79 
81 std::string varcode_format(Varcode code);
82 
86 #define WR_VAR(f, x, y) ((wreport::Varcode)( ((unsigned)(f)<<14) | ((unsigned)(x)<<8) | (unsigned)(y) ))
87 
94 #define WR_STRING_TO_VAR(str) ((wreport::Varcode)( \
95  (( ((str)[0] - '0')*10 + ((str)[1] - '0') ) << 8) | \
96  ( ((str)[2] - '0')*100 + ((str)[3] - '0')*10 + ((str)[4] - '0') ) \
97 ))
98 
102 #define WR_VAR_F(code) (((code) >> 14) & 0x3)
103 
106 #define WR_VAR_X(code) ((code) >> 8 & 0x3f)
107 
110 #define WR_VAR_Y(code) ((code) & 0xff)
111 
122 Varcode descriptor_code(const char* desc);
123 
124 
130 typedef short unsigned int Alteration;
131 
136 #define VARINFO_FLAG_STRING 0x01
137 #define VARINFO_FLAG_BINARY 0x02
138 
148 struct _Varinfo
149 {
151  Varcode var;
153  char desc[64];
155  char unit[24];
158  int scale;
161  int ref;
164  unsigned len;
167  int bit_ref;
170  unsigned bit_len;
172  unsigned flags;
174  int imin;
176  int imax;
178  double dmin;
180  double dmax;
182  Alteration alteration;
184  mutable struct _Varinfo* alterations;
186  char bufr_unit[24];
189 
191  mutable int _ref;
192 
193  _Varinfo();
194 
196  void do_ref() const { ++_ref; }
197 
202  bool do_unref() const { return (--_ref) == 0; }
203 
205  bool is_string() const
206  {
207  return (flags & VARINFO_FLAG_STRING) != 0;
208  }
209 
211  bool is_binary() const
212  {
213  return (flags & VARINFO_FLAG_BINARY) != 0;
214  }
215 
225  int encode_int(double fval) const throw ();
226 
236  unsigned encode_bit_int(double fval) const;
237 
247  double decode_int(int val) const throw ();
248 
258  double bufr_decode_int(uint32_t val) const throw ();
259 
263  void reset();
264 
270  void set(Varcode var, const char* desc, const char* unit, int scale = 0, int ref = 0, int len = 0, int bit_ref = 0, int bit_len = 0, int flags = 0, const char* bufr_unit = 0, int bufr_scale = 0);
271 
279  void set_string(Varcode var, const char* desc, int len);
280 
285  void compute_range();
286 };
287 
288 class Varinfo;
289 
292 {
293 protected:
296 
297 public:
299  MutableVarinfo(_Varinfo* impl) : m_impl(impl) { m_impl->do_ref(); }
301  MutableVarinfo(const MutableVarinfo& vi) : m_impl(vi.m_impl) { m_impl->do_ref(); }
303  ~MutableVarinfo() { if (m_impl->do_unref()) delete m_impl; }
304 
308  {
309  vi.m_impl->do_ref();
310  if (m_impl->do_unref()) delete m_impl;
311  m_impl = vi.m_impl;
312  return *this;
313  }
314  _Varinfo* operator->() { return m_impl; }
315  _Varinfo& operator*() { return *m_impl; }
317 
319  _Varinfo* impl() const { return m_impl; }
320 
331 
332  friend class wreport::Varinfo;
333 };
334 
336 class Varinfo
337 {
338 protected:
340  const _Varinfo* m_impl;
341 
342 public:
344  Varinfo(const _Varinfo* impl) : m_impl(impl) { m_impl->do_ref(); }
346  Varinfo(const _Varinfo& impl) : m_impl(&impl) { m_impl->do_ref(); }
347  Varinfo(const Varinfo& vi) : m_impl(vi.m_impl) { m_impl->do_ref(); }
348  Varinfo(const MutableVarinfo& vi) : m_impl(vi.m_impl) { m_impl->do_ref(); }
350  ~Varinfo() { if (m_impl->do_unref()) delete m_impl; }
351 
353  const Varinfo& operator=(const Varinfo& vi)
355  {
356  vi.m_impl->do_ref();
357  if (m_impl->do_unref()) delete m_impl;
358  m_impl = vi.m_impl;
359  return *this;
360  }
361  const _Varinfo& operator*() const { return *m_impl; }
362  const _Varinfo* operator->() const { return m_impl; }
364 
366  const _Varinfo* impl() const { return m_impl; }
367 };
368 
369 }
370 
371 #endif
372 /* vim:set ts=4 sw=4: */
Varcode var
The variable code.
Definition: varinfo.h:151
Alteration alteration
C-table alteration that has been applied to this entry (deprecated)
Definition: varinfo.h:182
double dmax
Maximum scaled value the field can have.
Definition: varinfo.h:180
const _Varinfo * operator->() const
Standard smart pointer methods.
Definition: varinfo.h:362
Smart pointer to handle/use varinfos.
Definition: varinfo.h:291
const _Varinfo & operator*() const
Standard smart pointer methods.
Definition: varinfo.h:361
int scale
The scale of the variable.
Definition: varinfo.h:158
#define VARINFO_FLAG_BINARY
Mark literal binary variables.
Definition: varinfo.h:137
int ref
The reference value for the variable.
Definition: varinfo.h:161
_Varinfo * m_impl
Varinfo structure to which the pointer refers.
Definition: varinfo.h:295
bool is_string() const
Check if we are a string value.
Definition: varinfo.h:205
_Varinfo * operator->()
Standard smart pointer methods.
Definition: varinfo.h:314
const _Varinfo * impl() const
Access the underlying _Varinfo structure.
Definition: varinfo.h:366
MutableVarinfo(const MutableVarinfo &vi)
Create a smart pointer to the given variable information.
Definition: varinfo.h:301
double dmin
Minimum scaled value the field can have.
Definition: varinfo.h:178
int bufr_scale
The scale of the variable when encoded in BUFR.
Definition: varinfo.h:188
void do_ref() const
Increment the reference count to this Data object.
Definition: varinfo.h:196
int _ref
Reference count.
Definition: varinfo.h:191
_Varinfo & operator*()
Standard smart pointer methods.
Definition: varinfo.h:315
Holds the information about a DBALLE variable.
Definition: varinfo.h:148
bool is_binary() const
Check if we are a binary value.
Definition: varinfo.h:211
_Varinfo * impl() const
Access the underlying _Varinfo structure.
Definition: varinfo.h:319
int bit_ref
The reference value for bit-encoding.
Definition: varinfo.h:167
short unsigned int Alteration
Describes how a wreport::Varinfo has been altered: it is used for supporting variables coming from BU...
Definition: varinfo.h:130
std::string varcode_format(Varcode code)
Format a varcode into a string.
MutableVarinfo & operator=(const MutableVarinfo &vi)
Standard smart pointer methods.
Definition: varinfo.h:307
short unsigned int Varcode
Holds the WMO variable code of a variable.
Definition: varinfo.h:78
Varcode descriptor_code(const char *desc)
Convert a FXXYYY string descriptor code into its short integer representation.
unsigned bit_len
The length in bits of the variable when encoded in a bit string (after scaling and changing reference...
Definition: varinfo.h:170
Main namespace.
Definition: buffers.h:30
Smart pointer to handle/use varinfos.
Definition: varinfo.h:336
int imax
Maximum unscaled value the field can have.
Definition: varinfo.h:176
unsigned flags
Variable flags (see VARINFO_FLAG_* constants)
Definition: varinfo.h:172
const Varinfo & operator=(const Varinfo &vi)
Standard smart pointer methods.
Definition: varinfo.h:354
static MutableVarinfo create_singleuse()
Create a single use varinfo structure.
unsigned len
The length in digits of the integer representation of this variable (after scaling and changing refer...
Definition: varinfo.h:164
Varinfo(const Varinfo &vi)
Create a smart pointer to the given variable information.
Definition: varinfo.h:347
Varinfo(const _Varinfo *impl)
Create a smart pointer to the given variable information.
Definition: varinfo.h:345
Varinfo(const MutableVarinfo &vi)
Create a smart pointer to the given variable information.
Definition: varinfo.h:348
MutableVarinfo(_Varinfo *impl)
Create a smart pointer to the given variable information.
Definition: varinfo.h:300
const _Varinfo * m_impl
Varinfo structure to which the pointer refers.
Definition: varinfo.h:340
Varinfo(const _Varinfo &impl)
Create a smart pointer to the given variable information.
Definition: varinfo.h:346
#define VARINFO_FLAG_STRING
Varinfo flags.
Definition: varinfo.h:136
struct _Varinfo * alterations
Other altered versions of this Varinfo.
Definition: varinfo.h:184
bool do_unref() const
Decrement the reference count to this Data object, and return true if the reference count went down t...
Definition: varinfo.h:202
int imin
Minimum unscaled value the field can have.
Definition: varinfo.h:174