libtins  4.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
Public Types | Public Member Functions | Static Public Member Functions | List of all members
Tins::AddressRange< Address > Class Template Reference

Represents a range of addresses. More...

#include <address_range.h>

Public Types

typedef Address address_type
 
typedef AddressRangeIterator
< address_type
const_iterator
 
typedef const_iterator iterator
 The iterator type. More...
 

Public Member Functions

 AddressRange (const address_type &first, const address_type &last, bool only_hosts=false)
 Constructs an address range from two addresses. More...
 
bool contains (const address_type &addr) const
 Indicates whether an address is included in this range. More...
 
const_iterator begin () const
 Returns an interator to the beginning of this range. More...
 
const_iterator end () const
 Returns an interator to the end of this range. More...
 
bool is_iterable () const
 Indicates whether this range is iterable. More...
 

Static Public Member Functions

static AddressRange from_mask (const address_type &first, const address_type &mask)
 Creates an address range from a base address and a network mask. More...
 

Detailed Description

template<typename Address>
class Tins::AddressRange< Address >

Represents a range of addresses.

This class provides a begin()/end() interface which allows iterating through every address stored in it.

Note that when iterating a range that was created using operator/(IPv4Address, int) and the analog for IPv6, the network and broadcast addresses are discarded:

auto range = IPv4Address("192.168.5.0") / 24;
for(const auto& addr : range) {
// process 192.168.5.1-254, .0 and .255 are discarded
process(addr);
}
// That's only valid for iteration, not for AddressRange<>::contains
assert(range.contains("192.168.5.0")); // works
assert(range.contains("192.168.5.255")); // works

Ranges created using AddressRange(address_type, address_type) will allow the iteration over the entire range:

AddressRange<IPv4Address> range("192.168.5.0", "192.168.5.255");
for(const auto& addr : range) {
// process 192.168.5.0-255, no addresses are discarded
process(addr);
}
assert(range.contains("192.168.5.0")); // still valid
assert(range.contains("192.168.5.255")); // still valid

Member Typedef Documentation

template<typename Address>
typedef Address Tins::AddressRange< Address >::address_type

The type of addresses stored in the range.

template<typename Address>
typedef AddressRangeIterator<address_type> Tins::AddressRange< Address >::const_iterator

The iterator type.

template<typename Address>
typedef const_iterator Tins::AddressRange< Address >::iterator

The iterator type.

This is the same type as const_iterator, since the addresses stored in this range are read only.

Constructor & Destructor Documentation

template<typename Address>
Tins::AddressRange< Address >::AddressRange ( const address_type first,
const address_type last,
bool  only_hosts = false 
)
inline

Constructs an address range from two addresses.

The range will consist of the addresses [first, last].

If only_hosts is true, then the network and broadcast addresses will not be available when iterating the range.

If last < first, an std::runtime_error exception is thrown.

Parameters
firstThe first address in the range.
lastThe last address(inclusive) in the range.
only_hostsIndicates whether only host addresses should be accessed when using iterators.

Member Function Documentation

template<typename Address>
const_iterator Tins::AddressRange< Address >::begin ( ) const
inline

Returns an interator to the beginning of this range.

const_iterator pointing to the beginning of this range.

template<typename Address>
bool Tins::AddressRange< Address >::contains ( const address_type addr) const
inline

Indicates whether an address is included in this range.

Parameters
addrThe address to test.
Returns
a bool indicating whether the address is in the range.
template<typename Address>
const_iterator Tins::AddressRange< Address >::end ( ) const
inline

Returns an interator to the end of this range.

const_iterator pointing to the end of this range.

template<typename Address>
static AddressRange Tins::AddressRange< Address >::from_mask ( const address_type first,
const address_type mask 
)
inlinestatic

Creates an address range from a base address and a network mask.

Parameters
firstThe base address.
maskThe network mask to be used.
template<typename Address>
bool Tins::AddressRange< Address >::is_iterable ( ) const
inline

Indicates whether this range is iterable.

Iterable ranges are those for which there is at least one address that could represent a host. For IPv4 ranges, a /31 or /32 ranges does not contain any, therefore it's not iterable. The same is true for /127 and /128 IPv6 ranges.

If is_iterable returns false for a range, then iterating it through the iterators returned by begin() and end() is undefined.

Returns
bool indicating whether this range is iterable.

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