Datatypes C++ Module¶
-
struct Result¶
Structure to represent an SNMP result.
This structure holds the information retrieved from an SNMP operation, including the OID, index, type, and value of the retrieved data.
Public Types
-
using ConvertedValue = std::variant<int, uint32_t, uint64_t, double, std::string, std::vector<unsigned char>>¶
Public Functions
-
std::string _to_string() const¶
Converts the Result object to a string representation.
This method generates a human-readable string representation of the Result object, including all its members.
- Returns:
A string representation of the Result object.
-
ConvertedValue _make_converted_value(std::string const &type, std::string const &value)¶
Factory function to obtain the appropriate ConvertedValue type based on an SNMP type string.
This function takes an SNMP type string and a value string, then attempts to convert the value into the corresponding C++ type. The converted value is stored in a ConvertedValue, which is a std::variant.
- Parameters:
snmpType – The SNMP type as a string (e.g., “INTEGER”, “OCTETSTR”). https://github.com/net-snmp/net-snmp/blob/02bee0fe32a4136ade3de137eef6c5acdfeed508/include/net-snmp/library/parse.h#L154-L170
value – The value as a string to be converted.
- Returns:
ConvertedValue The value converted to the appropriate C++ type, wrapped in a std::variant.
-
void update_converted_value()¶
Updates the converted_value member by converting the current type and value.
This method recalculates and assigns the converted_value field using the current values of the type and value members. It utilizes the private helper function _make_converted_value to perform the conversion.
Typically called after type or value has changed to ensure converted_value is up-to-date.
Public Members
-
std::string oid = ""¶
Object Identifier (OID) of the retrieved data.
-
std::string index = ""¶
Index of the retrieved data (if applicable).
-
std::string type = ""¶
Data type of the retrieved value.
-
std::string value = ""¶
Actual value of the retrieved data.
-
ConvertedValue converted_value = ""¶
Converted value of the type,value data.
-
using ConvertedValue = std::variant<int, uint32_t, uint64_t, double, std::string, std::vector<unsigned char>>¶
Result.converted_value Types and Results¶
This section outlines the possible C++ data types that result from the converted_value
member of the Result
struct. The conversion is based on the SNMP type
string. The underlying C++ type is a std::variant
that can hold one of the following types or an error string.
Numeric Types¶
These SNMP types are converted into their corresponding C++ numeric equivalents. The conversion logic is designed to extract numerical data from common SNMP output formats, such as "60000 milli-seconds"
or "up(1)"
.
Integer / Integer32¶
SNMP Types:
integer
,integer32
C++ Type:
int32_t
(signed 32-bit integer)Example Input:
"42"
->42
Gauge32 / Counter32 / TimeTicks¶
SNMP Types:
gauge32
,counter32
,timeticks
C++ Type:
uint32_t
(unsigned 32-bit integer)Example Input:
"363647793"
->363647793
Counter64¶
SNMP Types:
counter64
C++ Type:
uint64_t
(unsigned 64-bit integer)Example Input:
"18446744073709551615"
->18446744073709551615
Byte Vector Types¶
These types represent raw byte data and are converted into a vector of unsigned characters.
Hex-STRING¶
SNMP Type:
hex-string
C++ Type:
std::vector<unsigned char>
Description: Parses a space-separated string of hexadecimal bytes. Each byte must be 1 or 2 hex characters long.
Example Input:
"80 00 1F 88 04 01 01"
->{0x80, 0x00, 0x1F, 0x88, 0x04, 0x01, 0x01}
Special Case: An empty or whitespace-only input string results in an empty vector.
OctetStr¶
SNMP Type:
octetstr
C++ Type:
std::vector<unsigned char>
Description: Converts the raw string value directly into a vector of its character bytes.
Example Input:
"Hello"
->{'H', 'e', 'l', 'l', 'o'}
or{0x48, 0x65, 0x6c, 0x6c, 0x6f}
String Types¶
For many SNMP types, the most useful representation is the original string itself. These types are passed through without conversion.
- SNMP Types:
string
oid
,objid
,objidentity
ipaddress
,network address
opaque
bitstring
nsapaddress
traptype
,notiftype
objgroup
,notifgroup
,modid
,agentcap
,modcomp
null
other
C++ Type:
std::string
Description: The original string value from the
Result
is returned as is.Example Input:
"1.3.6.1.2.1.1.1.0"
->"1.3.6.1.2.1.1.1.0"
Error and Fallback Conditions¶
If a conversion fails or the type is unknown, the converted_value
will hold a std::string
describing the issue.
Conversion Error¶
C++ Type:
std::string
Description: Occurs when a numeric or hex conversion fails due to malformed input. The string will contain a descriptive error message.
Example:
"Integer Conversion Error: std::invalid_argument"
Example:
"Hex-STRING Conversion Error: Malformed hex part 'G0'"
Unknown Type¶
C++ Type:
std::string
Description: If the SNMP type string does not match any of the known types listed above, a generic fallback message is returned.
Value:
"Unknown Type Conversion"