I have a container of smart pointers to mutable objects. I have to write two for_each loops, one for accessing the objects as read-only data and another for mutable data. The compiler is telling me that std::vector< boost::shared_ptr<Object> > is not the same as std::vector< boost::shared_ptr<const Object> >, note the const.
Here is my example code:
#include <vector>
#include "boost/shared_ptr.hpp"
#include <iterator>
class Field_Interface
{ ; };
typedef boost::shared_ptr<Field_Interface> Ptr_Field_Interface;
typedef boost::shared_ptr<const Field_Interface> Ptr_Const_Field_Interface;
struct Field_Iterator
: std::input_iterator<std::forward_iterator_tag, Ptr_Field_Interface>
{
// forward iterator methods & operators...
};
struct Const_Field_Iterator
: std::input_iterator<std::forward_iterator_tag, Ptr_Const_Field_Interface>
{
// forward iterator methods & operators...
};
struct Field_Functor
{
virtual void operator()(const Ptr_Field_Interface&) = 0;
virtual void operator()(const Ptr_Const_Field_Interface&) = 0;
};
class Record;
typedef boost::shared_ptr<Record> Ptr_Record;
typedef boost::shared_ptr<const Record> Ptr_Const_Record;
class Record_Base
{
protected:
virtual Field_Iterator beginning_field(void) = 0;
virtual Field_Iterator ending_field(void) = 0;
virtual Const_Field_Iterator const_beginning_field(void) = 0;
virtual Const_Field_Iterator const_ending_field(void) = 0;
void for_each(Field_Functor * p_functor)
{
Field_Iterator iter_begin(beginning_field());
Field_Iterator iter_end(ending_field());
for (; iter_begin != iter_end; ++ iter_begin)
{
(*p_functor)(*iter_begin);
}
}
};
class Record_Derived
{
public:
typedef std::vector<Ptr_Field_Interface> Field_Container;
typedef std::vector<Ptr_Record> Record_Container;
private:
Field_Container m_fields;
Record_Container m_subrecords;
};
Given all the above details, how do I implement the pure abstract methods of Record_Base in Record_Derived?
I’ve tried:
- returning
m_fields.begin(), which
returns conversion errors (can’t
convertstd::vector<...> to)
Field_Iterator - returning
&m_fields[0], which is
dangerous, because it assumes stuff
about the internals ofstd::vector.
BTW, I’m not using std::for_each because I have to iterate over a container of fields and a container of sub-records.
What you’re doing resembles both the Composite and Visitor patterns. Those two patterns mesh well together, so it seems you are on the right track.
To implement the composite pattern, assign the following roles (refer to Composite pattern UML diagram):
FieldRecordFieldandRecord(can’t think of a good name)Component operations that are called on Composite types, are passed on recursively to all children (Leaves and other nested Composite types).
To implement the Visitor pattern, overload
operator()in your functor classes for each Component subtype (Field and Record).I recommend that you get a copy of the Design Patterns book by the “Gang of Four”, which explains these concepts better and goes into much more detail than I possibly can.
Here is some sample code to whet your appetite:
In Record, you may want to provide methods for manipulating/accessing children, instead of returning a reference to the underlying children vector.