Marine Library  1.0
C++ library for Linux Networking Development
Public Types | Public Member Functions | List of all members
CFdDataMap< T, LockT > Class Template Reference

Thread-safe hash table for fd (file descriptor) related data. More...

#include <fd_data_map.hh>

Public Types

typedef T value_type
 
typedef CSharedPtr< value_type > pointer
 

Public Member Functions

 CFdDataMap (size_t capacity=100)
 Initialize this object. More...
 
size_t size () const
 Get number of key/value pairs. More...
 
size_t capacity () const
 Get current capacity. More...
 
void capacity (size_t c)
 Set current capacity. More...
 
void setData (int fd, const pointer &data, pointer *old=NULL)
 Set value for an fd. More...
 
pointer getData (int fd) const
 Get value for an fd. More...
 
void getData (int fd, pointer *data) const
 Get value for an fd. More...
 
template<class ForwardIter , class OutputIter >
void getData (ForwardIter first, ForwardIter last, OutputIter dstFirst) const
 Get values for some file descriptors. More...
 
void clearData (int fd, pointer *old=NULL)
 Remove value for an fd. More...
 
template<class ForwardIter >
void clearData (ForwardIter first, ForwardIter last)
 Remove values for some file descriptors. More...
 
template<class ForwardIter , class OutputIter >
void clearData (ForwardIter first, ForwardIter last, OutputIter dstFirst)
 Remove values for some file descriptors. More...
 
void clear ()
 Clear all key/value pairs.
 

Detailed Description

template<class T, class LockT = CSpinLock>
class CFdDataMap< T, LockT >

Thread-safe hash table for fd (file descriptor) related data.

Key MUST be file descriptors, which are non-negative and of type int. In fact the file descriptor acts as an index to an underlying vector of values. Because the number of files a process can open is limited, e.g. 1024 (it is modifiable), there's no chance for the underlying vector to grow unexpectedly. So do NOT use CFdDataMap as a generic int to value hash table.
Value can be of any type, even non-copyable types. Usually you want to keep a session for each socket fd, and the session is non-copyable. CFdDataMap uses smart pointer to hold a value, there are several advantages. Firstly, it avoids copying issue as mentioned; secondly, it simplifies lifetime control for each value among multiple threads; And finally, it is efficient for the underlying vector to expand.
CFdDataMap is thread safe and implemented on top of CFdMap. You can specify any lock type you want, as long as it cooperates with CGuard.

Template Parameters
TValue type
LockTLock type, default to CSpinLock
See also
CFdMap

Constructor & Destructor Documentation

template<class T , class LockT = CSpinLock>
CFdDataMap< T, LockT >::CFdDataMap ( size_t  capacity = 100)
inlineexplicit

Initialize this object.

capacity is used as a hint to the number of key/value pairs this object wants to hold. But CFdDataMap is free to expand as needed.

Parameters
capacityInitial reserved room for key/value pairs

Member Function Documentation

template<class T , class LockT = CSpinLock>
size_t CFdDataMap< T, LockT >::capacity ( ) const
inline

Get current capacity.

Returns
Current capacity
template<class T , class LockT = CSpinLock>
void CFdDataMap< T, LockT >::capacity ( size_t  c)
inline

Set current capacity.

Parameters
cNew capacity
template<class T , class LockT = CSpinLock>
void CFdDataMap< T, LockT >::clearData ( int  fd,
pointer old = NULL 
)
inline

Remove value for an fd.

Parameters
[in]fdA file descriptor
[out]oldA smart pointer object to obtain old value for fd, or NULL if not interested
template<class T , class LockT = CSpinLock>
template<class ForwardIter >
void CFdDataMap< T, LockT >::clearData ( ForwardIter  first,
ForwardIter  last 
)
inline

Remove values for some file descriptors.

Parameters
[in]firstBegin iterator for a collection of file descriptors
[in]lastEnd iterator for a collection of file descriptors
template<class T , class LockT = CSpinLock>
template<class ForwardIter , class OutputIter >
void CFdDataMap< T, LockT >::clearData ( ForwardIter  first,
ForwardIter  last,
OutputIter  dstFirst 
)
inline

Remove values for some file descriptors.

Parameters
[in]firstBegin iterator for a collection of file descriptors
[in]lastEnd iterator for a collection of file descriptors
[out]dstFirstBegin iterator for a collection of smart pointers to receive old values
Note
The size and validation of destination collection is guaranteed by the user.
template<class T , class LockT = CSpinLock>
pointer CFdDataMap< T, LockT >::getData ( int  fd) const
inline

Get value for an fd.

Parameters
fdA file descriptor
Returns
A smart pointer object hold the value for fd
template<class T , class LockT = CSpinLock>
void CFdDataMap< T, LockT >::getData ( int  fd,
pointer data 
) const
inline

Get value for an fd.

Parameters
[in]fdA file descriptor
[out]dataA smart pointer object to obtain the value for fd
template<class T , class LockT = CSpinLock>
template<class ForwardIter , class OutputIter >
void CFdDataMap< T, LockT >::getData ( ForwardIter  first,
ForwardIter  last,
OutputIter  dstFirst 
) const
inline

Get values for some file descriptors.

This function saves many lock/unlock operations.
Example code:

vector<int> fds;
vector<CFdDataMap<Data>::pointer> values(fds.size());
fdmap.getData(fds.begin(), fds.end(), values.begin());
Parameters
[in]firstBegin iterator for a collection of file descriptors
[in]lastEnd iterator for a collection of file descriptors
[out]dstFirstBegin iterator for a collection of smart pointers to receive the values of file descriptors
Note
The size and validation of destination collection is guaranteed by the user.
template<class T, class LockT = CSpinLock>
void CFdDataMap< T, LockT >::setData ( int  fd,
const pointer data,
pointer old = NULL 
)
inline

Set value for an fd.

Parameters
[in]fdA file descriptor
[in]dataNew value for fd
[out]oldA smart pointer object to obtain old value for fd
template<class T, class LockT = CSpinLock>
size_t CFdDataMap< T, LockT >::size ( ) const
inline

Get number of key/value pairs.

Returns
Number of key/value pairs

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