libwreport  2.8
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 {
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;
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(); }
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: */