auto_ptr in managed code

  • Thread starter Thread starter Bill Burris
  • Start date Start date
B

Bill Burris

How do I use auto_ptr in managed C++?

Here is my existing code:

namespace Alta
{
public __gc class CMDAQ
{
public:
CMDAQ();
~CMDAQ();
private:
CDAQControl* itsDAQControl;
};
}

namespace Alta
{
CMDAQ::CMDAQ( )
{
itsDAQControl = new CDAQControl();
}

CMDAQ::~CMDAQ()
{
delete itsDAQControl;
}
}

If I change this code to the following, I get "error C3633: cannot define
itsDAQControl as a member of managed Alta::CMDAQ"

namespace Alta
{
public __gc class CMDAQ
{
public:
CMDAQ();
~CMDAQ();
private:
std::auto_ptr< CDAQControl > itsDAQControl;
};
}

namespace Alta
{
CMDAQ::CMDAQ( )
{
std::auto_ptr< CDAQControl > dc( new CDAQControl() );
itsDAQControl = dc;
}

CMDAQ::~CMDAQ()
{
}
}

thanks

Bill
 
Bill said:
How do I use auto_ptr in managed C++?

[SNIP]

If I change this code to the following, I get "error C3633: cannot define
itsDAQControl as a member of managed Alta::CMDAQ"
...
private:
std::auto_ptr< CDAQControl > itsDAQControl;

Hi Bill,
Unfortunately, you cannot do this with the current C++ language. auto_ptr
is a native class, and it is not possible to embed that class in a __gc
class. I realize this is a burden, and believe me -- we are working to
improve this situation.

The next version of the Visual C++ will allow you to write a different
auto_ptr template that would achieve what you are trying to do.

Cheerio!
 
auto_ptr
is a native class, and it is not possible to embed that class in a __gc
class. I realize this is a burden, and believe me -- we are working to
improve this situation.

What other native classes are not usable in a _gc class?

I use managed C++ as the bridge between my native C++ code and C#. If I was
doing pure .NET code, I would use C#.

Bill
 
Bill said:
What other native classes are not usable in a _gc class?

With one exception, all native classes cannot be embedded in a __gc class.
The exception is POD types (a POD is short for "plain old data" and is a
class that neither has a user defined copy constructor nor a user defined
destructor, and contains only other PODs or scalar data).
I use managed C++ as the bridge between my native C++ code and C#. If I
was doing pure .NET code, I would use C#.

I understand your position. This too is a sentiment we are looking to fix in
the next version of Visual C++.

Cheerio!
 
Brandon Bray said:
With one exception, all native classes cannot be embedded in a __gc class.
The exception is POD types (a POD is short for "plain old data" and is a
class that neither has a user defined copy constructor nor a user defined
destructor, and contains only other PODs or scalar data).

I have been using destructors in my managed C++. It seems to work ok.

Some __gc code which uses native classes:

CMDAQ::CMDAQ( IMessageHandler* pMessageHandler, int siteId, int
interfaceType )
{
itsMessageDispatcher = new CMessageDispatcher();
itsMessageDispatcher->itsMessageHandler = pMessageHandler;
CDebug::Initialize( itsMessageDispatcher );
try
{
itsDAQControl = new CDAQControl( itsMessageDispatcher, siteId,
interfaceType );
itsHistogramGroup = itsDAQControl->GetHistogramGroup();
itsPhantomHistogramGroup = itsDAQControl->GetPhantomHistogramGroup();
}
catch( char* str )
{
itsMessageDispatcher->DebugMessage( str );
throw( new System::Exception( str ) );
}
}

CMDAQ::~CMDAQ()
{
delete itsDAQControl;
delete itsMessageDispatcher;
}

This is used in my C# code as follows:

public class DAQ
{
private static CMDAQ itsDaq = null;
private IMessageHandler itsMessageHandler;

public DAQ( IMessageHandler messageHandler, int interfaceType, int
siteId )
{
itsMessageHandler = messageHandler;
itsDaq = new CMDAQ( itsMessageHandler, siteId, interfaceType );
}

public void Dispose()
{
itsDaq.__dtor();
}

Dispose is called in the OnClosing funtion for my Form:

protected override void OnClosing( CancelEventArgs e )
{
base.OnClosing( e );
if( itsState == ProgState.DAQ )
{
itsDaq.Stop();
itsDaq.Dispose();
itsSettings.Save( @"c:\alta_prog\33\ProgramSettings.xml" );
}
}

Bill
 
Brandon Bray said:
I understand your position. This too is a sentiment we are looking to fix in
the next version of Visual C++.

Even better would be if I could do everything in C#. For that to happen I
need serial port support in .NET, or device drivers for Motorola GPS
receivers which are usable in .NET. Also Measurement Computing
http://www.measurementcomputing.com/, hardware needs to be accessible from
..NET, and a .NET version of Tetradyne DriverX
http://www.tetradyne.com/driverx.htm.

Support for USB would also come in handy. I tried to roll my own but didn't
get very far. What I did do is posted at
http://www.componentsnotebook.com/notebooks/csharp/deviceio.aspx.

Bill
 
Back
Top