From 376455169208576167
X-Google-Thread: f78e5,b262f0559808d68d
X-Google-Attributes: gidf78e5,public
X-Google-Language: ENGLISH,ASCII-7-bit
Path: g2news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local02.nntp.dca.giganews.com!nntp.speakeasy.net!news.speakeasy.net.POSTED!not-for-mail
NNTP-Posting-Date: Thu, 14 Sep 2006 16:40:24 -0500
Return-Path: <devnull@stump.algebra.com>
X-Authentication-Warning: mulga.csse.unimelb.edu.au: fjh set sender to devnull@stump.algebra.com using -f
X-Robomod: STUMP, ichudov@algebra.com (Igor Chudov)
X-Original-To: std-c++@mailman.ucar.edu
Delivered-To: std-c++@mailman.ucar.edu
From: derek@antiquark.com
Newsgroups: comp.std.c++
Subject: Re: typed_index_wrapper
Organization: http://groups.google.com
Message-ID: <1158268062.498188.92720@d34g2000cwd.googlegroups.com>
References: <1158187421.369321.48370@m73g2000cwd.googlegroups.com>
   <memo.20060914192701.2844B@brangdon.cix.compulink.co.uk>
Mime-Version: 1.0
Content-Type: text/plain; charset="iso-8859-1"
X-Complaints-To: groups-abuse@google.com
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.12) Gecko/20050922 Fedora/1.7.12-1.3.1,gzip(gfe),gzip(gfe)
Complaints-To: groups-abuse@google.com
Injection-Info: d34g2000cwd.googlegroups.com; posting-host=206.45.72.49;
   posting-account=7FQ00RMAAAB94nG8vVqxnuNVHkXuKSF_JQPy3MdXsIgAWnuyOJmNcQ
X-Virus-Scanned: amavisd-new at ucar.edu
X-Virus-Scanned: amavisd-new at csse.unimelb.edu.au
Approved: Fergus Henderson <fjh@cs.mu.oz.au>, moderator of comp.std.c++
X-Virus-Scanned: amavisd-new at csse.unimelb.edu.au
Date: Thu, 14 Sep 2006 16:34:46 CST
Lines: 164
NNTP-Posting-Host: 65.182.171.162
X-Trace: sv3-jmjTHrxbCx1XEJGF+i8nQHK7nCGY+eiXm4AbX4PwVGLPxteQSJBAfA6qdWkFsf48AVNxjmqjIeRc8yV!VaYOe0Bdg8p7LPsEXbYXHrmC3tuoou+Pnj7sGyViVTFhmJQe1c0M7d9U2G0ng2HxafM+pFZjBrI3!w+/82b6vZPd4HSe8Okvn1ziVw8CCqyacDhwaRne0VA==
X-Complaints-To: abuse@speakeasy.net
X-DMCA-Complaints-To: abuse@speakeasy.net
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.32
Xref: g2news2.google.com comp.std.c++:3790

Dave Harris wrote:
> Ideally there would only be one wrapper, not a set of them. As with
> std::stack.

Here's an attempt at a wrapper. I think that to be done properly,
various wrappers would be needed for the various containers, mainly due
to difference in constructors.

The wrapper class takes two parameters: the container type, and the
indexing type.
EG, typed_index_wrapper < vector<int>, EFoo > myvec.

The code below shows how this adapter can be used for std::vector,
std::deque and boost::array. The boost::array causes a compiler error
for aggregate initialization though.

Other containers, such as bitset, valarray, stack and string, are
problematic for reasons mentioned near the bottom.

Derek

=====

#include <iostream>
#include <vector>
#include <deque>
#include <string>
#include <deque>

#include <boost/array.hpp>

template<typename Container, typename IndexType>
class typed_index_wrapper : public Container
{
public:
	typedef typename Container::reference reference;
	typedef typename Container::const_reference const_reference;
	typedef IndexType index_type;
	typedef typename Container::size_type size_type;
	typedef typename Container::value_type value_type;
	typedef Container base_type;

	reference operator[](const index_type& index)
	{
		return Container::operator[](size_type(index));
	}

	const_reference operator[](const index_type& index) const
	{
		return Container::operator[](size_type(index));
	}

	reference at(const index_type& index)
	{
		return Container::at(size_type(index));
	}

	const_reference at(const index_type& index) const
	{
		return Container::at(size_type(index));
	}

	typed_index_wrapper()
	:Container()
	{}

	typed_index_wrapper(size_t n)
	:Container(n)
	{}

	typed_index_wrapper(size_t n, value_type v)
	:Container(n, v)
	{}

	template<class InIt>
	typed_index_wrapper(InIt _begin, InIt _end)
	:Container(_begin, _end)
	{}
};



enum EFoo { a = 0, b, c };
enum EBar { x = 0, y, z };

#define PRINT(a) std::cout << #a << ": " << (a) << std::endl

int main()
{
	std::string s("hello");

	// The following containers are demonstrated below:
	//  std::vector
	//  boost::array
	//  std::deque

	typed_index_wrapper < std::vector<int>, EFoo > v1(11);
	typed_index_wrapper < std::vector<int>, EFoo > v2(22, 33);
	typed_index_wrapper < std::vector<int>, EFoo > v3(v1);
	typed_index_wrapper < std::vector<int>, EFoo > v4(s.begin(), s.end());

	PRINT(v1.size());
	PRINT(v2.size());
	PRINT(v3.size());

	std::cout << std::endl;

	PRINT(v1.at(a));
	PRINT(v2[a]);
	PRINT(v3[a]);
	PRINT(v4.at(a));

	std::cout << std::endl;

	PRINT( v1 == v2 );
	PRINT( v1 == v3 );

	v1 = v4;

	std::cout << std::endl;

	typed_index_wrapper< boost::array<int, 25>, EBar > a1;
	// a1[0] = 111; // compiler error
	// a1[a] = 111; // compiler error
	a1[x] = 111;
	a1.at(x) = 111;

	PRINT(a1.size());
	PRINT(a1.at(EBar(0)));
	PRINT(a1.at(x));

	// Aggregate initialize won't work for boost::array. Message is
	// error: `a2' must be initialized by constructor, not by `{...}
	//typed_index_wrapper< boost::array<int, 25>, EFoo > a2 =
{444,555,666};

	typed_index_wrapper< std::deque<int>, unsigned char> d1(1000, 0);
	d1.clear();
	for(int i = 0 ; i < 1000 ; i ++ )d1.push_back(i);
	PRINT(d1.at(257)); // index will roll over because it's uchar, should
print '1'

	// std::bitset will cause problems due to different ctors, and member
functions
	// returning a bitset<Bits>.
	//
	// std::valarray will cause problems due to different ctors, and
indexing functions
	// that take a slice or gslice.
	//
	// std::stack doesn't provide indexed access functions.
	//
	// std::string has different ctors, and just doesn't seem applicable
to typed indexing.
	
	
}

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]



