473,799 Members | 3,149 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

vector-like wrapper for IList

Hi all,
I'm trying to implement a std::vector-like wrapper for IList.
The hard part seems to be operator[], because it returns an unmanaged
reference. Probably I have to use pin_ptr to achieve this, but I don't
know how.
Can anybody help?

Thanks,
Christian
template <typename T>
class IListWrapper {
public:
IListWrapper(IL ist<T>^ _list) : list(_list) { }
T& operator[](unsigned i) { return list->default[i]; }
const T& operator[](unsigned i) { return list->default[i]; }
unsigned size() const { return list->Count; }
void push_back(const ItemType& item) { list->Add(item); }
protected:
gcroot<IList<It emType>^list;
};

void main() {
IListWrapper<do ublelist(gcnew List<double>(10 ));
list[3] = 7.5;
}
Jul 9 '07 #1
4 1703
Christian Schmidt wrote:
Hi all,
I'm trying to implement a std::vector-like wrapper for IList.
The hard part seems to be operator[], because it returns an unmanaged
reference. Probably I have to use pin_ptr to achieve this, but I don't
know how.
Can anybody help?
You can't do it like that. pin_ptr by design must be a local
(stack-allocated) variable, which means that any pin_ptr your operator[]
declares will go out of scope, unpinning the value before the operator
returns.

Unfortunately, I think you're probably going to have to return some kind of
proxy object (a la vector<bool>) to simulate the std::vector interface on an
IList<T>. The proxy object would have an operator T() and an
operator=(const T&) so it can stand-in for a 'T' in many contexts - but not
all. The proxy object would hold onto a T^ and perform the native <-->
managed conversions in the two conversion functions.

-cd
Jul 9 '07 #2
Hi Carl,
thanks for your answer.

Carl Daniel [VC++ MVP] wrote:
>I'm trying to implement a std::vector-like wrapper for IList.
The hard part seems to be operator[], because it returns an unmanaged
reference. Probably I have to use pin_ptr to achieve this, but I don't
know how.
Can anybody help?

You can't do it like that. pin_ptr by design must be a local
(stack-allocated) variable, which means that any pin_ptr your operator[]
declares will go out of scope, unpinning the value before the operator
returns.
So I would need to pin the list in the constructor. Is this a problem?
How would I proceed?
Unfortunately, I think you're probably going to have to return some kind of
proxy object (a la vector<bool>) to simulate the std::vector interface on an
IList<T>. The proxy object would have an operator T() and an
operator=(const T&) so it can stand-in for a 'T' in many contexts - but not
all. The proxy object would hold onto a T^ and perform the native <-->
managed conversions in the two conversion functions.
Will this work for value types in the IList, too? Or do I have to use
the setter of the IList?
Is this a correct implementation of the Proxy?

template <typename T>
class ReferenceWrappe r {
public:
ReferenceWrappe r(T^ _me) : me (_me) { }
ReferenceWrappe r<Toperator=(co nst T& other) {
me = other;
return *this;
}
operator T() { return *(T^) me; }
protected:
gcroot<T^me;
};

Jul 9 '07 #3
Christian Schmidt wrote:
Hi Carl,
thanks for your answer.

Carl Daniel [VC++ MVP] wrote:
>>I'm trying to implement a std::vector-like wrapper for IList.
The hard part seems to be operator[], because it returns an
unmanaged reference. Probably I have to use pin_ptr to achieve
this, but I don't know how.
Can anybody help?

You can't do it like that. pin_ptr by design must be a local
(stack-allocated) variable, which means that any pin_ptr your
operator[] declares will go out of scope, unpinning the value before
the operator returns.

So I would need to pin the list in the constructor. Is this a problem?
How would I proceed?
No, you can't have a pin_ptr as a member variable. You have to pin it,
reference it, and un-pin it in each invocation of operator T() or operator=
(const T&).
>
>Unfortunatel y, I think you're probably going to have to return some
kind of proxy object (a la vector<bool>) to simulate the std::vector
interface on an IList<T>. The proxy object would have an operator
T() and an operator=(const T&) so it can stand-in for a 'T' in many
contexts - but not all. The proxy object would hold onto a T^ and
perform the native <--managed conversions in the two conversion
functions.

Will this work for value types in the IList, too? Or do I have to use
the setter of the IList?
Is this a correct implementation of the Proxy?
Something like that, yes.

-cd
Jul 10 '07 #4

"Christian Schmidt" <no**@locom.dew rote in message
news:uy******** ******@TK2MSFTN GP03.phx.gbl...
Hi Carl,
thanks for your answer.

Carl Daniel [VC++ MVP] wrote:
>>I'm trying to implement a std::vector-like wrapper for IList.
The hard part seems to be operator[], because it returns an unmanaged
reference. Probably I have to use pin_ptr to achieve this, but I don't
know how.
Can anybody help?

You can't do it like that. pin_ptr by design must be a local
(stack-allocated) variable, which means that any pin_ptr your operator[]
declares will go out of scope, unpinning the value before the operator
returns.

So I would need to pin the list in the constructor. Is this a problem? How
would I proceed?
>Unfortunatel y, I think you're probably going to have to return some kind
of proxy object (a la vector<bool>) to simulate the std::vector interface
on an IList<T>. The proxy object would have an operator T() and an
operator=(cons t T&) so it can stand-in for a 'T' in many contexts - but
not all. The proxy object would hold onto a T^ and perform the native
<--managed conversions in the two conversion functions.

Will this work for value types in the IList, too? Or do I have to use the
setter of the IList?
To implement operator[] assignment, yes, you'll need to use the IList
setter.

Either your caller needs to be managed type aware, in which case you don't
need pinning pointers at all, or you have a managed collection of primitive
types. Can you fill in some of the blanks here?

Maybe all you are looking for is the T^% tracking reference syntax.
>

Is this a correct implementation of the Proxy?

template <typename T>
class ReferenceWrappe r {
public:
ReferenceWrappe r(T^ _me) : me (_me) { }
ReferenceWrappe r<Toperator=(co nst T& other) {
It's customary to return a reference.
me = other;
Need to dereference me here, ala (*me = other), or else reassign me to a new
valid handle, which other isn't.
return *this;
}
operator T() { return *(T^) me; }
The compiler should choke on that if T is a class type. If it isn't, why
are you boxing it (T^ to a value type incurs boxing).
protected:
gcroot<T^me;
};

Look up interior_ptr<T> .
Jul 17 '07 #5

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

Similar topics

7
10629
by: Forecast | last post by:
I run the following code in UNIX compiled by g++ 3.3.2 successfully. : // proj2.cc: returns a dynamic vector and prints out at main~~ : // : #include <iostream> : #include <vector> : : using namespace std; : : vector<string>* getTyphoon()
2
11550
by: Pepijn Kenter | last post by:
Dear experts. I have a vector<float> and want to convert that to a vector<double>. I optimistically tried: #include <vector> #include <iostream> using namespace std; int main() {
19
3365
by: daniel | last post by:
1) is C++ smart enough to automatically use "bits" for bool or will a bool have the size of a charcter (byte). 2) The index of a vector is it an integer (4 byte) or a "long long" with 8 bytes or something else? I ask because I want to use vector<bool> with a few billion entries exceeding the int32 range for indexing and I want to use as few memory as possible for the whole
20
17842
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:
10
4848
by: Bob | last post by:
Here's what I have: void miniVector<T>::insertOrder(miniVector<T>& v,const T& item) { int i, j; T target; vSize += 1; T newVector; newVector=new T;
8
5115
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value types or std::vector<int>. So where I would use an int* and reallocate it from time to time in C, and randomly access it via , then I figure to copy the capacity and reserve methods, because I just need a growable array. I get to considering...
16
4441
by: Martin Jørgensen | last post by:
Hi, I get this using g++: main.cpp:9: error: new types may not be defined in a return type main.cpp:9: note: (perhaps a semicolon is missing after the definition of 'vector') main.cpp:9: error: two or more data types in declaration of 'set' I don't really see the problem... Here's the code:
6
6830
by: zl2k | last post by:
hi, there I am using a big, sparse binary array (size of 256^3). The size may be changed in run time. I first thought about using the bitset but found its size is unchangeable. If I use the vector<bool>, does each element takes 4 bytes instead of 1 bit? I am using gcc3.4.4. There is a bit_vector which is kind of old so I wont use that. Any other choices? Thanks ahead. zl2k
4
3516
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor, destructor, or type conversion before '.' token vector_static_function.c:21: error: expected constructor, destructor, or type conversion before '.' token
0
9687
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...
0
9541
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10251
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10228
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
10027
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
7565
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
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4141
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

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.