Dangerous SNMP
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
variant.hpp
1 #pragma once
2 
3 #include <ostream>
4 
5 #include "numericoid.hpp"
6 
7 
8 namespace dangerous { namespace snmp {
9 
78 class Variant {
79 public:
88  enum Type {
102  };
103 
104 public:
108  Variant() : _type( SIMPLE_NULL ) {}
109 
113  Variant( const Variant& variant ) {
114  _type = variant._type;
115 
116  if( variant.is_int32_t() ) {
117  v.i32 = variant.v.i32;
118  } else if( variant.is_uint32_t() ) {
119  v.u32 = variant.v.u32;
120  } else if( variant.is_uint64_t() ) {
121  v.u64 = variant.v.u64;
122  } else if( variant.is_string() ) {
123  new( &v.s ) std::string();
124  v.s = variant.v.s;
125  } else if( variant.is_oid() ) {
126  new( &v.oid ) NumericOid();
127  v.oid = variant.v.oid;
128  }
129  }
130 
134  Variant& operator=( const Variant& variant ) {
135  _type = variant._type;
136 
137  if( variant.is_int32_t() ) {
138  v.i32 = variant.v.i32;
139  } else if( variant.is_uint32_t() ) {
140  v.u32 = variant.v.u32;
141  } else if( variant.is_uint64_t() ) {
142  v.u64 = variant.v.u64;
143  } else if( variant.is_string() ) {
144  new( &v.s ) std::string();
145  v.s = variant.v.s;
146  } else if( variant.is_oid() ) {
147  new( &v.oid ) NumericOid();
148  v.oid = variant.v.oid;
149  }
150 
151  return *this;
152  }
153 
161  }
162 
167  Type type() const {
168  return _type;
169  }
170 
171 private:
172  /*
173  * Internal workings.
174  */
175 
179  union ValueUnion {
184  ValueUnion() {
185  }
186 
191  ~ValueUnion() {
192  }
193 
195  int32_t i32;
197  uint32_t u32;
199  uint64_t u64;
201  std::string s;
203  NumericOid oid;
204 
205  // TODO: MOAR! (like what?)
206  };
207 
209  Type _type;
211  ValueUnion v;
212 
213 public:
214  /*
215  * Type setting methods.
216  */
217 
229  // Make sure that the new type is okay for null.
230  switch( type ) {
231  case SIMPLE_NULL:
235  break;
236  default:
237  throw InvalidTypeException( "Invalid type for null." );
238  }
239 
240  // Clear out any of the non-trivial data if the current
241  // type has a non-trivial constructor.
242  switch( this->_type ) {
243  case SIMPLE_STRING:
244  v.s.std::string::~string();
245  break;
246  case SIMPLE_OID:
247  v.oid.~NumericOid();
248  break;
249  default:
250  // No special work necessary.
251  ;
252  }
253 
254  this->_type = type;
255  }
256 
264  void set_int32_t( Type type, int32_t i32 = 0 ) throw( InvalidTypeException ) {
265  switch( type ) {
266  case SIMPLE_INTEGER:
268  v.i32 = i32;
269  break;
270  default:
271  throw InvalidTypeException( "Invalid type for int32_t." );
272  }
273 
274  this->_type = type;
275  }
276 
284  void set_uint32_t( Type type, uint32_t u32 = 0 ) throw( InvalidTypeException ) {
285  switch( type ) {
287  case APPLICATION_GAUGE32:
290  v.u32 = u32;
291  break;
292  default:
293  throw InvalidTypeException( "Invalid type for uint32_t." );
294  }
295 
296  this->_type = type;
297  }
298 
306  void set_uint64_t( Type type, uint64_t u64 = 0 ) throw( InvalidTypeException ) {
307  switch( type ) {
310  v.u64 = u64;
311  break;
312  default:
313  throw InvalidTypeException( "Invalid type for uint64_t." );
314  }
315 
316  this->_type = type;
317  }
318 
326  void set_string( Type type, std::string string = std::string() ) throw( InvalidTypeException ) {
327  switch( type ) {
328  case SIMPLE_STRING:
330  case APPLICATION_OPAQUE:
332  new( &v.s ) std::string();
333  v.s = string;
334  break;
335  default:
336  throw InvalidTypeException( "Invalid type for std::string." );
337  }
338 
339  this->_type = type;
340  }
341 
349  void set_oid( Type type, NumericOid oid = NumericOid() ) throw( InvalidTypeException ) {
350  switch( type ) {
351  case SIMPLE_OID:
353  new( &v.oid ) NumericOid();
354  v.oid = oid;
355  break;
356  default:
357  throw InvalidTypeException( "Invalid type for NumericOid." );
358  }
359 
360  this->_type = type;
361  }
362 
363 public:
364  /*
365  * Type testing / casting methods.
366  *
367  * These are broken down into two categories:
368  * 1. The "is_<type>" methods.
369  * These return true or false depending on whether or not you can call the Variant
370  * member function "get_<type>".
371  * 2. The "get_<type>" methods.
372  * These return references to the "<type>" requested from the Variant..
373  * If the Variant type does not match "<type>" (and you should check with the "is_<type>" method),
374  * then this will throw an exception.
375  */
376 
383  bool is_null() const {
384  switch( _type ) {
385  case SIMPLE_NULL:
389  return true;
390  default:
391  return false;
392  }
393  }
394 
401  bool is_int32_t() const {
402  switch( _type ) {
403  case SIMPLE_INTEGER:
404  return true;
405  default:
406  return false;
407  }
408  }
409 
417  int32_t get_int32_t() const throw( InvalidTypeException ) {
418  switch( _type ) {
419  case SIMPLE_INTEGER:
420  return v.i32;
421  default:
422  throw InvalidTypeException( "Invalid type for int32_t." );
423  }
424  }
425 
432  bool is_uint32_t() const {
433  switch( _type ) {
435  case APPLICATION_GAUGE32:
437  return true;
438  default:
439  return false;
440  }
441  }
442 
450  uint32_t get_uint32_t() const throw( InvalidTypeException ) {
451  switch( _type ) {
453  case APPLICATION_GAUGE32:
455  return v.u32;
456  default:
457  throw InvalidTypeException( "Invalid type for uint32_t." );
458  }
459  }
460 
467  bool is_uint64_t() const {
468  switch( _type ) {
470  return true;
471  default:
472  return false;
473  }
474  }
475 
483  uint64_t get_uint64_t() const throw( InvalidTypeException ) {
484  switch( _type ) {
486  return v.u64;
487  default:
488  throw InvalidTypeException( "Invalid type for uint64_t." );
489  }
490  }
491 
498  bool is_string() const {
499  switch( _type ) {
500  case SIMPLE_STRING:
502  case APPLICATION_OPAQUE:
503  return true;
504  default:
505  return false;
506  }
507  }
508 
516  const std::string& get_string() const throw( InvalidTypeException ) {
517  switch( _type ) {
518  case SIMPLE_STRING:
520  case APPLICATION_OPAQUE:
521  return v.s;
522  default:
523  throw InvalidTypeException( "Invalid type for std::string." );
524  }
525  }
526 
533  bool is_oid() const {
534  switch( _type ) {
535  case SIMPLE_OID:
536  return true;
537  default:
538  return false;
539  }
540  }
541 
549  const NumericOid& get_oid() const throw( InvalidTypeException ) {
550  switch( _type ) {
551  case SIMPLE_OID:
552  return v.oid;
553  default:
554  throw InvalidTypeException( "Invalid type for NumericOid." );
555  }
556  }
557 };
558 
559 std::ostream& operator<<( std::ostream& out, const Variant& variant ) {
560  switch( variant.type() ) {
562  out << variant.get_int32_t();
563  break;
565  out << variant.get_string();
566  break;
567  case Variant::SIMPLE_OID:
568  out << variant.get_oid().str();
569  break;
571  out << "null";
572  break;
574  out << variant.get_string();
575  break;
577  out << variant.get_uint32_t();
578  break;
580  out << variant.get_uint32_t();
581  break;
583  out << variant.get_int32_t();
584  break;
586  out << variant.get_string();
587  break;
589  out << variant.get_uint64_t();
590  break;
592  out << "noSuchObject";
593  break;
595  out << "noSuchInstance";
596  break;
598  out << "endOfMibView";
599  break;
600  }
601 
602  return out;
603 }
604 
605 } }
606 
Stored as v.oid.
Definition: variant.hpp:91
~Variant()
Destructor; all memory is freed.
Definition: variant.hpp:159
Functionally SIMPLE_NULL.
Definition: variant.hpp:101
std::ostream & operator<<(std::ostream &out, const Variant &variant)
Definition: variant.hpp:559
Functionally SIMPLE_NULL.
Definition: variant.hpp:99
void set_oid(Type type, NumericOid oid=NumericOid())
This sets the value to an OID.
Definition: variant.hpp:349
std::string str() const
This returns a string representing the OID.
A Variant defines an arbitrary value (within the scope of SNMP).
Definition: variant.hpp:78
bool is_string() const
This returns whether or not the Variant represents a string.
Definition: variant.hpp:498
Stored as v.u64.
Definition: variant.hpp:98
Functionally SIMPLE_NULL.
Definition: variant.hpp:100
Stored as v.s.
Definition: variant.hpp:97
bool is_uint32_t() const
This returns whether or not the Variant represents a 32-bit unsigned integer.
Definition: variant.hpp:432
Not stored.
Definition: variant.hpp:92
Stored as v.u32.
Definition: variant.hpp:96
Variant(const Variant &variant)
Copy constructor; the Variant is set to a copy of the given one.
Definition: variant.hpp:113
Stored as v.u32.
Definition: variant.hpp:95
uint32_t get_uint32_t() const
This returns the 32-bit unsigned integer value of the Variant.
Definition: variant.hpp:450
A NumericOid represents an "true" OID; that is, one identified by a series of integers (as opposed to...
Definition: numericoid.hpp:22
Stored as v.i32.
Definition: variant.hpp:89
Variant()
Default constructor; the Variant is set to a null value.
Definition: variant.hpp:108
bool is_uint64_t() const
This returns whether or not the Variant represents a 64-bit unsigned integer.
Definition: variant.hpp:467
void set_null(Type type)
This restores the Variant to its default, null state.
Definition: variant.hpp:228
void set_string(Type type, std::string string=std::string())
This sets the value to a string.
Definition: variant.hpp:326
uint64_t get_uint64_t() const
This returns the 64-bit unsigned integer value of the Variant.
Definition: variant.hpp:483
bool is_int32_t() const
This returns whether or not the Variant represents a 32-bit signed integer.
Definition: variant.hpp:401
bool is_null() const
This returns whether or not the Variant represents a null value.
Definition: variant.hpp:383
void set_uint32_t(Type type, uint32_t u32=0)
This sets the value to an unsigned, 32-bit integer.
Definition: variant.hpp:284
int32_t get_int32_t() const
This returns the 32-bit signed integer value of the Variant.
Definition: variant.hpp:417
Variant & operator=(const Variant &variant)
Assignment operator; the Variant is set to a copy of the given one.
Definition: variant.hpp:134
Type type() const
This returns the type of the Variant.
Definition: variant.hpp:167
void set_uint64_t(Type type, uint64_t u64=0)
This sets the value to an unsigned, 64-bit integer.
Definition: variant.hpp:306
This represents a failed type conversion or use.
Definition: exception.hpp:116
bool is_oid() const
This returns whether or not the Variant represents an OID.
Definition: variant.hpp:533
Type
This enumeration defines the different types of data that can be represented by this Variant...
Definition: variant.hpp:88
const NumericOid & get_oid() const
This returns the OID value of the Variant.
Definition: variant.hpp:549
Stored as v.s.
Definition: variant.hpp:90
void set_int32_t(Type type, int32_t i32=0)
This sets the value to a signed, 32-bit integer.
Definition: variant.hpp:264
Stored as v.s.
Definition: variant.hpp:93
const std::string & get_string() const
This returns the string value of the Variant.
Definition: variant.hpp:516
Stored as v.u32.
Definition: variant.hpp:94