SNMP++  3.3.11
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$
54 
55 #ifndef _SNMP_CTR64_H_
56 #define _SNMP_CTR64_H_
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 
82  /**
83  * Constructs a valid Counter64 with the given values.
84  *
85  * @param hi - value for the high 32 bits (0..MAX_UINT32)
86  * @param lo - value for the low 32 bits (0..MAX_UINT32)
87  */
88  Counter64(unsigned long hi, unsigned long lo)
89  : m_changed(true)
90  {
91  smival.syntax = sNMP_SYNTAX_CNTR64;
92  smival.value.hNumber.hipart = hi;
93  smival.value.hNumber.lopart = lo;
94  }
95 
96  /**
97  * Constructs a valid Counter64 with the given value (default 0).
98  *
99  * @param val - value (full 64-bit)
100  */
102  : m_changed(true)
103  {
104  smival.syntax = sNMP_SYNTAX_CNTR64;
105  smival.value.hNumber.hipart = val >> 32;
106  smival.value.hNumber.lopart = val & UINT32_MAX;
107  }
108 
109  /**
110  * Copy constructor.
111  *
112  * @param ctr64 - value
113  */
114  Counter64(const Counter64 &ctr64)
115  : m_changed(true)
116  {
117  smival.syntax = sNMP_SYNTAX_CNTR64;
118  smival.value.hNumber = ctr64.smival.value.hNumber;
119  }
120 
121  /**
122  * Destructor (ensure that SnmpSyntax::~SnmpSyntax() is overridden).
123  */
125 
126  operator pp_uint64 () const
127  {
128  pp_uint64 v = ((pp_uint64)smival.value.hNumber.hipart) << 32;
129  v += smival.value.hNumber.lopart;
130  return v;
131  }
132 
133  //-----------[ get/set using 32 bit variables ]----------------------
134 
135  /**
136  * Get the high 32 bit part.
137  *
138  * @return The high part of the Counter64
139  */
140  unsigned long high() const { return smival.value.hNumber.hipart; }
141 
142  /**
143  * Get the low 32 bit part.
144  *
145  * @return The low part of the Counter64
146  */
147  unsigned long low() const { return smival.value.hNumber.lopart; }
148 
149  /**
150  * Set the high 32 bit part. The low part will stay unchanged.
151  *
152  * @param h - The new high part of the Counter64
153  */
154  void set_high(const unsigned long h)
155  { smival.value.hNumber.hipart = h; m_changed = true; }
156 
157  /**
158  * Set the low 32 bit part. The high part will stay unchanged.
159  *
160  * @param l - The new low part of the Counter64
161  */
162  void set_low(const unsigned long l)
163  { smival.value.hNumber.lopart = l; m_changed = true; }
164 
165 
166  //-----------[ SnmpSyntax methods ]----------------------
167 
168  /**
169  * Get a printable ASCII string representing the current value.
170  *
171  * @note The returned string is valid as long as the object is not
172  * modified.
173  *
174  * @return Null terminated string.
175  */
176  const char *get_printable() const;
177 
178  /**
179  * Get the Syntax of the object.
180  *
181  * @return This method always returns sNMP_SYNTAX_CNTR64.
182  */
184 
185  /**
186  * Clone the object.
187  *
188  * @return A cloned Counter64 object allocated through new.
189  */
190  SnmpSyntax *clone() const { return (SnmpSyntax *) new Counter64(*this); }
191 
192  /**
193  * Overloaded assignement operator.
194  *
195  * @param val - Try to map the given value to a Counter64 and assign it
196  * @return Always *this with the new value.
197  */
198  SnmpSyntax& operator=(const SnmpSyntax &val);
199 
200  /**
201  * Return validity of the object.
202  *
203  * @return Always true
204  */
205  bool valid() const { return true; }
206 
207  /**
208  * Return the space needed for serialization.
209  *
210  * @return The needed space that depends on the current value.
211  */
212  int get_asn1_length() const;
213 
214  /**
215  * Reset the object.
216  */
217  void clear()
218  { smival.value.hNumber.hipart = 0; smival.value.hNumber.lopart = 0;
219  m_changed = true; };
220 
221  //-----------[ overloaded operators ]----------------------
222 
223  /**
224  * Assign a Counter64 to a Counter64.
225  */
227  {
228  if (this == &ctr64)
229  return *this; // check for self assignment
230 
231  smival.value.hNumber.hipart = ctr64.high();
232  smival.value.hNumber.lopart = ctr64.low();
233  m_changed = true;
234  return *this;
235  }
236 
237  /**
238  * Assign a unsigned long to a Counter64.
239  *
240  * @param i - The new low part. The high part is cleared.
241  */
242  Counter64& operator = (const pp_uint64 i)
243  {
244  m_changed = true;
245  smival.value.hNumber.hipart = i >> 32;
246  smival.value.hNumber.lopart = i & UINT32_MAX;
247  return *this;
248  }
249 
250  protected:
251 
252  SNMP_PP_MUTABLE char output_buffer[CTR64OUTBUF];
254 };
255 
256 #ifdef SNMP_PP_NAMESPACE
257 } // end of namespace Snmp_pp
258 #endif
259 
260 #endif // _SNMP_CTR64_H_
unsigned long SmiUINT32
Definition: smi.h:157
SmiVALUE smival
Definition: smival.h:175
Counter64 & operator=(const Counter64 &ctr64)
Assign a Counter64 to a Counter64.
Definition: ctr64.h:226
#define SNMP_PP_MUTABLE
void set_high(const unsigned long h)
Set the high 32 bit part.
Definition: ctr64.h:154
Counter64(unsigned long hi, unsigned long lo)
Constructs a valid Counter64 with the given values.
Definition: ctr64.h:88
SmiCNTR64 hNumber
Definition: smival.h:94
#define sNMP_SYNTAX_CNTR64
Definition: smi.h:111
#define DLLOPT
~Counter64()
Destructor (ensure that SnmpSyntax::~SnmpSyntax() is overridden).
Definition: ctr64.h:124
SnmpSyntax * clone() const
Clone the object.
Definition: ctr64.h:190
void set_low(const unsigned long l)
Set the low 32 bit part.
Definition: ctr64.h:162
SmiUINT32 get_syntax() const
Get the Syntax of the object.
Definition: ctr64.h:183
#define UINT32_MAX
Definition: ctr64.h:61
Counter64(pp_uint64 val=0)
Constructs a valid Counter64 with the given value (default 0).
Definition: ctr64.h:101
bool valid() const
Return validity of the object.
Definition: ctr64.h:205
union SmiVALUE::@1 value
unsigned long long pp_uint64
#define CTR64OUTBUF
maximum ascii string for a 64-bit counter
Definition: ctr64.h:68
void clear()
Reset the object.
Definition: ctr64.h:217
Counter64 Class encapsulates two unsigned integers into a a single entity.
Definition: ctr64.h:76
unsigned long high() const
Get the high 32 bit part.
Definition: ctr64.h:140
unsigned long low() const
Get the low 32 bit part.
Definition: ctr64.h:147
SNMP_PP_MUTABLE bool m_changed
Definition: ctr64.h:253
An "abstract" (pure virtual) class that serves as the base class for all specific SNMP syntax types...
Definition: smival.h:117
Counter64(const Counter64 &ctr64)
Copy constructor.
Definition: ctr64.h:114