 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Rusty Guest
|
Posted: Tue May 16, 2006 11:21 am Post subject: Synchronised STL Iterators |
|
|
Hi,
I needed synchronised iterators on STL containers. Unable to find some
standard or popular implementation, I have written something myself.
It would be great if it can be reviewed and any mistakes pointed out
.
Thanks,
Regards,
Aseem.
/**
* This class attempts to provide synchronised STL like iterators.
* It assumes following inputs and behaviours:
*
* 1. All operations on the container will be done under some mutex.
* 2. Mutex class will provide Lock datatype type whose constructor
will acquire
* mutex and destructor will release mutex.
*
* For more clarity see the usage of lock in the Iterator class.
* 3. If the object pointed by the iterator at present has been removed
from
* container, it is assumed that some boolean predicate will be
provided as
* input which will take object as input and return a boolean
* which indicats that object has been removed (false) or not
(true).
* 4. A dereference functor will be provided which will return object
given iterator
* of type container.
* 5. Default Dereference and IsValid are given below.
*/
#ifndef _SYNC_STL_ITERATORS_H
#define _SYNC_STL_ITERATORS_H
#include<iterator>
#include<lib/Mutex.h>
template<class T>
class IsValid
{
public:
bool operator() (T t)
{
return (t->isValid ());
}
};
template<class Container, class T>
class MapValue
{
public:
typedef typename Container::iterator Iterator;
typedef typename Container::value_type ValueType;
T operator() (Iterator it)
{
return it->second;
}
T operator() (const ValueType& value)
{
return value.second;
}
};
/*
* we have to redeclare iterator struct here because:
*
* 1. In host platform, this struct is always defined.
* 2. In target platform, this struct is defined only under flag
__STL_USE_NAMESPACES.
*
* .h files in question are :
*
* For host : /usr/include/g++-3/stl_iterator.h
* For target : /opt/eldk/ppc_8xx/usr/include/g++-3/stl_iterator.h
*
* therefore we have to define this struct if platform is target and
__STL_USE_NAMESPACES
* is not defined.
*/
#if !defined(EMULATOR) && !defined(__STL_USE_NAMESPACES)
template <class _Category, class _Tp, class _Distance = ptrdiff_t,
class _Pointer = _Tp*, class _Reference = _Tp&>
struct iterator {
typedef _Category iterator_category;
typedef _Tp value_type;
typedef _Distance difference_type;
typedef _Pointer pointer;
typedef _Reference reference;
};
#endif
template<class T, class Container, class Dereference =
MapValue<Container, T>,
class MutexClass = Mutex, class Predicate = IsValid<T> >
class iteratorT : public iterator<forward_iterator_tag, T>
{
private:
typedef typename Container::iterator mIter;
typedef typename MutexClass::Lock Lock;
mIter position;
Container& mInstances;
MutexClass& mMutex;
T object;
Predicate mPred;
Dereference mDereference;
inline void setNull ()
{
object = T (NULL);
}
inline bool isEnd ()
{
return (position == mInstances.end ());
}
inline void increment ()
{
if ((object) && mPred (object)) {
init (++position);
} else {
init (mInstances.end ());
}
}
inline void init (const mIter& it)
{
position = it;
if (isEnd ()) {
setNull ();
} else {
object = mDereference (position);
}
}
public:
typedef const T ReturnType;
typedef iteratorT<T, Container, Dereference, MutexClass,
Predicate> SelfType;
iteratorT (const mIter& it,
Container& instances,
MutexClass& m)
: mInstances (instances), mMutex (m),
mPred (IsValid<T> ()), mDereference (MapValue<Container,
T> ())
{
init (it);
}
iteratorT (const mIter& it, Container& instances, Mutex& m,
Predicate p,
Dereference dereference)
: mInstances (instances), mMutex (m),
mPred (p), mDereference (dereference)
{
init (it);
}
SelfType& operator= (const SelfType& other)
{
init (other.position);
}
bool operator== (const SelfType& other) const
{
return (object == other.object);
}
ReturnType operator*() const { return object; }
SelfType& operator++()
{
{
Lock lock (mMutex);
increment ();
}
return *this;
};
SelfType operator++(int)
{
iteratorT temp = *this;
{
Lock lock (mMutex);
increment ();
}
return temp;
}
};
#endif //_SYNC_STL_ITERATORS_H
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
David Abrahams Guest
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|