I have created a wrapper class around an oid array (which is actually an array of long elements).
This wrapper class contains a pointer to an oid and the length of the array.
I'm using this wrapper class as a key in a std::map. When calling at of find on this map it uses the less-than operator overloader for my wrapper.
Fetching from this map while all keys are of equal length goes fine:
const oid oid1[] = { 1,3,6,1,4,1,40850,1,3,1 };
const oid oid2[] = { 1,3,6,1,4,1,40850,1,3,2 };
const oid oid3[] = { 1,3,6,1,4,1,40850,1,3,3 };
Fetching from this map while keys have different length does not work The function at/find throws out_of_range exception.
const oid oid4[] = { 1,3,6,1,4,1,40850,1,3,1,9,1 };
const oid oid5[] = { 1,3,6,1,4,1,40850,1,3,1,9,2 };
const oid oid6[] = { 1,3,6,1,4,1,40850,1,3,1,9,3 };
My wrapper class: oid_wrapper.cpp
#ifndef OID_WRAPPER_H_
#define OID_WRAPPER_H_
#include <ostream>
#include <sstream>
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
class oid_wrapper {
friend std::ostream& operator<<(std::ostream& output, const oid_wrapper& wrapper);
public:
oid_wrapper(const oid* _oid_value, int _oid_length) : oid_value(0), oid_length(_oid_length) {
oid_value = snmp_duplicate_objid(_oid_value, oid_length);
}
const oid* get_oid() const { return oid_value; }
const int& get_length() const { return oid_length; }
/**
* copy operator overloader
*/
oid_wrapper& operator=(const oid_wrapper& wrapper) {
oid_value = snmp_duplicate_objid(wrapper.get_oid(), wrapper.get_length());
oid_length = wrapper.get_length();
return *this;
}
/**
* equal operator overloader
*/
bool operator==(const oid_wrapper& rhs) const {
const oid* rhs_oid = rhs.get_oid();
const int rhs_length = rhs.get_length();
if (netsnmp_oid_equals(oid_value, oid_length, rhs_oid, rhs_length) == 0) {
return true;
} else {
return false;
}
}
/**
* less-than operator overloader
*/
bool operator<(const oid_wrapper& rhs) const {
const oid* rhs_oid = rhs.get_oid();
const int rhs_length = rhs.get_length();
bool retval = false;
if (oid_length < rhs_length) { return true; }
for (int i = 0; i < oid_length; i++) {
if (oid_value[i] < rhs_oid[i]) { return true; }
}
return false;
}
const long getLastNumberInOid() const { return oid_value[oid_length - 1]; }
bool operator!=(const oid_wrapper& rhs) { return !operator==(rhs); }
bool operator>(const oid_wrapper& rhs) { return !operator<(rhs); }
bool operator<=(const oid_wrapper& rhs) { return !operator>(rhs); }
bool operator>=(const oid_wrapper& rhs) { return !operator<(rhs); }
private:
const oid* oid_value;
int oid_length;
};
/**
* output operator overloader
*/
inline std::ostream& operator<<(std::ostream& output, const oid_wrapper& wrapper) {
const oid* oid_value = wrapper.get_oid();
int oid_length = wrapper.get_length();
for(int i = 0; i < oid_length; i++) {
output << oid_value[i];
if (i != oid_length - 1) {
output << ".";
}
}
return output;
}
#endif /* OID_WRAPPER_H_ */

New Topic/Question
Reply



MultiQuote




|