|
| 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.
|
|
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
-
T | Value type |
LockT | Lock type, default to CSpinLock |
- See also
- CFdMap
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] | first | Begin iterator for a collection of file descriptors |
[in] | last | End iterator for a collection of file descriptors |
[out] | dstFirst | Begin 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.