Dangerous SNMP
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
Variant Class Reference

A Variant defines an arbitrary value (within the scope of SNMP). More...

#include <variant.hpp>

Public Types

enum  Type {
  SIMPLE_INTEGER, SIMPLE_STRING, SIMPLE_OID, SIMPLE_NULL,
  APPLICATION_IPADDRESS, APPLICATION_COUNTER32, APPLICATION_GAUGE32, APPLICATION_TIMETICKS,
  APPLICATION_OPAQUE, APPLICATION_COUNTER64, CONTEXT_NOSUCHOBJECT, CONTEXT_NOSUCHINSTANCE,
  CONTEXT_ENDOFMIBVIEW
}
 This enumeration defines the different types of data that can be represented by this Variant. More...
 

Public Member Functions

 Variant ()
 Default constructor; the Variant is set to a null value.
 
 Variant (const Variant &variant)
 Copy constructor; the Variant is set to a copy of the given one.
 
Variantoperator= (const Variant &variant)
 Assignment operator; the Variant is set to a copy of the given one.
 
 ~Variant ()
 Destructor; all memory is freed. More...
 
Type type () const
 This returns the type of the Variant. More...
 
void set_null (Type type) throw ( InvalidTypeException )
 This restores the Variant to its default, null state. More...
 
void set_int32_t (Type type, int32_t i32=0) throw ( InvalidTypeException )
 This sets the value to a signed, 32-bit integer. More...
 
void set_uint32_t (Type type, uint32_t u32=0) throw ( InvalidTypeException )
 This sets the value to an unsigned, 32-bit integer. More...
 
void set_uint64_t (Type type, uint64_t u64=0) throw ( InvalidTypeException )
 This sets the value to an unsigned, 64-bit integer. More...
 
void set_string (Type type, std::string string=std::string()) throw ( InvalidTypeException )
 This sets the value to a string. More...
 
void set_oid (Type type, NumericOid oid=NumericOid()) throw ( InvalidTypeException )
 This sets the value to an OID. More...
 
bool is_null () const
 This returns whether or not the Variant represents a null value. More...
 
bool is_int32_t () const
 This returns whether or not the Variant represents a 32-bit signed integer. More...
 
int32_t get_int32_t () const throw ( InvalidTypeException )
 This returns the 32-bit signed integer value of the Variant. More...
 
bool is_uint32_t () const
 This returns whether or not the Variant represents a 32-bit unsigned integer. More...
 
uint32_t get_uint32_t () const throw ( InvalidTypeException )
 This returns the 32-bit unsigned integer value of the Variant. More...
 
bool is_uint64_t () const
 This returns whether or not the Variant represents a 64-bit unsigned integer. More...
 
uint64_t get_uint64_t () const throw ( InvalidTypeException )
 This returns the 64-bit unsigned integer value of the Variant. More...
 
bool is_string () const
 This returns whether or not the Variant represents a string. More...
 
const std::string & get_string () const throw ( InvalidTypeException )
 This returns the string value of the Variant. More...
 
bool is_oid () const
 This returns whether or not the Variant represents an OID. More...
 
const NumericOidget_oid () const throw ( InvalidTypeException )
 This returns the OID value of the Variant. More...
 

Detailed Description

A Variant defines an arbitrary value (within the scope of SNMP).

It can have any one of a number of different "types". Thus, a Variant can represent a null value, an integer, a string, a 64-bit counter, etc.

There are four "simple" types in SNMP (imported directly from ASN.1):

  1. SIMPLE_INTEGER: This represents a 32-bit signed integer.
  2. SIMPLE_STRING: This represents a string (technically, an array of bytes).
  3. SIMPLE_OID: This represents an SNMP Object Identifier (OID).
  4. SIMPLE_NULL: This represents a null value. This is the default type for a Variant instance.

There are six "application" types in SNMP (built from simple types):

  1. APPLICATION_IPADDRESS: This represents an IPv4 address (thus, it is four bytes long). In SNMP, this is implemented as a 4-byte string.
  2. APPLICATION_COUNTER32: This represents a 32-bit counter value. Counter values may only legally increment (unless, of course, they are reset in some capacity or overflow).
  3. APPLICATION_GAUGE32: This represents a 32-bit unsigned integer.
  4. APPLICATION_TIMETICKS: This also represents a 32-bit unsigned integer, but the units are understood to be 1/100 seconds.
  5. APPLICATION_OPAQUE: This represents an arbitrary collection of bytes. Conceptually, it is the same thing as a string, except that it is understood that the contents should not be tried to be interpreted.
  6. APPLICATION_COUNTER64: This represents a 64-bit counter value. Counter values may only legally increment (unless, of course, they are reset in some capacity or overflow).

Finally, there are three "error" types in SNMP (added in SNMPv2). These allow for much better error reporting in "get"-style operations.

  1. CONTEXT_NOSUCHOBJECT: This is used as a "value" to mean that there is no such OID.
  2. CONTEXT_NOSUCHINSTANCE: This is used as a "value" to mean that there is no such conceptual instance.
  3. CONTEXT_ENDOFMIBVIEW: This is used as a "value" to mean that there is no more to be found.

If you are interested in determining the exact type of a Variant, then you may use the type method, which will return the Variant::Type of the instance.

If you are interested in extracting the value from a Variant, then you must first check the type by using one of the is_... methods, each of which tests for a particular C++ return type (for example, is_string will return true for SIMPLE_STRING, APPLICATION_IPADDRESS, and APPLICATION_OPAQUE). Once you have determined the appropriate C++ type, you may then use the matching get_... function to retrieve the value (for example, if is_string returns true, then you may call get_string).


Implementation note: Variant is defined entirely in this header file.

Member Enumeration Documentation

enum Type

This enumeration defines the different types of data that can be represented by this Variant.

For example, a Variant instance may represent a simple integer, or a simple string. However, it might also be an OID, or a null value. In addition, there are some SNMP-specific things (such as IP address, or timeticks) that it can be.

Enumerator
SIMPLE_INTEGER 

Stored as v.i32.

SIMPLE_STRING 

Stored as v.s.

SIMPLE_OID 

Stored as v.oid.

SIMPLE_NULL 

Not stored.

APPLICATION_IPADDRESS 

Stored as v.s.

APPLICATION_COUNTER32 

Stored as v.u32.

APPLICATION_GAUGE32 

Stored as v.u32.

APPLICATION_TIMETICKS 

Stored as v.u32.

APPLICATION_OPAQUE 

Stored as v.s.

APPLICATION_COUNTER64 

Stored as v.u64.

CONTEXT_NOSUCHOBJECT 

Functionally SIMPLE_NULL.

CONTEXT_NOSUCHINSTANCE 

Functionally SIMPLE_NULL.

CONTEXT_ENDOFMIBVIEW 

Functionally SIMPLE_NULL.

Constructor & Destructor Documentation

~Variant ( )
inline

Destructor; all memory is freed.

Since the Variant internally uses a union with non-trivial components, their destructors will need to be called manually.

Member Function Documentation

int32_t get_int32_t ( ) const
throw (InvalidTypeException
)
inline

This returns the 32-bit signed integer value of the Variant.

You should first check that this is the correct type by calling is_int32_t.

Returns
The 32-bit signed integer value.
Exceptions
InvalidTypeExceptionif the Variant is not of this format.
const NumericOid& get_oid ( ) const
throw (InvalidTypeException
)
inline

This returns the OID value of the Variant.

You should first check that this is the correct type by calling is_oid.

Returns
The OID value.
Exceptions
InvalidTypeExceptionif the Variant is not of this format.
const std::string& get_string ( ) const
throw (InvalidTypeException
)
inline

This returns the string value of the Variant.

You should first check that this is the correct type by calling is_string.

Returns
The string value.
Exceptions
InvalidTypeExceptionif the Variant is not of this format.
uint32_t get_uint32_t ( ) const
throw (InvalidTypeException
)
inline

This returns the 32-bit unsigned integer value of the Variant.

You should first check that this is the correct type by calling is_uint32_t.

Returns
The 32-bit unsigned integer value.
Exceptions
InvalidTypeExceptionif the Variant is not of this format.
uint64_t get_uint64_t ( ) const
throw (InvalidTypeException
)
inline

This returns the 64-bit unsigned integer value of the Variant.

You should first check that this is the correct type by calling is_uint64_t.

Returns
The 64-bit unsigned integer value.
Exceptions
InvalidTypeExceptionif the Variant is not of this format.
bool is_int32_t ( ) const
inline

This returns whether or not the Variant represents a 32-bit signed integer.

In this case, you may retrieve the value using get_int32_t.

Returns
true if the Variant is of type SIMPLE_INTEGER.
bool is_null ( ) const
inline

This returns whether or not the Variant represents a null value.

In this case, there is no corresponding get_null method.

Returns
true if the Variant is of type SIMPLE_NULL, CONTEXT_NOSUCHOBJECT, CONTEXT_NOSUCHINSTANCE, or CONTEXT_ENDOFMIBVIEW.
bool is_oid ( ) const
inline

This returns whether or not the Variant represents an OID.

In this case, you may retrieve the value using get_oid.

Returns
true if the Variant is of type SIMPLE_OID.
bool is_string ( ) const
inline

This returns whether or not the Variant represents a string.

In this case, you may retrieve the value using get_string.

Returns
true if the Variant is of type SIMPLE_STRING, APPLICATION_IPADDRESS, or APPLICATION_OPAQUE.
bool is_uint32_t ( ) const
inline

This returns whether or not the Variant represents a 32-bit unsigned integer.

In this case, you may retrieve the value using get_uint32_t.

Returns
true if the Variant is of type APPLICATION_COUNTER32, APPLICATION_GAUGE32, APPLICATION_TIMETICKS.
bool is_uint64_t ( ) const
inline

This returns whether or not the Variant represents a 64-bit unsigned integer.

In this case, you may retrieve the value using get_uint64_t.

Returns
true if the Variant is of type APPLICATION_COUNTER64.
void set_int32_t ( Type  type,
int32_t  i32 = 0 
)
throw (InvalidTypeException
)
inline

This sets the value to a signed, 32-bit integer.

Parameters
typeThe new type for the Variant. This may be SIMPLE_INTEGER.
i32The signed, 32-bit integer.
Exceptions
InvalidTypeExceptionif type is not one of the allowed types.
void set_null ( Type  type)
throw (InvalidTypeException
)
inline

This restores the Variant to its default, null state.

Note: this will be called internally to clear out the string and OID data when the type of the Variant is reassigned.

Parameters
typeThe new type for the Variant. This may be SIMPLE_NULL, CONTEXT_NOSUCHOBJECT, CONTEXT_NOSUCHINSTANCE, or CONTEXT_ENDOFMIBVIEW.
Exceptions
InvalidTypeExceptionif type is not one of the allowed types.
void set_oid ( Type  type,
NumericOid  oid = NumericOid() 
)
throw (InvalidTypeException
)
inline

This sets the value to an OID.

Parameters
typeThe new type for the Variant. This may be SIMPLE_OID.
oidThe string.
Exceptions
InvalidTypeExceptionif type is not one of the allowed types.
void set_string ( Type  type,
std::string  string = std::string() 
)
throw (InvalidTypeException
)
inline

This sets the value to a string.

Parameters
typeThe new type for the Variant. This may be SIMPLE_STRING.
stringThe signed integer.
Exceptions
InvalidTypeExceptionif type is not one of the allowed types.
void set_uint32_t ( Type  type,
uint32_t  u32 = 0 
)
throw (InvalidTypeException
)
inline

This sets the value to an unsigned, 32-bit integer.

Parameters
typeThe new type for the Variant. This may be APPLICATION_COUNTER32, APPLICATION_GAUGE32, or APPLICATION_TIMETICKS.
u32The unsigned, 32-bit integer.
Exceptions
InvalidTypeExceptionif type is not one of the allowed types.
void set_uint64_t ( Type  type,
uint64_t  u64 = 0 
)
throw (InvalidTypeException
)
inline

This sets the value to an unsigned, 64-bit integer.

Parameters
typeThe new type for the Variant. This may be APPLICATION_COUNTER64.
u64The unsigned, 64-bit integer.
Exceptions
InvalidTypeExceptionif type is not one of the allowed types.
Type type ( ) const
inline

This returns the type of the Variant.

Returns
The Variant::Type for the variant.

The documentation for this class was generated from the following file: