Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Making a shared container that behaves like std::vector.

Reply
Thread Tools

Making a shared container that behaves like std::vector.

 
 
Jason Heyes
Guest
Posts: n/a
 
      12-19-2003
I wrote a previous post that asked whether there was a reference-counted
implementation of std::vector. Apparantly there wasn't. So my next question
is, is it possible to write your own shared container that behaves like
std::vector?

Here is me trying to answer that question:

class FooReference;


// user class
class Foo
{
int a;
public:
Foo() : a(3) { }
int get() const { return a; }
void set(int a_) { a = a_; }

// reference support
Foo(const FooReference &ref) : a(ref.access().a) { }
Foo &operator=(const FooReference &ref) { a = ref.access().a; }
};

// common to all references
template <class T>
class ReferenceBase
{
SharedPtr<std::vector<T> > &cont;
typename std::vector<T>::size_type pos;

protected:
T access() { cont.unshare(); return (*cont)[pos]; }
const T &access() const { return (*cont)[pos]; }

public:
typedef std::vector<T> container_type;
typedef typename container_type::size_type size_type;

ReferenceBase(size_type pos_, SharedPtr<container_type> &cont_) :
pos(pos_), cont(cont_) { }

ReferenceBase &operator=(const ReferenceBase &ref)
{ access() = ref.access(); return *this; }
};

// custom user-made reference
class FooReference : public ReferenceBase<Foo>
{
friend class Foo;

public:
FooReference(ReferenceBase<Foo>::size_type pos,
SharedPtr<ReferenceBase<Foo>::container_type> &cont) :
ReferenceBase<Foo>(pos, cont) { }

// public methods of Foo
void set(int a) { access().set(a); }
int get() const { return access().get(); }
FooReference &operator=(const Foo &rvalue)
{ access() = rvalue; return *this; }
};

// reference for basic types like int,char,etc
template <typename T>
class BasicReference : public ReferenceBase<T>
{
public:
BasicReference(ReferenceBase<T>::size_type pos,
SharedPtr<ReferenceBase<T>::container_type> &cont) :
ReferenceBase<T>(pos, cont) { }

BasicReference &operator=(const T &rvalue)
{ access() = rvalue; return *this; }
friend T &operator=(T &lvalue, const BasicReference &ref)
{ lvalue = ref.access(); return lvalue; }
};

// note the special template parameter
template <class T, class R = BasicReference<T> >
class SharedVector
{
SharedPtr<std::vector<T> > ptr;
public:
// typedefs same as std::vector except for these two
typedef R reference;
typedef const R const_reference;

// public interface same as std::vector, forwarding to ptr
// except for copy constructor, operator= and destructor
// which are compiler-generated ones

// only explicit mutator methods unshare ptr
};


// for convenience
typedef SharedVector<Foo, FooReference> FooVector;


As you can see, my shared container class will return reference objects that
act like normal C++ references (only that operator& is not defined). I have
yet to write an iterator for my shared container because I don't know how to
do it yet. But what do you guys think of what I have done so far? Thanks.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
left shift operator behaves like left rotate when the operand is a variable. pc C Programming 2 06-08-2011 06:58 PM
Re: Help: Tamron SP 90mm F2.8 behaves like a variable aperture zoom Andy Digital Photography 7 04-26-2007 11:01 AM
how can i make functions which behaves like printf Kaush C Programming 5 02-02-2004 10:54 AM
Re: Help: Tamron SP 90mm F2.8 behaves like a variable aperture zoom Dave Martindale Digital Photography 1 08-27-2003 12:26 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57