Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / ASNMP / asnmp / vb.cpp
blob8ceb3e9f729825b2ac90e141e44a6e85cb5af254
2 //=============================================================================
3 /**
4 * @file vb.cpp
6 * The Vb class is an encapsulation of the snmp variable binding.
7 * This module contains the class definition for the variable binding (VB)
8 * class. The VB class is an encapsulation of a SNMP VB. A VB object is
9 * composed of one SNMP++ Oid and one SMI value. The Vb class utilizes Oid
10 * objects and thus requires the Oid class. To use this class,
11 * set oid, value then call valid() to be sure object was constructed correctly.
13 * @author Peter E MellquistMichael R MacFaden mrm@cisco.com - rework & ACE port
15 //=============================================================================
17 /*===================================================================
18 Copyright (c) 1996
19 Hewlett-Packard Company
21 ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.
22 Permission to use, copy, modify, distribute and/or sell this software
23 and/or its documentation is hereby granted without fee. User agrees
24 to display the above copyright notice and this license notice in all
25 copies of the software and any documentation of the software. User
26 agrees to assume all liability for the use of the software; Hewlett-Packard
27 makes no representations about the suitability of this software for any
28 purpose. It is provided "AS-IS without warranty of any kind,either express
29 or implied. User hereby grants a royalty-free license to any and all
30 derivatives based upon this software code base.
31 =====================================================================*/
33 #include "asnmp/oid.h" // include oid class defs
34 #include "asnmp/vb.h" // include vb class defs
35 #include "asnmp/snmperrs.h" // error codes
36 #include "ace/OS_NS_stdio.h"
37 #include "ace/OS_NS_string.h"
38 #include "ace/OS_Memory.h"
40 //---------------[ Vb::Vb() ]--------------------------------------
41 // constructor with no arguments
42 // makes an vb, unitialized
43 Vb::Vb(): output_(0), iv_vb_value_(0),
44 exception_status_(SNMP_CLASS_SUCCESS)
48 //---------------[ Vb::Vb( const Oid &oid) ]-----------------------------
49 // constructor to initialize the oid
50 // makes a vb with oid portion initialized
51 Vb::Vb( const Oid &oid): output_(0), iv_vb_oid_(oid), iv_vb_value_(0),
52 exception_status_(SNMP_CLASS_SUCCESS)
56 //---------------[ Vb::Vb( const Oid &oid, const SmiSyntax &val) ]-------
57 Vb::Vb( const Oid &oid, const SnmpSyntax &val, const SmiUINT32 status):
58 output_(0), iv_vb_oid_(oid), iv_vb_value_(0), exception_status_(status)
60 // iv_vb_value_ = recast_smi_object(val); // allocate and construct object
61 iv_vb_value_ = val.clone();
64 //---------------[ Vb::Vb( const Vb &vb) ]-----------------------------
65 // copy constructor
66 Vb::Vb( const Vb &vb): output_(0), iv_vb_value_(0)
68 *this = vb;
71 //---------------[ Vb::~Vb() ]------------------------------------------
72 // destructor
73 // if the vb has a oid or an octect string then
74 // the associated memory needs to be freed
75 Vb::~Vb()
77 free_vb();
78 delete [] output_; // formatting buffer if it exists
82 //--------------[ Vb::valid() ]-----------------------------------------
83 // returns validity of a Vb object
84 // must have a valid oid and value
85 int Vb::valid() const
87 if ( iv_vb_oid_.valid() && (iv_vb_value_ && iv_vb_value_->valid()) )
88 return 1;
89 else
90 return 0;
94 //---------------[ Vb& Vb::operator=( const Vb &vb) ]--------------------
95 // overloaded assignment allows assigning one Vb to another
96 // this involves deep memory thus target vb needs to be freed
97 // before assigning source
98 Vb& Vb::operator=( const Vb &vb)
100 free_vb(); // free up target to begin with
102 //-----[ reassign the Oid portion 1st ]
103 vb.get_oid( iv_vb_oid_);
105 //-----[ next set the vb value portion ]
106 if (vb.iv_vb_value_ == 0) {
107 iv_vb_value_ = 0;
109 else {
110 iv_vb_value_ = vb.iv_vb_value_->clone();
112 exception_status_ = vb.exception_status_;
114 return *this; // return self reference
117 // set a Vb null, if its not already
118 void Vb::set_null()
120 free_vb();
123 //---------------[ Vb::set_oid( const Oid oid ) ]-----------------------
124 // set value oid only with another oid
125 void Vb::set_oid( const Oid& oid)
127 iv_vb_oid_ = oid;
130 //---------------[ Vb::get_oid( Oid &oid) ]-----------------------------
131 // get oid portion
132 void Vb::get_oid( Oid &oid) const
134 oid = iv_vb_oid_;
137 //----------------[ void Vb::free_vb() ]--------------------------------
138 // protected method to free memory
139 // this methos is used to free memory when assigning new vbs
140 // or destructing
141 // in the case of oids and octets, we need to do a deep free
142 void Vb::free_vb()
144 if (iv_vb_value_)
145 delete iv_vb_value_;
146 exception_status_ = SNMP_CLASS_SUCCESS;
147 iv_vb_value_ = 0;
150 void Vb::set_value( const SnmpInt32& i)
152 free_vb();
153 iv_vb_value_ = (SnmpSyntax *)new SnmpInt32(i);
156 void Vb::set_value( const SnmpUInt32& u)
158 free_vb();
159 iv_vb_value_ = (SnmpSyntax *)new SnmpUInt32(u);
162 void Vb::set_value( const Gauge32& g)
164 free_vb();
165 iv_vb_value_ = (SnmpSyntax *)new Gauge32(g);
168 void Vb::set_value( const Counter32& c)
170 free_vb();
171 iv_vb_value_ = (SnmpSyntax *)new Counter32(c);
174 void Vb::set_value( const Counter64& c)
176 free_vb();
177 iv_vb_value_ = (SnmpSyntax *)new Counter64(c);
180 void Vb::set_value( const TimeTicks& t)
182 free_vb();
183 iv_vb_value_ = (SnmpSyntax *)new TimeTicks(t);
186 void Vb::set_value( const OctetStr& s)
188 free_vb();
189 iv_vb_value_ = (SnmpSyntax *)new OctetStr(s);
192 void Vb::set_value( const Oid& o)
194 free_vb();
195 iv_vb_value_ = (SnmpSyntax *)new Oid(o);
198 void Vb::set_value ( const SnmpSyntax &val)
200 free_vb();
201 iv_vb_value_ = val.clone();
204 int Vb::get_value( SnmpInt32 &i)
206 if (iv_vb_value_ &&
207 iv_vb_value_->valid() &&
208 (iv_vb_value_->get_syntax() == sNMP_SYNTAX_INT32 )) {
209 i = *((SnmpInt32 *) iv_vb_value_);
210 return SNMP_CLASS_SUCCESS;
212 else
213 return SNMP_CLASS_INVALID;
216 int Vb::get_value( SnmpUInt32 &u)
218 if (iv_vb_value_ && iv_vb_value_->valid())
220 SmiUINT32 syntax = iv_vb_value_->get_syntax();
221 if (syntax == sNMP_SYNTAX_GAUGE32 ||
222 syntax == sNMP_SYNTAX_CNTR32 ||
223 syntax == sNMP_SYNTAX_TIMETICKS ||
224 syntax == sNMP_SYNTAX_UINT32)
226 u = *((SnmpUInt32 *) iv_vb_value_);
227 return SNMP_CLASS_SUCCESS;
230 return SNMP_CLASS_INVALID;
233 /* return a uint or a gauge. this is casting, but no semantic difference
234 * at this level
236 int Vb::get_value( Gauge32 &g)
238 if (iv_vb_value_ &&
239 iv_vb_value_->valid() &&
240 ((iv_vb_value_->get_syntax() == sNMP_SYNTAX_GAUGE32) ||
241 iv_vb_value_->get_syntax() == sNMP_SYNTAX_UINT32) ) {
242 g = *((Gauge32 *) iv_vb_value_);
243 return SNMP_CLASS_SUCCESS;
245 else
246 return SNMP_CLASS_INVALID;
249 int Vb::get_value( Counter32 &c)
251 if (iv_vb_value_ &&
252 iv_vb_value_->valid() &&
253 (iv_vb_value_->get_syntax() == sNMP_SYNTAX_CNTR32 )) {
254 c = *((Counter32 *) iv_vb_value_);
255 return SNMP_CLASS_SUCCESS;
257 else
258 return SNMP_CLASS_INVALID;
261 int Vb::get_value( Counter64 &c)
263 if (iv_vb_value_ &&
264 iv_vb_value_->valid() &&
265 (iv_vb_value_->get_syntax() == sNMP_SYNTAX_CNTR64 )) {
266 c = *((Counter32 *) iv_vb_value_);
267 return SNMP_CLASS_SUCCESS;
269 else
270 return SNMP_CLASS_INVALID;
273 int Vb::get_value( TimeTicks &t)
275 if (iv_vb_value_ &&
276 iv_vb_value_->valid() &&
277 (iv_vb_value_->get_syntax() == sNMP_SYNTAX_TIMETICKS )) {
278 t = *((TimeTicks *) iv_vb_value_);
279 return SNMP_CLASS_SUCCESS;
281 else
282 return SNMP_CLASS_INVALID;
285 int Vb::get_value( OctetStr &s)
287 if (iv_vb_value_ &&
288 iv_vb_value_->valid() &&
289 (iv_vb_value_->get_syntax() == sNMP_SYNTAX_OCTETS )) {
290 s = *((OctetStr *) iv_vb_value_);
291 return SNMP_CLASS_SUCCESS;
293 else
294 return SNMP_CLASS_INVALID;
297 int Vb::get_value( Oid &s)
299 if (iv_vb_value_ &&
300 iv_vb_value_->valid() &&
301 (iv_vb_value_->get_syntax() == sNMP_SYNTAX_OID )) {
302 s = *((Oid *) iv_vb_value_);
303 return SNMP_CLASS_SUCCESS;
305 else
306 return SNMP_CLASS_INVALID;
310 //---------------[ Vb::get_value( Value &val) ]--------
311 int Vb::get_value( SnmpSyntax &val)
313 if (iv_vb_value_) {
314 val = *iv_vb_value_;
315 if (val.valid())
316 return SNMP_CLASS_SUCCESS;
317 else
318 return SNMP_CLASS_INVALID;
320 else
322 //TM: should set val to be invalid
323 return SNMP_CLASS_INVALID;
328 //-----[ misc]--------------------------------------------------------
330 // return the current syntax
331 // This method violates Object Orientation but may be useful if
332 // the caller has a vb object and does not know what it is.
333 // This would be useful in the implementation of a browser.
334 SmiUINT32 Vb::get_syntax()
336 if ( exception_status_ != SNMP_CLASS_SUCCESS)
337 return exception_status_;
338 else
339 return ( iv_vb_value_ ? iv_vb_value_->get_syntax() : sNMP_SYNTAX_NULL);
342 // return the printabel value
343 const char *Vb::to_string_value()
345 if (iv_vb_value_)
346 return iv_vb_value_->to_string();
347 else
348 return "";
351 // return the printable oid
352 const char *Vb::to_string_oid()
354 return iv_vb_oid_.to_string();
357 // generate string with name/ value format
358 const char *Vb::to_string()
360 int len = ACE_OS::strlen(iv_vb_oid_.to_string());
361 const char *ptr = iv_vb_value_ ? iv_vb_value_->to_string() : "";
362 len += ACE_OS::strlen(ptr) + 3 + 1; // " / " + null
363 ACE_NEW_RETURN(output_, char[len], "");
364 ACE_OS::sprintf(output_, "%s / %s", iv_vb_oid_.to_string(), ptr);
365 return output_;
368 // friend function to set exception status
369 void set_exception_status( Vb *vb, const SmiUINT32 status)
371 vb->exception_status_ = status;
374 // equivlence operator overloaded
375 // hack, by side effect, compare based on string formatting output_
376 bool operator==( const Vb &lhs, const Vb &rhs)
378 if ( lhs.iv_vb_oid_ != rhs.iv_vb_oid_)
379 return false;
381 if (lhs.iv_vb_value_ != 0 && rhs.iv_vb_value_ != 0)
383 const int val =
384 ACE_OS::strcmp (lhs.iv_vb_value_->to_string(),
385 rhs.iv_vb_value_->to_string());
386 return !val;
388 else
389 return false;