<< Back to man.lupaworld.com

9.       Object Id Class

Object Modeling Technique (OMT) view of the SNMP++ Oid Class

 

9.1.  The Object Identifier Class

The Object Identification (Oid) class is the encapsulation of an SMI object identifier. The SMI object is a data identifier for a data element found in a Management Information Base (MIB), as defined by a MIB definition. The SMI Oid, its related structures and functions, are a natural fit for object orientation. In fact, the Oid class shares many common features to the C++ String class. For those of you familiar with the C++ String class or Microsoft’s Foundation Classes (MFC) CString class, the Oid class will be familiar and easy to use. The Oid class is designed to be efficient and fast. The Oid class allows definition and manipulation of object identifiers.  The Oid Class is fully portable and does not rely on any SNMP API to be present. The Oid class may be compiled and used with any ANSI C++ compiler.

 

9.2.   Overview of Oid Class Member Functions

 

Oid Class Member Functions

Description

Constructors

 

     Oid::Oid( void);

Construct an empty Oid.

     Oid::Oid( const char *dotted_string);

Construct an Oid with a dotted string.

     Oid::Oid( const Oid &oid);

Construct an Oid with another Oid, copy constructor.

     Oid::Oid( const unsigned long *data, int len);

Construct an Oid with a pointer and length.

Destructor

 

     Oid::~Oid( );

Destroy the Oid, frees up all memory held.


 

9.3.   Overview of Oid Class Member Functions Continued

 

Oid Class Member Functions

Description

Overloaded Operators

 

     Oid & operator = ( const char *dotted_string);

Assign an Oid a dotted string.

     Oid & operator = ( const Oid &oid);

Assign an Oid an Oid.

     int operator == ( const Oid &lhs, const Oid& rhs);

Compare two Oids for equivalence.

     int operator == ( const Oid& lhs, const char*dotted_string);

Compare an Oid and a dotted string for equivalence.

     int operator != ( const Oid &lhs, const Oid& rhs);

Compare two Oids for not equal.

     int operator != ( const Oid & lhs, const char *dotted_string);

Compare an Oid and dotted string for not equal.

     int operator < ( const Oid &lhs, const Oid& rhs);

Determine if one Oid is less than another Oid.

     int operator < ( const Oid &lhs, const char *dotted_string);

Determine if an Oid is less than a dotted string.

     int operator <=( const Oid &lhs,const Oid &rhs);

Determine if one Oid is less than or equal to another Oid.

     int operator <= ( const Oid &lhs, const char *dotted_string);

Determine if one Oid is less than or equal to a dotted string.

     int operator > ( const Oid &lhs, const Oid &rhs);

Determine if one Oid is greater than another Oid.

     int operator > ( const Oid &lhs, const char * dotted_string);

Determine if one Oid is greater than a dotted string.

     int operator >= ( const Oid&lhs, const Oid &rhs);

Determine if one Oid is greater than or equal to another Oid.

     int operator >= ( const Oid &lhs, const char* dotted_string);

Determine if one Oid is greater than or equal to a dotted string.

     Oid& operator += ( const char *dotted_string);

Append a dotted string to an Oid.

     Oid& operator +=( const unsigned long i);

Append a single value to a dotted string.

     Oid& operator+=( const Oid& oid);

Append one Oid to another Oid.

     unsigned long &operator [ ] ( int position);

Access an individual sub-element of an Oid, read or write.

Output Member Functions

 

     char * get_printable( const unsigned int n);

Return the dotted format where n specifies how many sub elements to include.

     char *get_printable( const unsigned long s, const unsigned long n);

Return the dotted format where s specifies the start position and n specifies how many sub elements to include.

     char *get_printable();

Return the entire Oid as a dotted string.

     operator char *();

Same as get_printable().

Miscellaneous Member Functions

 

     set_data (const unsigned long *data,const unsigned long n);

Set the data of an Oid using a pointer and a length.

     unsigned long len( );

Return the length, number of sub elements, in an Oid.

     trim(  const unsigned long n=1);

Trim off the rightmost sub element of an Oid, default 1.

     nCompare( const unsigned long n, const Oid& oid);

Compare the first n sub-ids (left to right ) of an Oid parameter.

     RnCompare( const unsigned long n, const Oid& oid);

Compare the last n sub-ids ( right to left) of an Oid parameter.

     int valid( );

Return the validity of an Oid.


 

9.4.   Some Oid Class Examples

The following examples show different ways in which to use the Oid class. The Oid class does not require or depend on any other libraries or modules. The following  code is ANSI/ISO C++ compatible.

 

#include oid.h

void oid_example()

{

   // construct an Oid with a dotted string and print it out

   Oid o1("1.2.3.4.5.6.7.8.9.1");

   cout << “o1= “ << o1.get_printable ();

 

   // construct an Oid with another Oid and print it out

   Oid o2(o1);

   cout << “o2=  << o2.get_printable();

 

   // trim o2’s last value and print it out

   o2.trim(1);

   cout <<  “o2=  ” << o2.get_printable();

 

   // add a 2 value to the end of o2 and print it out

   o2+=2;

   cout << “o2=  ” << o2.get_printable();

 

   // create a new Oid, o3

   Oid o3;

 

   // assign o3 a value and print it out

   o3="1.2.3.4.5.6.7.8.9.3";

   cout << “o3= ” << o3.get_printable();

 

   // create o4

   Oid o4;

   // assign o4 o1’s value

  o4=o1;

 

   // trim off o4 by 1

   o4.trim(1);

 

   // concat a 4 onto o4 and print it out

   o4+=”.4”;

   cout <<  “o4= ” << o4.get_printable();

 

   // make o5 from o1 and print it out

   Oid o5(o1);

   cout << “o5= ” << o5.get_printable();

 

 


 


Some Oid Class Examples Continued...

 

   // compare two not equal oids

   if (o1==o2)   cout << "O1 EQUALS O2";

   else    cout << "o1 not equal to o2";

              

   // print out a piece of o1

   cout << "strval(3) of O1 = “ << o1.get_printable(3);

 

   // print out a piece of o1

   cout << "strval(1,3) of O1 = “ << o1.get_printable(1,3);

 

   // set o1's last subid

   o1[ o1.len()-1] = 49;

   cout << "O1 modified = “ << o1.get_printable();

 

   // set o1's 3rd subid

   o1[2]=49;

   cout << "O1 modified = “ << o1.get_printable();

  

   // get the last subid of 02

   cout << "last of o2 = “ << o2[o2.len()-1];

  

   // get the 3rd subid of 02

   cout << "3rd of o2 = “ << o2[2];

  

   // ncompare

   if (o1.nCompare(3,o2))

      cout << "nCompare o1,o2,3 ==";

   else

     cout << "nCompare o1,o2,3 !=";               

    

   // make an array of oids

   Oid oids[30]; int w;

   for ( w=0;w<30;w++)                          

   {

     oids[w] = "300.301.302.303.304.305.306.307";

     oids[w] += (w+1);

   }  

   for (w=0;w<25;w++)

   {

     sprintf( msg,"Oids[%d] = %s",w, oids[w].get_printable());

     printf(“%s”,msg, strlen(msg));

    } 

}