473,785 Members | 2,435 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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=(cons t 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(s ize_type pos_, SharedPtr<conta iner_type> &cont_) :
pos(pos_), cont(cont_) { }

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

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

public:
FooReference(Re ferenceBase<Foo >::size_type pos,
SharedPtr<Refer enceBase<Foo>:: container_type> &cont) :
ReferenceBase<F oo>(pos, cont) { }

// public methods of Foo
void set(int a) { access().set(a) ; }
int get() const { return access().get(); }
FooReference &operator=(cons t 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<Refer enceBase<T>::co ntainer_type> &cont) :
ReferenceBase<T >(pos, cont) { }

BasicReference &operator=(cons t 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<Fo o, 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.
Jul 22 '05 #1
0 1596

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
5974
by: Jason Heyes | last post by:
To my understanding, std::vector does not use reference counting to avoid the overhead of copying and initialisation. Where can I get a reference counted implementation of std::vector? Thanks.
18
2879
by: Janina Kramer | last post by:
hi ng, i'm working on a multiplayer game for a variable number of players and on the client side, i'm using a std::vector<CPlayer> to store informatik about the players. CPlayer is a class that contains another std::vector<CPosition>. Because one of the players is the client itself (and the size of the vector<CPlayer> doesn't change during a game), i thought i could store a std::vector<CPlayer>::iterator "localplayer" that points to the...
5
1790
by: Eric Lilja | last post by:
Hello, consider this complete program (sorry, it's not minimal but I hope it's readable at least): #include <algorithm> #include <iostream> #include <vector> class Row { public:
20
17835
by: Anonymous | last post by:
Is there a non-brute force method of doing this? transform() looked likely but had no predefined function object. std::vector<double> src; std::vector<int> dest; std::vector<double>::size_type size = src.size(); dest.reserve(size); for (std::vector<int>::size_type i = 0;
17
3361
by: Michael Hopkins | last post by:
Hi all I want to create a std::vector that goes from 1 to n instead of 0 to n-1. The only change this will have is in loops and when the vector returns positions of elements etc. I am calling this uovec at the moment (for Unit-Offset VECtor). I want the class to respond correctly to all usage of STL containers and algorithms so that it is a transparent replacement for std:vector. The options seems to be:
32
69698
by: zl2k | last post by:
hi, c++ user Suppose I constructed a large array and put it in the std::vector in a function and now I want to return it back to where the function is called. I can do like this: std::vector<int> fun(){ //build the vector v; return v; }
9
8897
by: aaragon | last post by:
I am trying to create a vector of type T and everything goes fine until I try to iterate over it. For some reason, the compiler gives me an error when I declare std::vector<T>::iterator iter; Any ideas why is tihs happening? The code is as follows: template <class T> struct StdVectorStorage { std::vector<T>* _storage;
13
2963
by: jubelbrus | last post by:
Hi I'm trying to do the following. #include <vector> #include <boost/thread/mutex.hpp> #include <boost/shared_ptr.hpp> #include <boost/tuple/tuple.hpp> class {
0
855
by: Utku Altinkaya | last post by:
Hello, I am wrapping the reference container vector as document suggests. typedef std::vector<CUEEntity*EntityContainer; class_<CUEEntity>("Entity", init<CUEEntity*>()); class_<EntityContainer>("EntityContainer") .def(vector_indexing_suite<EntityContainer, true>() )
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10091
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6739
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.