473,395 Members | 1,656 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Managed smart pointer

I did an interesting experiment with C++/CLI. I need a way to store
unmanaged objects in a managed class in an exception safe way, and I
thought I would attempt to duplicate the functionality of
boost::scoped_ptr. Here's my preliminary code:

template <class T>
ref class ScopedPtr
{
typedef ScopedPtr<T> this_type;

public:
ScopedPtr()
: ptr(nullptr)
{
}

explicit ScopedPtr(T * p)
: ptr(p)
{
}

~ScopedPtr()
{
delete ptr;
}

T * get()
{
return ptr;
}

T & operator*()
{
return *ptr;
}

T * operator->()
{
return ptr;
}

void swap(ScopedPtr % other)
{
T * tmp = other.ptr;
other.ptr = ptr;
ptr = tmp;
}

void swap(ScopedPtr ^ other)
{
T * tmp = other->ptr;
other->ptr = ptr;
ptr = tmp;
}

void reset()
{
this_type().swap(this);
}

void reset(T * p)
{
this_type(p).swap(this);
}

private:
T * ptr;
ScopedPtr(const ScopedPtr %);
ScopedPtr % operator=(const ScopedPtr %);
};

The main differences between the original code and this one are the
following:
- It is a ref class, although it wraps an unmanaged class.
- There are no const functions in .NET, therefore get, operator* and
operator-> can not be const.
- swap uses a managed reference (%), not a regular &. Same with the copy
c'tor and assignment op. I had to implement a handle version of the
swap, which is based on "^" instead of "%". That's because "*this" calls
operator* automatically, which I think is a bug in the compiler. I'm not
that familiar with the standard, but IMO, *this should evaluate to a
"this_type %", instead of a call to operator*. I'm really not sure about
this.
- There's no default argument in .NET, so two separate c'tors had to be
created, and two reset functions.

It seems to be working nicely:
struct C
{
int i;
C(int value) : i(value) { }
void Do() { }
};

ref class RC
{
public:
ScopedPtr<C> c;
};

void Test()
{
ScopedPtr<C> c1;
ScopedPtr<C> c2(new C(0));
*c2 = 5;
c2->Do();
//c1 = c2; // this fails to compile, as it should
ce.reset();
}

What do you guys think? Is there any ratinale behind this? After all, GC
is not for unmanaged types, so exception safety is important there.

Tom
Nov 17 '05 #1
0 778

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

Similar topics

11
by: Lloyd Dupont | last post by:
(not I use 2.0, so new return a "normal" pointer and gcnew return a managed one, my question below regarding new concern plain standart C++ allocator) - if I use the default new operator, are all...
8
by: Axter | last post by:
I normally use a program call Doxygen to document my source code.(http://www.stack.nl/~dimitri/doxygen) This method works great for small and medium size projects, and you can get good...
33
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the...
16
by: pkoniusz | last post by:
Hello everybody, Been just thinking how actually one could convert the following unmanaged code to the managed C++: struct JustAnExample { char value1; int value2; // etc ....
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
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...

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.