// // NBO.h // // A module to encapsulate a Network Byte Order integer. // #ifndef __NBO_H__ #define __NBO_H__ // // NBO // // An NBO (Network Byte Order integer) is an unsigned 4-byte // integer with bytes accessible as 4 unsigned chars. // These bytes are in Network Byte Order (big endian), which // means that most significant bits come first. // // An NBO can be used in place of an unsigned int in // mathematical expressions or in function parameters, and // its bytes can be accessed as an array of 4 chars. // // For those less comfortable with operator overloading, there // are also 6 functions to access and manipulate an NBO: // <1> getInt - retreives the value as an unsigned int // <2> setInt - sets the value as an unsigned int // <3> getByte - retrieves the specified byte as a char // <4> setByte - sets the specified byte to a char // <5> setBytes - sets all 4 bytes to individual chars // <6> setArray - sets all 4 bytes to chars passed in as an array // class NBO { public: // // Default Constructor // // Purpose: To create a new NBO with a value of 0. // Argument(s): N/A // Precondition(s): N/A // Returns: N/A // Side Effect: A new NBO with a value of 0 is created. // NBO (); // // Constructor // // Purpose: To create a new NBO with the value specified. // Argument(s): // <1> value: The value // Precondition(s): N/A // Returns: N/A // Side Effect: A new NBO with a value of value is created. // NBO (int value); NBO (unsigned int value); // // Constructor // // Purpose: To create a new NBO with the 4 bytes specified. // Argument(s): // <1> b1 // <2> b2 // <3> b3 // <4> b4: The 4 bytes composing the value. b1 is the most // significant byte. // Precondition(s): N/A // Returns: N/A // Side Effect: A new NBO is created with a value of the // concatination of b1, b2, b3, and b4 into an // unsigned int. // NBO (char b1, char b2, char b3, char b4); // // Constructor // // Purpose: To create a new NBO with the 4 bytes specified. // Argument(s): // <1> a_bytes: A array containing the 4 bytes in Network // Byte Order // Precondition(s): // <1> a_bytes != NULL // Returns: N/A // Side Effect: A new NBO with a value of the concatination of // the first 4 elements of a_bytes into an // unsigned int. // NBO (const char* a_bytes); // // Copy Constructor // // Purpose: To create a new NBO with the same value as the // specified NBO. // Argument(s): // <1> original: The NBO to copy // Precondition(s): N/A // Returns: N/A // Side Effect: A new NBO with the same value as original. // NBO (const NBO& original); // // Assignment Operator // // Purpose: To modify this NBO to have the value specified. // Argument(s): // <1> value: The new value // Precondition(s): N/A // Returns: A reference to this NBO. // Side Effect: This NBO is set to have value value. // NBO& operator= (int value); NBO& operator= (unsigned int value); // // Assignment Operator // // Purpose: To modify this NBO to have the value specified in // an array of chars. // Argument(s): // <1> a_bytes: An array containing the new value as 4 bytes // in Network Byte Order // Precondition(s): // <1> a_bytes != NULL // Returns: A reference to this NBO. // Side Effect: This NBO is set to have a value equal to the // first 4 elements in a_bytes concatenated into // an unsigned int. // NBO& operator= (const char* a_bytes); // // Assignment Operator // // Purpose: To modify this NBO to have the same value as // another NBO. // Argument(s): // <1> original: The NBO to copy // Precondition(s): N/A // Returns: A reference to this NBO. // Side Effect: This NBO is set to have the same value as // original. // NBO& operator= (const NBO& original); // // Destructor // // Purpose: To safely destroy this NBO without memory leaks. // Argument(s): N/A // Precondition(s): N/A // Returns: N/A // Side Effect: All dynamically allocated memory is freed. // ~NBO (); // // Typecast To Unsigned Int // // Purpose: To retrieve the value of this NBO as an unsigned // int. // Argument(s): N/A // Precondition(s): N/A // Returns: The value of this NBO as an unsigned int. // Side Effect: N/A // operator unsigned int () const; // // Subscript Operator // // Purpose: To retrieve a reference to the specified byte in // this NBO. // Argument(s): // <1> index: Which byte // Precondition(s): // <1> index < 4 // Returns: Byte index in this NBO. // Side Effect: N/A // char operator[] (unsigned int index) const; char& operator[] (unsigned int index); // // getInt // // Purpose: To retrieve the value of this NBO as an unsigned // int. // Argument(s): N/A // Precondition(s): N/A // Returns: The value of this NBO as an unsigned int. // Side Effect: N/A // unsigned int getInt () const; // // setInt // // Purpose: To change the value of this NBO to the specified // unsigned int. // Argument(s): // <1> value: The new value // Precondition(s): N/A // Returns: N/A // Side Effect: This NBO is set to have a value of value. // void setInt (unsigned int value); // // getByte // // Purpose: To retrieve the specified byte of this NBO. // Argument(s): // <1> byte: Which index // Precondition(s): // <1> index < 4 // Returns: The value of byte index in this NBO. // Side Effect: N/A // char getByte (unsigned int index) const; // // setByte // // Purpose: To change the value of the specified byte of this NBO. // Argument(s): // <1> index: Which byte // <2> value: The new value // Precondition(s): // <1> index < 4 // Returns: N/A // Side Effect: Byte index in this NBO is set to value. // void setByte (unsigned int index, char value); // // setBytes // // Purpose: To change the value of this NBO to a value // specified as 4 bytes. // Argument(s): // <1> b1 // <2> b2 // <3> b3 // <4> b4: The 4 bytes composing the value. b1 is the most // significant. // Precondition(s): N/A // Returns: N/A // Side Effect: This NBO is set to have a value of the // concatination of b1, b2, b3, and b4 into an // unsigned int. // void setBytes (char b1, char b2, char b3, char b4); // // setArray // // Purpose: To change the value of this NBO to a value // specified as an array of 4 bytes. // Argument(s): // <1> a_bytes: A array containing the new value as 4 bytes // in Network Byte Order // Precondition(s): // <1> a_bytes != NULL // Returns: N/A // Side Effect: This NBO is set to have a value of the // concatination of the first 4 elements in // a_bytes. // void setArray (const char* a_bytes); private: char ma_bytes[4]; }; #endif