libDwm-0.9.45
Dwm::Ipv6PrefixMap< T, Hash > Class Template Reference

A wrapper around an unordered_map of T keyed by Ipv6Prefix using Hash as the hash function. More...

#include <DwmIpv6PrefixMap.hh>

Public Types

using MapType = std::unordered_map<Ipv6Prefix,T,Hash>
 The types of our encapsulated containers.
 
using LengthMapType = std::map<uint8_t,uint64_t>
 
using MapPair = std::pair<MapType,LengthMapType>
 Internally we keep our maps in a pair.
 

Public Member Functions

 Ipv6PrefixMap ()
 Default constructor.
 
void Add (const Ipv6Prefix &pfx, const T &value)
 Adds the given value to the map at key pfx.
 
void Add (std::unique_lock< std::shared_mutex > &lck, const Ipv6Prefix &pfx, const T &value)
 Adds the given value to the map at key pfx.
 
bool Find (const Ipv6Prefix &pfx, T &value) const
 Find the entry with key pfx.
 
bool Find (std::shared_lock< std::shared_mutex > &lck, const Ipv6Prefix &pfx, T &value) const
 Find the entry with key pfx.
 
bool Find (std::unique_lock< std::shared_mutex > &lck, const Ipv6Prefix &pfx, T &value) const
 Find the entry with key pfx.
 
bool FindLongest (const Ipv6Address &addr, std::pair< Ipv6Prefix, T > &value) const
 Find the longest match for the given IPv6 address addr.
 
bool FindLongest (std::shared_lock< std::shared_mutex > &lck, const Ipv6Address &addr, std::pair< Ipv6Prefix, T > &value) const
 Find the longest match for the given IPv6 address addr.
 
bool FindLongest (std::unique_lock< std::shared_mutex > &lck, const Ipv6Address &addr, std::pair< Ipv6Prefix, T > &value) const
 Find the longest match for the given IPv6 address addr.
 
bool FindMatches (const Ipv6Address &addr, std::vector< std::pair< Ipv6Prefix, T > > &values) const
 Find all matches for the given IPv6 address addr.
 
bool FindMatches (std::shared_lock< std::shared_mutex > &lck, const Ipv6Address &addr, std::vector< std::pair< Ipv6Prefix, T > > &values) const
 Find all matches for the given IPv6 address addr.
 
bool FindMatches (std::unique_lock< std::shared_mutex > &lck, const Ipv6Address &addr, std::vector< std::pair< Ipv6Prefix, T > > &values) const
 Find all matches for the given IPv6 address addr.
 
bool Remove (const Ipv6Prefix &pfx)
 Removes the entry for the given prefix pfx.
 
bool Remove (std::unique_lock< std::shared_mutex > &lck, const Ipv6Prefix &pfx)
 Removes the entry for the given prefix pfx.
 
void Clear ()
 Clears the map.
 
void Clear (std::unique_lock< std::shared_mutex > &lck)
 Clears the map.
 
bool Empty () const
 Returns true if the map is empty.
 
bool Empty (std::shared_lock< std::shared_mutex > &lck) const
 Returns true if the map is empty.
 
bool Empty (std::unique_lock< std::shared_mutex > &lck) const
 Returns true if the map is empty.
 
std::istream & Read (std::istream &is)
 Reads the Ipv6PrefixMap from an istream. Returns the istream.
 
std::ostream & Write (std::ostream &os) const
 Writes the Ipv6PrefixMap to an ostream. Returns the ostream.
 
size_t Read (FILE *f)
 Reads the Ipv6PrefixMap from f.
 
size_t Write (FILE *f) const
 Writes the Ipv6PrefixMap to f.
 
ssize_t Read (int fd)
 Reads the Ipv6PrefixMap from file descriptor fd.
 
ssize_t Write (int fd) const
 Writes the Ipv6PrefixMap to file descriptor fd.
 
int Read (gzFile gzf)
 Reads the Ipv6PrefixMap from gzf.
 
int Write (gzFile gzf) const
 Writes the Ipv6PrefixMap to gzf.
 
int BZRead (BZFILE *bzf)
 Reads the Ipv6PrefixMap from bzf.
 
int BZWrite (BZFILE *bzf) const
 Writes the Ipv6PrefixMap to bzf.
 
uint64_t StreamedLength () const
 Returns the number of bytes that would be written if the Ipv6PrefixMap was written to a FILE, file descriptor or ostream.
 
bool Read (boost::asio::ip::tcp::socket &s, boost::system::error_code &ec)
 Reads the Ipv6PrefixMap from s.
 
bool Read (boost::asio::local::stream_protocol::socket &s, boost::system::error_code &ec)
 Reads the Ipv6PrefixMap from s.
 
bool Read (boost::asio::generic::stream_protocol::socket &s, boost::system::error_code &ec)
 Reads the Ipv6PrefixMap from s.
 
bool Write (boost::asio::ip::tcp::socket &s, boost::system::error_code &ec) const
 Writes the Ipv6PrefixMap to s.
 
bool Write (boost::asio::local::stream_protocol::socket &s, boost::system::error_code &ec) const
 Writes the Ipv6PrefixMap to s.
 
bool Write (boost::asio::generic::stream_protocol::socket &s, boost::system::error_code &ec) const
 Writes the Ipv6PrefixMap to s.
 
std::shared_lock< std::shared_mutex > SharedLock () const
 Returns a shared lock of the Ipv6PrefixMap, in the locked state.
 
std::unique_lock< std::shared_mutex > UniqueLock ()
 Returns a unique lock of the Ipv6PrefixMap, in the locked state.
 

Detailed Description

template<typename T, typename Hash = OurIpv6PrefixHash>
class Dwm::Ipv6PrefixMap< T, Hash >

A wrapper around an unordered_map of T keyed by Ipv6Prefix using Hash as the hash function.

This isn't the fastest IPv6 prefix container, but... it's fast enough for my uses, and is very little code to maintain since it leverages the C++ standard library facilities.

Members are threadsafe (using a mutex).

Member Typedef Documentation

◆ MapPair

template<typename T , typename Hash = OurIpv6PrefixHash>
using Dwm::Ipv6PrefixMap< T, Hash >::MapPair = std::pair<MapType,LengthMapType>

Internally we keep our maps in a pair.

This simplifies the I/O functions.

Member Function Documentation

◆ Add() [1/2]

template<typename T , typename Hash = OurIpv6PrefixHash>
void Dwm::Ipv6PrefixMap< T, Hash >::Add ( const Ipv6Prefix & pfx,
const T & value )
inline

Adds the given value to the map at key pfx.

If the entry already exists, it will be replaced.

Here is the call graph for this function:

◆ Add() [2/2]

template<typename T , typename Hash = OurIpv6PrefixHash>
void Dwm::Ipv6PrefixMap< T, Hash >::Add ( std::unique_lock< std::shared_mutex > & lck,
const Ipv6Prefix & pfx,
const T & value )
inline

Adds the given value to the map at key pfx.

If the entry already exists, it will be replaced. lck must be a lock created with UniqueLock() and must be locked.

Here is the call graph for this function:

◆ BZRead()

template<typename T , typename Hash = OurIpv6PrefixHash>
int Dwm::Ipv6PrefixMap< T, Hash >::BZRead ( BZFILE * bzf)
inline

Reads the Ipv6PrefixMap from bzf.

Returns the number of bytes read on success, -1 on failure. Be wary; the integer return is risky (could overflow) but it's what bzlib's BZ2_bzRead() returns and we trickled up the return type.

Here is the call graph for this function:

◆ BZWrite()

template<typename T , typename Hash = OurIpv6PrefixHash>
int Dwm::Ipv6PrefixMap< T, Hash >::BZWrite ( BZFILE * bzf) const
inline

Writes the Ipv6PrefixMap to bzf.

Returns the number of bytes written on success, -1 on failure. Be wary; the integer return is risky (could overflow) but it's what bzlib's BZ2_bzWrite() returns and we trickled up the return type.

Here is the call graph for this function:

◆ Clear()

template<typename T , typename Hash = OurIpv6PrefixHash>
void Dwm::Ipv6PrefixMap< T, Hash >::Clear ( std::unique_lock< std::shared_mutex > & lck)
inline

Clears the map.

lck must be a lock created with UniqueLock() and must be locked.

◆ Empty() [1/2]

template<typename T , typename Hash = OurIpv6PrefixHash>
bool Dwm::Ipv6PrefixMap< T, Hash >::Empty ( std::shared_lock< std::shared_mutex > & lck) const
inline

Returns true if the map is empty.

lck must be a lock created with SharedLock() and must be locked.

◆ Empty() [2/2]

template<typename T , typename Hash = OurIpv6PrefixHash>
bool Dwm::Ipv6PrefixMap< T, Hash >::Empty ( std::unique_lock< std::shared_mutex > & lck) const
inline

Returns true if the map is empty.

lck must be a lock created with UniqueLock() and must be locked.

◆ Find() [1/3]

template<typename T , typename Hash = OurIpv6PrefixHash>
bool Dwm::Ipv6PrefixMap< T, Hash >::Find ( const Ipv6Prefix & pfx,
T & value ) const
inline

Find the entry with key pfx.

If found, sets value to the value stored at pfx and returns true. If not found, returns false.

Here is the call graph for this function:

◆ Find() [2/3]

template<typename T , typename Hash = OurIpv6PrefixHash>
bool Dwm::Ipv6PrefixMap< T, Hash >::Find ( std::shared_lock< std::shared_mutex > & lck,
const Ipv6Prefix & pfx,
T & value ) const
inline

Find the entry with key pfx.

If found, sets value to the value stored at pfx and returns true. If not found, returns false. lck must be a lock created with SharedLock() and must be locked.

◆ Find() [3/3]

template<typename T , typename Hash = OurIpv6PrefixHash>
bool Dwm::Ipv6PrefixMap< T, Hash >::Find ( std::unique_lock< std::shared_mutex > & lck,
const Ipv6Prefix & pfx,
T & value ) const
inline

Find the entry with key pfx.

If found, sets value to the value stored at pfx and returns true. If not found, returns false. lck must be a lock created with UniqueLock() and must be locked.

◆ FindLongest() [1/3]

template<typename T , typename Hash = OurIpv6PrefixHash>
bool Dwm::Ipv6PrefixMap< T, Hash >::FindLongest ( const Ipv6Address & addr,
std::pair< Ipv6Prefix, T > & value ) const
inline

Find the longest match for the given IPv6 address addr.

If a match is found, sets value.first to the matching prefix and value.second to the value stored at the matching prefix and returns true. If no match is found, returns false.

Here is the call graph for this function:

◆ FindLongest() [2/3]

template<typename T , typename Hash = OurIpv6PrefixHash>
bool Dwm::Ipv6PrefixMap< T, Hash >::FindLongest ( std::shared_lock< std::shared_mutex > & lck,
const Ipv6Address & addr,
std::pair< Ipv6Prefix, T > & value ) const
inline

Find the longest match for the given IPv6 address addr.

If a match is found, sets value.first to the matching prefix and value.second to the value stored at the matching prefix and returns true. If no match is found, returns false. lck must be a lock created with SharedLock() and must be locked.

◆ FindLongest() [3/3]

template<typename T , typename Hash = OurIpv6PrefixHash>
bool Dwm::Ipv6PrefixMap< T, Hash >::FindLongest ( std::unique_lock< std::shared_mutex > & lck,
const Ipv6Address & addr,
std::pair< Ipv6Prefix, T > & value ) const
inline

Find the longest match for the given IPv6 address addr.

If a match is found, sets value.first to the matching prefix and value.second to the value stored at the matching prefix and returns true. If no match is found, returns false. lck must be a lock created with UniqueLock() and must be locked.

◆ FindMatches() [1/3]

template<typename T , typename Hash = OurIpv6PrefixHash>
bool Dwm::Ipv6PrefixMap< T, Hash >::FindMatches ( const Ipv6Address & addr,
std::vector< std::pair< Ipv6Prefix, T > > & values ) const
inline

Find all matches for the given IPv6 address addr.

If matches are found, they are placed in values, in most-specific (longest prefix length) to least-specific (shortest prefix length) order and true is returned. If no matches are found, returns false.

Here is the call graph for this function:

◆ FindMatches() [2/3]

template<typename T , typename Hash = OurIpv6PrefixHash>
bool Dwm::Ipv6PrefixMap< T, Hash >::FindMatches ( std::shared_lock< std::shared_mutex > & lck,
const Ipv6Address & addr,
std::vector< std::pair< Ipv6Prefix, T > > & values ) const
inline

Find all matches for the given IPv6 address addr.

If matches are found, they are placed in values, in most-specific (longest prefix length) to least-specific (shortest prefix length) order and true is returned. If no matches are found, returns false. lck must be a lock created with SharedLock() and must be locked.

◆ FindMatches() [3/3]

template<typename T , typename Hash = OurIpv6PrefixHash>
bool Dwm::Ipv6PrefixMap< T, Hash >::FindMatches ( std::unique_lock< std::shared_mutex > & lck,
const Ipv6Address & addr,
std::vector< std::pair< Ipv6Prefix, T > > & values ) const
inline

Find all matches for the given IPv6 address addr.

If matches are found, they are placed in values, in most-specific (longest prefix length) to least-specific (shortest prefix length) order and true is returned. If no matches are found, returns false. lck must be a lock created with UniqueLock() and must be locked.

◆ Read() [1/6]

template<typename T , typename Hash = OurIpv6PrefixHash>
bool Dwm::Ipv6PrefixMap< T, Hash >::Read ( boost::asio::generic::stream_protocol::socket & s,
boost::system::error_code & ec )
inline

Reads the Ipv6PrefixMap from s.

Returns true on success, false on failure.

Here is the call graph for this function:

◆ Read() [2/6]

template<typename T , typename Hash = OurIpv6PrefixHash>
bool Dwm::Ipv6PrefixMap< T, Hash >::Read ( boost::asio::ip::tcp::socket & s,
boost::system::error_code & ec )
inline

Reads the Ipv6PrefixMap from s.

Returns true on success, false on failure.

Here is the call graph for this function:

◆ Read() [3/6]

template<typename T , typename Hash = OurIpv6PrefixHash>
bool Dwm::Ipv6PrefixMap< T, Hash >::Read ( boost::asio::local::stream_protocol::socket & s,
boost::system::error_code & ec )
inline

Reads the Ipv6PrefixMap from s.

Returns true on success, false on failure.

Here is the call graph for this function:

◆ Read() [4/6]

template<typename T , typename Hash = OurIpv6PrefixHash>
size_t Dwm::Ipv6PrefixMap< T, Hash >::Read ( FILE * f)
inline

Reads the Ipv6PrefixMap from f.

Returns 1 on success, 0 on failure.

Here is the call graph for this function:

◆ Read() [5/6]

template<typename T , typename Hash = OurIpv6PrefixHash>
int Dwm::Ipv6PrefixMap< T, Hash >::Read ( gzFile gzf)
inline

Reads the Ipv6PrefixMap from gzf.

Returns the number of bytes read on success, -1 on failure. Be wary; the integer return is risky (could overflow) but it's what zlib's gzread() returns and we trickled up the return type.

Here is the call graph for this function:

◆ Read() [6/6]

template<typename T , typename Hash = OurIpv6PrefixHash>
ssize_t Dwm::Ipv6PrefixMap< T, Hash >::Read ( int fd)
inline

Reads the Ipv6PrefixMap from file descriptor fd.

Returns the number of bytes read on success, -1 on failure.

Here is the call graph for this function:

◆ Remove() [1/2]

template<typename T , typename Hash = OurIpv6PrefixHash>
bool Dwm::Ipv6PrefixMap< T, Hash >::Remove ( const Ipv6Prefix & pfx)
inline

Removes the entry for the given prefix pfx.

Returns true if an entry was removed, false if no entry was found for pfx.

Here is the call graph for this function:

◆ Remove() [2/2]

template<typename T , typename Hash = OurIpv6PrefixHash>
bool Dwm::Ipv6PrefixMap< T, Hash >::Remove ( std::unique_lock< std::shared_mutex > & lck,
const Ipv6Prefix & pfx )
inline

Removes the entry for the given prefix pfx.

Returns true if an entry was removed, false if no entry was found for pfx. lck must be a lock created with UniqueLock() and must be locked.

Here is the call graph for this function:

◆ SharedLock()

template<typename T , typename Hash = OurIpv6PrefixHash>
std::shared_lock< std::shared_mutex > Dwm::Ipv6PrefixMap< T, Hash >::SharedLock ( ) const
inline

Returns a shared lock of the Ipv6PrefixMap, in the locked state.

This should be used with care to avoid deadlock. It is intended for scenarios where the caller needs to perform many read-only operations in quick succession and performance is paramount, since it allows one to call the members which accept a shared lock and hence do not lock and unlock on each call. Note that the only read-only member with significant locking overhead is Find(). FindLongest()'s locking overhead is dwarfed by the cycles it needs for other activities.

◆ UniqueLock()

template<typename T , typename Hash = OurIpv6PrefixHash>
std::unique_lock< std::shared_mutex > Dwm::Ipv6PrefixMap< T, Hash >::UniqueLock ( )
inline

Returns a unique lock of the Ipv6PrefixMap, in the locked state.

This (and the members that accept a unique lock as an argument) should be used with care to avoid deadlock. It is intended for scenarios where the caller needs to perform many operations in quick succession and performance is paramount, since it allows one to call the members which accept a unique lock and hence do not lock and unlock on each call. Note that the only read-only member with significant locking overhead is Find(); FindLongest()'s locking overhead is dwarfed by the cycles it needs for other activities. Add() is a little bit faster for repetitive operations using the pre-locked version (about 5%).

◆ Write() [1/6]

template<typename T , typename Hash = OurIpv6PrefixHash>
bool Dwm::Ipv6PrefixMap< T, Hash >::Write ( boost::asio::generic::stream_protocol::socket & s,
boost::system::error_code & ec ) const
inline

Writes the Ipv6PrefixMap to s.

Returns true on success, false on failure.

Here is the call graph for this function:

◆ Write() [2/6]

template<typename T , typename Hash = OurIpv6PrefixHash>
bool Dwm::Ipv6PrefixMap< T, Hash >::Write ( boost::asio::ip::tcp::socket & s,
boost::system::error_code & ec ) const
inline

Writes the Ipv6PrefixMap to s.

Returns true on success, false on failure.

Here is the call graph for this function:

◆ Write() [3/6]

template<typename T , typename Hash = OurIpv6PrefixHash>
bool Dwm::Ipv6PrefixMap< T, Hash >::Write ( boost::asio::local::stream_protocol::socket & s,
boost::system::error_code & ec ) const
inline

Writes the Ipv6PrefixMap to s.

Returns true on success, false on failure.

Here is the call graph for this function:

◆ Write() [4/6]

template<typename T , typename Hash = OurIpv6PrefixHash>
size_t Dwm::Ipv6PrefixMap< T, Hash >::Write ( FILE * f) const
inline

Writes the Ipv6PrefixMap to f.

Returns 1 on success, 0 on failure.

Here is the call graph for this function:

◆ Write() [5/6]

template<typename T , typename Hash = OurIpv6PrefixHash>
int Dwm::Ipv6PrefixMap< T, Hash >::Write ( gzFile gzf) const
inline

Writes the Ipv6PrefixMap to gzf.

Returns the number of bytes written on success, -1 on failure. Be wary; the integer return is risky (could overflow) but it's what zlib's gzread() returns and we trickled up the return type.

Here is the call graph for this function:

◆ Write() [6/6]

template<typename T , typename Hash = OurIpv6PrefixHash>
ssize_t Dwm::Ipv6PrefixMap< T, Hash >::Write ( int fd) const
inline

Writes the Ipv6PrefixMap to file descriptor fd.

Returns the number of bytes written on success, -1 on failure.

Here is the call graph for this function:

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