SNMP++  3.3.11
integer.h
Go to the documentation of this file.
1 /*_############################################################################
2  _##
3  _## integer.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++ I N T E G E R. H
45 
46  INTEGER CLASS DEFINITION
47 
48  DESIGN + AUTHOR: Jeff Meyer
49 
50  DESCRIPTION:
51  Class definition for Integer classes.
52 
53 =====================================================================*/
54 // $Id$
55 
56 #ifndef _SNMP_INTEGER_H_
57 #define _SNMP_INTEGER_H_
58 
59 #include <libsnmp.h>
60 #include "snmp_pp/smival.h"
61 
62 #ifdef SNMP_PP_NAMESPACE
63 namespace Snmp_pp {
64 #endif
65 
66 #define INTOUTBUF 15 // largest ASCII formatted integer
67 
68 //------------[ Integer Classes ]------------------------------------------
69 
70 /**
71  * 32 bit unsigned integer class.
72  *
73  * The integer class allows all the functionality of the various
74  * integers but is contained in a Value object for consistency
75  * among the various types.
76  * class objects may be set or get into Vb objects.
77  */
79 {
80  public:
81 
82  /**
83  * Constructor with value (defaults to 0).
84  *
85  * @param i - initial value
86  */
87  SnmpUInt32(const unsigned long i = 0)
88  : SnmpSyntax(), valid_flag(true), m_changed(true)
89  {
90  smival.value.uNumber = i;
91  smival.syntax = sNMP_SYNTAX_UINT32;
92  }
93 
94  /**
95  * Copy constructor.
96  *
97  * @param c - initial value
98  */
100  : SnmpSyntax(), valid_flag(c.valid_flag), m_changed(true)
101  {
102  smival.value.uNumber = c.smival.value.uNumber;
103  smival.syntax = sNMP_SYNTAX_UINT32;
104  }
105 
106  /**
107  * Destructor (ensure that SnmpSyntax::~SnmpSyntax() is overridden).
108  */
109  virtual ~SnmpUInt32() {}
110 
111  /**
112  * Return the syntax.
113  *
114  * @return This method always returns sNMP_SYNTAX_UINT32.
115  */
116  virtual SmiUINT32 get_syntax() const { return sNMP_SYNTAX_UINT32; }
117 
118  /**
119  * Overloaded assignment for unsigned longs.
120  *
121  * @param i - new value
122  * @return self reference
123  */
124  SnmpUInt32 & operator = (const unsigned long i)
125  {
126  smival.value.uNumber = i;
127  valid_flag = true;
128  m_changed = true;
129  return *this;
130  }
131 
132  /**
133  * Overloaded assignment for SnmpUInt32.
134  *
135  * @param uli - new value
136  * @return self reference
137  */
138  SnmpUInt32 & operator = (const SnmpUInt32 &uli)
139  {
140  if (this == &uli)
141  return *this; // check for self assignment
142 
143  smival.value.uNumber = uli.smival.value.uNumber;
144  valid_flag = uli.valid_flag;
145  m_changed = true;
146  return *this;
147  }
148 
149  /**
150  * Map other SnmpSyntax objects to SnmpUInt32.
151  */
152  SnmpSyntax& operator=(const SnmpSyntax &val);
153 
154  /**
155  * Behave like an unsigned long.
156  *
157  * @return value as unsigned long
158  */
159  operator unsigned long() const { return smival.value.uNumber; };
160 
161  /**
162  * Get a printable ASCII value.
163  */
164  virtual const char *get_printable() const;
165 
166  /**
167  * Clone operator.
168  *
169  * @return Pointer to a newly created copy of the object.
170  */
171  virtual SnmpSyntax *clone() const
172  { return (SnmpSyntax *)new SnmpUInt32(*this); };
173 
174  /**
175  * Return validity of the object.
176  * An SnmpUInt32 will only be invalid after a failed asignment
177  * of another SnmpSyntax object.
178  */
179  bool valid() const { return valid_flag; }
180 
181  /**
182  * Return the space needed for serialization.
183  */
184  int get_asn1_length() const;
185 
186  /**
187  * Reset the object.
188  */
189  void clear()
190  { smival.value.uNumber = 0; valid_flag = true; m_changed = true; }
191 
192  protected:
194  SNMP_PP_MUTABLE char output_buffer[INTOUTBUF];
196 };
197 
198 
199 /**
200  * 32 bit signed integer class.
201  */
203 {
204  public:
205 
206  /**
207  * Constructor with value.
208  *
209  * @param i - initial value
210  */
211  SnmpInt32 (const long i = 0)
212  : SnmpSyntax(), valid_flag(true), m_changed(true)
213  {
214  smival.value.sNumber = i;
215  smival.syntax = sNMP_SYNTAX_INT32;
216  }
217 
218  /**
219  * Copy constructor.
220  *
221  * @param c - initial value
222  */
223  SnmpInt32 (const SnmpInt32 &c)
224  : SnmpSyntax(), valid_flag(c.valid_flag), m_changed(true)
225  {
226  smival.value.sNumber = c.smival.value.sNumber;
227  smival.syntax = sNMP_SYNTAX_INT32;
228  }
229 
230  /**
231  * Destructor (ensure that SnmpSyntax::~SnmpSyntax() is overridden).
232  */
233  virtual ~SnmpInt32() {}
234 
235  /**
236  * Return the syntax.
237  *
238  * @return This method always returns sNMP_SYNTAX_INT32.
239  */
240  virtual SmiUINT32 get_syntax() const { return sNMP_SYNTAX_INT32; }
241 
242  /**
243  * Overloaded assignment for longs.
244  *
245  * @param i - new value
246  * @return self reference
247  */
248  SnmpInt32 & operator = (const long i)
249  {
250  smival.value.sNumber = i;
251  valid_flag = true;
252  m_changed = true;
253  return *this;
254  }
255 
256  /**
257  * Overloaded assignment for SnmpInt32.
258  *
259  * @param li - new value
260  * @return self reference
261  */
262  SnmpInt32 & operator = (const SnmpInt32 &li)
263  {
264  if (this == &li)
265  return *this; // check for self assignment
266 
267  smival.value.sNumber = li.smival.value.sNumber;
268  valid_flag = li.valid_flag;
269  m_changed = true;
270  return *this;
271  }
272 
273  /**
274  * Map other SnmpSyntax objects to SnmpInt32.
275  */
276  SnmpSyntax& operator=(const SnmpSyntax &val);
277 
278  /**
279  * Behave like an long.
280  *
281  * @return value as long
282  */
283  operator long () const { return (long) smival.value.sNumber; }
284 
285  /**
286  * Get a printable ASCII value.
287  */
288  const char *get_printable() const;
289 
290  /**
291  * Clone operator.
292  *
293  * @return Pointer to a newly created copy of the object.
294  */
295  SnmpSyntax *clone() const { return (SnmpSyntax *)new SnmpInt32(*this); };
296 
297  /**
298  * Return validity of the object.
299  * An SnmpUInt32 will only be invalid after a failed asignment
300  * of another SnmpSyntax object.
301  */
302  bool valid() const { return valid_flag; }
303 
304  /**
305  * Return the space needed for serialization.
306  */
307  int get_asn1_length() const;
308 
309  /**
310  * Reset the object.
311  */
312  void clear()
313  { smival.value.sNumber = 0; valid_flag = true; m_changed = true; }
314 
315  protected:
317  SNMP_PP_MUTABLE char output_buffer[INTOUTBUF];
319 };
320 
321 #ifdef SNMP_PP_NAMESPACE
322 } // end of namespace Snmp_pp
323 #endif
324 
325 #endif // _SNMP_INTEGER_H_
#define sNMP_SYNTAX_UINT32
Definition: smi.h:112
unsigned long SmiUINT32
Definition: smi.h:157
SmiVALUE smival
Definition: smival.h:175
#define SNMP_PP_MUTABLE
virtual SmiUINT32 get_syntax() const
Return the syntax.
Definition: integer.h:116
SmiUINT32 uNumber
Definition: smival.h:90
SNMP_PP_MUTABLE bool m_changed
Definition: integer.h:195
SnmpSyntax * clone() const
Clone operator.
Definition: integer.h:295
void clear()
Reset the object.
Definition: integer.h:312
virtual ~SnmpInt32()
Destructor (ensure that SnmpSyntax::~SnmpSyntax() is overridden).
Definition: integer.h:233
32 bit signed integer class.
Definition: integer.h:202
bool valid_flag
Definition: integer.h:193
#define DLLOPT
SnmpUInt32(const SnmpUInt32 &c)
Copy constructor.
Definition: integer.h:99
bool valid() const
Return validity of the object.
Definition: integer.h:302
virtual ~SnmpUInt32()
Destructor (ensure that SnmpSyntax::~SnmpSyntax() is overridden).
Definition: integer.h:109
SnmpInt32(const long i=0)
Constructor with value.
Definition: integer.h:211
SnmpInt32(const SnmpInt32 &c)
Copy constructor.
Definition: integer.h:223
32 bit unsigned integer class.
Definition: integer.h:78
union SmiVALUE::@1 value
#define sNMP_SYNTAX_INT32
Definition: smi.h:105
SNMP_PP_MUTABLE bool m_changed
Definition: integer.h:318
void clear()
Reset the object.
Definition: integer.h:189
SmiINT sNumber
Definition: smival.h:88
SnmpUInt32(const unsigned long i=0)
Constructor with value (defaults to 0).
Definition: integer.h:87
An "abstract" (pure virtual) class that serves as the base class for all specific SNMP syntax types...
Definition: smival.h:117
#define INTOUTBUF
Definition: integer.h:66
bool valid_flag
Definition: integer.h:316
virtual SnmpSyntax * clone() const
Clone operator.
Definition: integer.h:171
bool valid() const
Return validity of the object.
Definition: integer.h:179
virtual SmiUINT32 get_syntax() const
Return the syntax.
Definition: integer.h:240