libtins  3.4
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
Public Types | Public Member Functions | Static Public Attributes | Friends | List of all members
Tins::HWAddress< n, Storage > Class Template Reference

Represents a hardware address. More...

#include <hw_address.h>

Public Types

typedef Storage storage_type
 The type of the elements stored in the hardware address. More...
 
typedef storage_typeiterator
 The random access iterator type.
 
typedef const storage_typeconst_iterator
 Const iterator type.
 

Public Member Functions

 HWAddress (const storage_type *ptr=0)
 Constructor from a const storage_type*. More...
 
 HWAddress (const std::string &address)
 Constructs an address from a hex-notation address. More...
 
template<size_t i>
 HWAddress (const char(&address)[i])
 Overload provided basically for string literals. More...
 
template<size_t i>
 HWAddress (const HWAddress< i > &rhs)
 Copy construct from a HWAddress of length i. More...
 
iterator begin ()
 Retrieves an iterator pointing to the begining of the address. More...
 
const_iterator begin () const
 Retrieves a const iterator pointing to the begining of the address. More...
 
iterator end ()
 Retrieves an iterator pointing one-past-the-end of the address. More...
 
const_iterator end () const
 Retrieves a const iterator pointing one-past-the-end of the address. More...
 
bool operator== (const HWAddress &rhs) const
 Compares this HWAddress for equality. More...
 
bool operator!= (const HWAddress &rhs) const
 Compares this HWAddress for in-equality. More...
 
bool operator< (const HWAddress &rhs) const
 Compares this HWAddress for less-than inequality. More...
 
HWAddress operator& (const HWAddress &mask) const
 Apply a mask to this address. More...
 
const size_t size () const
 Retrieves the size of this address. More...
 
bool is_broadcast () const
 Indicates whether this is a broadcast address.
 
bool is_multicast () const
 Indicates whether this is a multicast address.
 
bool is_unicast () const
 Indicates whether this is an unicast address.
 
std::string to_string () const
 Convert this address to a hex-notation std::string address. More...
 
storage_type operator[] (size_t i) const
 Retrieves the i-th storage_type in this address. More...
 
storage_typeoperator[] (size_t i)
 Retrieves the i-th storage_type in this address. More...
 
template<typename OutputIterator >
OutputIterator copy (OutputIterator output) const
 Helper function which copies the address into an output iterator. More...
 

Static Public Attributes

static const size_t address_size = n
 Non-member constant indicating the amount of storage_type elements in this address.
 
static const HWAddress< n,
Storage > 
broadcast = make_broadcast_address()
 The broadcast address.
 

Friends

std::ostream & operator<< (std::ostream &os, const HWAddress &addr)
 Writes this HWAddress in hex-notation to a std::ostream. More...
 

Detailed Description

template<size_t n, typename Storage = uint8_t>
class Tins::HWAddress< n, Storage >

Represents a hardware address.

This class represents a hardware (MAC) address. It can be constructed from it's string representation and you can iterate over the bytes that compose it.

For example:

// Construct it from a string.
HWAddress<6> address("00:01:fa:9e:1a:cd");
// Iterate over its bytes.
for(auto element : address) {
// element will be each of the bytes(\x00, \x01, \xfa, etc)
}

Member Typedef Documentation

template<size_t n, typename Storage = uint8_t>
typedef Storage Tins::HWAddress< n, Storage >::storage_type

The type of the elements stored in the hardware address.

This is the same as the template parameter Storage.

Constructor & Destructor Documentation

template<size_t n, typename Storage = uint8_t>
Tins::HWAddress< n, Storage >::HWAddress ( const storage_type ptr = 0)
inline

Constructor from a const storage_type*.

If no pointer or a null pointer is provided, the address is initialized to 00:00:00:00:00:00.

This constructor is very usefull when passing zero initialized addresses as arguments to other functions. You can use a literal 0, which will be implicitly converted to the empty address.

If a pointer is provided, address_size storage_type elements are copied from the pointer, into the internal address representation.

Parameters
ptrThe pointer from which to construct this address.
template<size_t n, typename Storage = uint8_t>
Tins::HWAddress< n, Storage >::HWAddress ( const std::string &  address)
inline

Constructs an address from a hex-notation address.

This constructor will parse strings in the form:

"00:01:da:fa:..."

And initialize the internal representation accordingly.

Parameters
addressThe hex-notation address to be parsed.
template<size_t n, typename Storage = uint8_t>
template<size_t i>
Tins::HWAddress< n, Storage >::HWAddress ( const char(&)  address[i])
inline

Overload provided basically for string literals.

This constructor takes a const char array of i elements in hex-notation.

See Also
HWAddress::HWAddress(const std::string& address)

This is mostly used when providing string literals. If this where a const char*, then there would be an ambiguity when providing a null pointer.

Parameters
addressThe array of chars containing the hex-notation cstring to be parsed.
template<size_t n, typename Storage = uint8_t>
template<size_t i>
Tins::HWAddress< n, Storage >::HWAddress ( const HWAddress< i > &  rhs)
inline

Copy construct from a HWAddress of length i.

If i is lower or equal than address_size, then i storage_type elements are copied, and the last (n - i) are initialized to the default storage_type value(0 most of the times).

If i is larger than address_size, then only the first address_size elements are copied.

Parameters
rhsThe HWAddress to be constructed from.

Member Function Documentation

template<size_t n, typename Storage = uint8_t>
iterator Tins::HWAddress< n, Storage >::begin ( )
inline

Retrieves an iterator pointing to the begining of the address.

Returns
iterator.
template<size_t n, typename Storage = uint8_t>
const_iterator Tins::HWAddress< n, Storage >::begin ( ) const
inline

Retrieves a const iterator pointing to the begining of the address.

Returns
const_iterator.
template<size_t n, typename Storage = uint8_t>
template<typename OutputIterator >
OutputIterator Tins::HWAddress< n, Storage >::copy ( OutputIterator  output) const
inline

Helper function which copies the address into an output iterator.

This is the same as:

std::copy(begin(), end(), iter);

But since some PDUs return a HWAddress<> by value, this function can be used to avoid temporaries.

Parameters
outputThe output iterator in which to store this address.
Returns
OutputIterator pointing to one-past the last position written.
template<size_t n, typename Storage = uint8_t>
iterator Tins::HWAddress< n, Storage >::end ( )
inline

Retrieves an iterator pointing one-past-the-end of the address.

Returns
iterator.
template<size_t n, typename Storage = uint8_t>
const_iterator Tins::HWAddress< n, Storage >::end ( ) const
inline

Retrieves a const iterator pointing one-past-the-end of the address.

Returns
const_iterator.
template<size_t n, typename Storage = uint8_t>
bool Tins::HWAddress< n, Storage >::operator!= ( const HWAddress< n, Storage > &  rhs) const
inline

Compares this HWAddress for in-equality.

Parameters
rhsThe HWAddress to be compared to.
Returns
bool indicating whether addresses are distinct.
template<size_t n, typename Storage = uint8_t>
HWAddress Tins::HWAddress< n, Storage >::operator& ( const HWAddress< n, Storage > &  mask) const
inline

Apply a mask to this address.

Parameters
maskThe mask to be applied
Returns
The result of applying the mask to this address
template<size_t n, typename Storage = uint8_t>
bool Tins::HWAddress< n, Storage >::operator< ( const HWAddress< n, Storage > &  rhs) const
inline

Compares this HWAddress for less-than inequality.

Parameters
rhsThe HWAddress to be compared to.
Returns
bool indicating whether this address is less-than rhs.
template<size_t n, typename Storage = uint8_t>
bool Tins::HWAddress< n, Storage >::operator== ( const HWAddress< n, Storage > &  rhs) const
inline

Compares this HWAddress for equality.

Parameters
rhsThe HWAddress to be compared to.
Returns
bool indicating whether addresses are equal.
template<size_t n, typename Storage = uint8_t>
storage_type Tins::HWAddress< n, Storage >::operator[] ( size_t  i) const
inline

Retrieves the i-th storage_type in this address.

Parameters
iThe element to retrieve.
template<size_t n, typename Storage = uint8_t>
storage_type& Tins::HWAddress< n, Storage >::operator[] ( size_t  i)
inline

Retrieves the i-th storage_type in this address.

Parameters
iThe element to retrieve.
template<size_t n, typename Storage = uint8_t>
const size_t Tins::HWAddress< n, Storage >::size ( ) const
inline

Retrieves the size of this address.

This effectively returns the address_size constant.

template<size_t n, typename Storage = uint8_t>
std::string Tins::HWAddress< n, Storage >::to_string ( ) const
inline

Convert this address to a hex-notation std::string address.

Returns
std::string containing the hex-notation address.

Friends And Related Function Documentation

template<size_t n, typename Storage = uint8_t>
std::ostream& operator<< ( std::ostream &  os,
const HWAddress< n, Storage > &  addr 
)
friend

Writes this HWAddress in hex-notation to a std::ostream.

Parameters
osThe stream in which to write the address.
addrThe parameter to be written.
Returns
std::ostream& pointing to the os parameter.

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