SNMP++  3.3.4
ctr64.h
Go to the documentation of this file.
1 /*_############################################################################
2  _##
3  _## ctr64.h
4  _##
5  _## SNMP++ v3.3
6  _## -----------------------------------------------
7  _## Copyright (c) 2001-2013 Jochen Katz, Frank Fock
8  _##
9  _## This software is based on SNMP++2.6 from Hewlett Packard:
10  _##
11  _## Copyright (c) 1996
12  _## Hewlett-Packard Company
13  _##
14  _## ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.
15  _## Permission to use, copy, modify, distribute and/or sell this software
16  _## and/or its documentation is hereby granted without fee. User agrees
17  _## to display the above copyright notice and this license notice in all
18  _## copies of the software and any documentation of the software. User
19  _## agrees to assume all liability for the use of the software;
20  _## Hewlett-Packard and Jochen Katz make no representations about the
21  _## suitability of this software for any purpose. It is provided
22  _## "AS-IS" without warranty of any kind, either express or implied. User
23  _## hereby grants a royalty-free license to any and all derivatives based
24  _## upon this software code base.
25  _##
26  _##########################################################################*/
27 /*===================================================================
28 
29  Copyright (c) 1999
30  Hewlett-Packard Company
31 
32  ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.
33  Permission to use, copy, modify, distribute and/or sell this software
34  and/or its documentation is hereby granted without fee. User agrees
35  to display the above copyright notice and this license notice in all
36  copies of the software and any documentation of the software. User
37  agrees to assume all liability for the use of the software; Hewlett-Packard
38  makes no representations about the suitability of this software for any
39  purpose. It is provided "AS-IS without warranty of any kind,either express
40  or implied. User hereby grants a royalty-free license to any and all
41  derivatives based upon this software code base.
42 
43 
44  SNMP++ C O U N T E R 6 4 . H
45 
46  COUNTER64 CLASSES DEFINITION
47 
48  DESIGN + AUTHOR: Peter E Mellquist
49 
50  DESCRIPTION: SNMP Counter64 class definition.
51 
52 =====================================================================*/
53 // $Id: ctr64.h 2359 2013-05-09 20:07:01Z fock $
54 
55 #ifndef _CTR64
56 #define _CTR64
57 
58 #include "snmp_pp/smival.h"
59 
60 #ifndef UINT32_MAX
61 # define UINT32_MAX (4294967295U)
62 #endif
63 
64 #ifdef SNMP_PP_NAMESPACE
65 namespace Snmp_pp {
66 #endif
67 
68 #define CTR64OUTBUF 30 //!< maximum ascii string for a 64-bit counter
69 
70 //---------[ 64 bit Counter Class ]--------------------------------
71 /**
72  * Counter64 Class encapsulates two unsigned integers into a
73  * a single entity. This type has is available in SNMPv2 but
74  * may be used anywhere where needed.
75  */
77 {
78  public:
79 
80  //-----------[ Constructors and Destrucotr ]----------------------
81 #if 0
82  /**
83  * Constructs a valid Couter64 with value 0.
84  */
85  Counter64();
86 
87  /**
88  * Constructs a valid Counter64 with the given value as the lower 32 bits.
89  *
90  * @param lo - value (0..MAX_UINT32)
91  */
92  Counter64(unsigned long lo);
93 #endif
94  /**
95  * Constructs a valid Counter64 with the given values.
96  *
97  * @param hi - value for the high 32 bits (0..MAX_UINT32)
98  * @param lo - value for the low 32 bits (0..MAX_UINT32)
99  */
100  Counter64(unsigned long hi, unsigned long lo)
101  : m_changed(true)
102  {
103  smival.syntax = sNMP_SYNTAX_CNTR64;
104  smival.value.hNumber.hipart = hi;
105  smival.value.hNumber.lopart = lo;
106  }
107 
108  /**
109  * Constructs a valid Counter64 with the given value (default 0).
110  *
111  * @param val - value (full 64-bit)
112  */
114  : m_changed(true)
115  {
116  smival.syntax = sNMP_SYNTAX_CNTR64;
117  smival.value.hNumber.hipart = val >> 32;
118  smival.value.hNumber.lopart = val & UINT32_MAX;
119  }
120 
121  /**
122  * Copy constructor.
123  *
124  * @param ctr64 - value
125  */
126  Counter64(const Counter64 &ctr64)
127  : m_changed(true)
128  {
129  smival.syntax = sNMP_SYNTAX_CNTR64;
130  smival.value.hNumber = ctr64.smival.value.hNumber;
131  }
132 
133  /**
134  * Destructor (ensure that SnmpSyntax::~SnmpSyntax() is overridden).
135  */
137 
138 #if 0
139  //-----------[ conversion from/to unsigned long long ]----------------
140 
141  /**
142  * Get the value of the object as 64 bit integer.
143  *
144  * @param c64 - The Counter64 object whose value should be returned
145  * @return value as a unsigned 64 bit integer
146  */
147  static pp_uint64 c64_to_ll(const Counter64 &c64);
148 
149  /**
150  * Get the value of this object as 64 bit integer.
151  *
152  * @return value as a unsigned 64 bit integer
153  */
154  pp_uint64 c64_to_ll() const;
155 
156  /**
157  * Convert a 64 bit integer to a Counter64.
158  *
159  * @param ld - the value to convert
160  * @return A Counter64 object with the value of the param ld.
161  */
162  static Counter64 ll_to_c64(const pp_uint64 &ll);
163 #endif
164 
165  operator pp_uint64 () const
166  {
167  pp_uint64 v = ((pp_uint64)smival.value.hNumber.hipart) << 32;
168  v += smival.value.hNumber.lopart;
169  return v;
170  }
171 
172  //-----------[ get/set using 32 bit variables ]----------------------
173 
174  /**
175  * Get the high 32 bit part.
176  *
177  * @return The high part of the Counter64
178  */
179  unsigned long high() const { return smival.value.hNumber.hipart; }
180 
181  /**
182  * Get the low 32 bit part.
183  *
184  * @return The low part of the Counter64
185  */
186  unsigned long low() const { return smival.value.hNumber.lopart; }
187 
188  /**
189  * Set the high 32 bit part. The low part will stay unchanged.
190  *
191  * @param h - The new high part of the Counter64
192  */
193  void set_high(const unsigned long h)
194  { smival.value.hNumber.hipart = h; m_changed = true; }
195 
196  /**
197  * Set the low 32 bit part. The high part will stay unchanged.
198  *
199  * @param l - The new low part of the Counter64
200  */
201  void set_low(const unsigned long l)
202  { smival.value.hNumber.lopart = l; m_changed = true; }
203 
204 
205  //-----------[ SnmpSyntax methods ]----------------------
206 
207  /**
208  * Get a printable ASCII string representing the current value.
209  *
210  * @note The returned string is valid as long as the object is not
211  * modified.
212  *
213  * @return Null terminated string.
214  */
215  const char *get_printable() const;
216 
217  /**
218  * Get the Syntax of the object.
219  *
220  * @return This method always returns sNMP_SYNTAX_CNTR64.
221  */
223 
224  /**
225  * Clone the object.
226  *
227  * @return A cloned Counter64 object allocated through new.
228  */
229  SnmpSyntax *clone() const { return (SnmpSyntax *) new Counter64(*this); }
230 
231  /**
232  * Overloaded assignement operator.
233  *
234  * @param val - Try to map the given value to a Counter64 and assign it
235  * @return Always *this with the new value.
236  */
237  SnmpSyntax& operator=(const SnmpSyntax &val);
238 
239  /**
240  * Return validity of the object.
241  *
242  * @return Always true
243  */
244  bool valid() const { return true; }
245 
246  /**
247  * Return the space needed for serialization.
248  *
249  * @return The needed space that depends on the current value.
250  */
251  int get_asn1_length() const;
252 
253  /**
254  * Reset the object.
255  */
256  void clear()
257  { smival.value.hNumber.hipart = 0; smival.value.hNumber.lopart = 0;
258  m_changed = true; };
259 
260  //-----------[ overloaded operators ]----------------------
261 
262  /**
263  * Assign a Counter64 to a Counter64.
264  */
266  {
267  if (this == &ctr64)
268  return *this; // check for self assignment
269 
270  smival.value.hNumber.hipart = ctr64.high();
271  smival.value.hNumber.lopart = ctr64.low();
272  m_changed = true;
273  return *this;
274  }
275 
276  /**
277  * Assign a unsigned long to a Counter64.
278  *
279  * @param i - The new low part. The high part is cleared.
280  */
281  Counter64& operator = (const pp_uint64 i)
282  {
283  m_changed = true;
284  smival.value.hNumber.hipart = i >> 32;
285  smival.value.hNumber.lopart = i & UINT32_MAX;
286  return *this;
287  }
288 
289 #if 0
290  /**
291  * Add two Counter64.
292  */
293  Counter64 operator+(const Counter64 &c) const;
294 
295  /**
296  * Add a unsigned long and a Counter64.
297  */
298  DLLOPT friend Counter64 operator+(unsigned long ul, const Counter64 &c64)
299  { return Counter64(ul) + c64; };
300 
301  /**
302  * Subtract two Counter64.
303  */
304  Counter64 operator-(const Counter64 &c) const;
305 
306  /**
307  * Subtract a unsigned long and a Counter64.
308  */
309  DLLOPT friend Counter64 operator-(unsigned long ul, const Counter64 &c64)
310  { return Counter64(ul) - c64; };
311 
312  /**
313  * Multiply two Counter64.
314  */
315  Counter64 operator*(const Counter64 &c) const;
316 
317  /**
318  * Multiply a unsigned long and a Counter64.
319  */
320  DLLOPT friend Counter64 operator*(unsigned long ul, const Counter64 &c64)
321  { return Counter64(ul) * c64; };
322 
323  /**
324  * Divide two Counter64.
325  */
326  Counter64 operator/(const Counter64 &c) const;
327 
328  /**
329  * Divide a unsigned long and a Counter64.
330  */
331  DLLOPT friend Counter64 operator/(unsigned long ul, const Counter64 &c64)
332  { return Counter64(ul) / c64; };
333 
334  //-------[ overloaded comparison operators ]--------------
335 
336  /**
337  * Equal operator for two Cunter64.
338  */
339  DLLOPT friend bool operator==(const Counter64 &lhs, const Counter64 &rhs);
340 
341  /**
342  * Not equal operator for two Cunter64.
343  */
344  DLLOPT friend bool operator!=(const Counter64 &lhs, const Counter64 &rhs);
345 
346  /**
347  * Less than operator for two Cunter64.
348  */
349  DLLOPT friend bool operator<(const Counter64 &lhs, const Counter64 &rhs);
350 
351  /**
352  * Less than or equal operator for two Cunter64.
353  */
354  DLLOPT friend bool operator<=(const Counter64 &lhs, const Counter64 &rhs);
355 
356  /**
357  * Greater than operator for two Cunter64.
358  */
359  DLLOPT friend bool operator>(const Counter64 &lhs, const Counter64 &rhs);
360 
361  /**
362  * Greater than or equal operator for two Cunter64.
363  */
364  DLLOPT friend bool operator>=(const Counter64 &lhs, const Counter64 &rhs);
365 #endif
366 
367  protected:
368 
369  SNMP_PP_MUTABLE char output_buffer[CTR64OUTBUF];
371 };
372 
373 #ifdef SNMP_PP_NAMESPACE
374 } // end of namespace Snmp_pp
375 #endif
376 
377 #endif
unsigned long SmiUINT32
Definition: smi.h:157
SmiVALUE smival
Definition: smival.h:179
Counter64 & operator=(const Counter64 &ctr64)
Assign a Counter64 to a Counter64.
Definition: ctr64.h:265
#define SNMP_PP_MUTABLE
void set_high(const unsigned long h)
Set the high 32 bit part.
Definition: ctr64.h:193
Counter64(unsigned long hi, unsigned long lo)
Constructs a valid Counter64 with the given values.
Definition: ctr64.h:100
SmiCNTR64 hNumber
Definition: smival.h:93
#define sNMP_SYNTAX_CNTR64
Definition: smi.h:111
#define DLLOPT
~Counter64()
Destructor (ensure that SnmpSyntax::~SnmpSyntax() is overridden).
Definition: ctr64.h:136
SmiUINT32 get_syntax() const
Get the Syntax of the object.
Definition: ctr64.h:222
void set_low(const unsigned long l)
Set the low 32 bit part.
Definition: ctr64.h:201
#define UINT32_MAX
Definition: ctr64.h:61
bool valid() const
Return validity of the object.
Definition: ctr64.h:244
Counter64(pp_uint64 val=0)
Constructs a valid Counter64 with the given value (default 0).
Definition: ctr64.h:113
union SmiVALUE::@1 value
unsigned long long pp_uint64
unsigned long low() const
Get the low 32 bit part.
Definition: ctr64.h:186
#define CTR64OUTBUF
maximum ascii string for a 64-bit counter
Definition: ctr64.h:68
void clear()
Reset the object.
Definition: ctr64.h:256
SnmpSyntax * clone() const
Clone the object.
Definition: ctr64.h:229
Counter64 Class encapsulates two unsigned integers into a a single entity.
Definition: ctr64.h:76
SNMP_PP_MUTABLE bool m_changed
Definition: ctr64.h:370
An "abstract" (pure virtual) class that serves as the base class for all specific SNMP syntax types...
Definition: smival.h:116
Counter64(const Counter64 &ctr64)
Copy constructor.
Definition: ctr64.h:126
unsigned long high() const
Get the high 32 bit part.
Definition: ctr64.h:179