|
Boost : |
From: Powell, Gary (powellg_at_[hidden])
Date: 2004-02-11 12:34:36
Hello, I was reviewing the View Template Library (VTL) - see http://www.zib.de/weiser/vtl/ ... and have a few questions regarding Views
as applied to STL Maps.
I was also wondering if Boost was planning to eventually provide a VTL-like framework?
----------------------------------
Gary > Yes, I'm planning on using the new Iterator Adaptors and converting the VTL library to use boost.
---------------------------
I'm looking for a type of View that provides read-only access to an STL map, with only const iterators, but also provides limited insertion and removal
access as well (as shown below):
This new limited View-like map will only allow clients to iterate using const iterators, and also provides only one public insert(const key&) and
one erase(const key&) method.
These insert and erase member functions need to be implemented as "Template Method Functions", as follows: (see Design Patterns)
* Note: these are shown using the latest SGI STL implementation (i.e., the
_M_t.insert_unique and _M_t.erase calls)
virtual bool preInsert(const value_type&)
{
return true;
}
pair<iterator, bool> insert(const value_type& x)
{
if(preInsert(x) {
return _M_t.insert_unique(x);
}
return make_pair(end(), false); // make compiler happy
}
virtual bool preErase(const key_type&)
{
return true;
}
size_type erase(const key_type& x)
{
if(preErase(x) {
return _M_t.erase(x);
}
return 0; // make compiler happy
}
The preInsert and preErase methods are virtual Hook Methods, and derived classes override these to add behavior to the (non-virtual) insert and erase
functions..
Another nice addition to this map view would be a const _Tp& operator[](const key_type& k) const function, implemented something like this:
const _Tp& operator[](const key_type& k) const
{
const_iterator i = lower_bound(k);
if(i == end() || key_comp(k, (*i).first)) // if i->first is >= k
throw SearchError(k);
return (*i).second;
}
* Note: this simple implementation assumes that the key can be converted to a string in the constructor of the SearchError exception object.
I have at least three Manager-like (Mediator) classes that can use this pattern (i.e., derived from this Map View class) as well as others that need
a vector view with a similar filtered design...
----------------------------------------------
Gary > If you are asking me to write this code, well I'm not likely to get around to it. However I'd be glad to review it as an addition to the library. We generally stayed with the STL model of no virtual functions. In the meantime you might like to look at the Iterator Adaptor library and generate your own special view container.
-Gary-
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk