C++/CLI, "constant" reference to a tracking handle - c++-cli

I have spotted something like this in code:
void foo(IList<int>^ const & list ) { ... }
What does this ^ const& mean? I looked in the C++/CLI specification, but found no comments on making constant tracking references, nor the ^& combo.
Is this legal?

This code was probably written by a C++ programmer that used common C++ idiom to write C++/CLI. It is quite wrong, passing a reference to tracking handle is only possible if the handle is stored on the stack. It cannot work if the passed List<> reference is stored in a field of an object on the heap, the garbage collector can move it and make the pointer invalid. The compiler will catch it and generate an error. The ^ is already a reference, no additional reference is needed.
Without the reference, the const keyword doesn't make a lot of sense anymore either. Not that it ever did before, the CLR cannot enforce it. Not that this mattered much here, this code could not be called from any other .NET language. They won't generate a pointer to the tracking handle.
Just fix it, there's little point in keeping bad code like this:
void foo(IList<int>^ list ) { ... }
Example of code that shows that the reference cannot work:
using namespace System;
using namespace System::Collections::Generic;
ref class Test {
public:
IList<int>^ lst;
void foo(IList<int> const &list) {}
void wontcompile() {
foo(lst); // C3699
IList<int>^ okay;
foo(okay);
}
};

It's a reference which is constant to a tracking handle.
It allows you to pass the handle by reference instead of by value. Presumably the author thinks it's more efficient than copying the handle.
If the author meant to make the handle constant he should have used either of
Method(TestClass const ^ const & parameter)
Method(TestClass const^ parameter)
Or alternatively
Method(TestClass const^& parameter) - but the caller must const up the handle first
with
TestClass const^ constHandle = nonConstHandle
An example of each:
// test.cpp : Defines the entry point for the console application.
#include "stdafx.h"
ref class TestClass
{
public:
void setA(int value)
{
a = value;
}
TestClass() :
a(10)
{
}
private:
int a;
};
class TakesHandle
{
public:
void methodX1(TestClass const ^ const & parameter)
{
// Un-commenting below causes compiler error
// parameter->setA(11);
}
void methodX2(TestClass const^ parameter)
{
// Un-commenting below causes compiler error
// parameter->setA(11);
}
void methodX3(TestClass const^& parameter)
{
// Un-commenting below causes compiler error
// parameter->setA(11);
}
};
int _tmain(int argc, _TCHAR* argv[])
{
TakesHandle takes;
TestClass ^ test1 = gcnew TestClass();
// compiles
takes.methodX1(test1);
// compiles
takes.methodX2(test1);
TestClass const ^ constHandle = test1;
takes.methodX3(constHandle);
return 0;
}

Related

Parallel for loop in c++/cli

Parallel.For(<your starting value >,<End criteria for loop>, delegate(int < your variable Name>)
{
// Your own code
}); 
Here above I am showing some sample code in C#. I want similar functionality in C++/CLI but I don't know how to use this expression: "delegate(int < your variable Name>)".
If you are using c++cli, then you should be able to use the same Parallel.For that you use in C# since System.Threading.Tasks.Parallel is a regular .Net Framework class
Example (untested, not even compiled):
ref class SomeClass
{
public:
static void Func(int index)
{
Console::WriteLine("Test {0}", index);
}
};
delegate void MyCallback(int index);
int main( )
{
MyCallback^ callback = gcnew MyCallback(SomeClass::Func);
Parallel.For(0, 9, callback);
}
Relevant: How to: Define and Use Delegates in C++/CLI

RunmtimeTypeHandle to type

Here is a sample program that wraps up a managed enum for unmanaged use. I want to be able to get wrapped enums back from unmanaged code and convert them back to managed enums with as little overhead as possible. My current scheme requires creating a GCHandle to the Type object. I was thinking maybe there is a way to use the RuntimeTypeHandle value instead to remove this overhead. Is that possible? Or any other efficiency suggestions anyone has.
public value class Stuff {
public:
enum class Modes { A, B };
};
struct enum_wrap {
intptr_t type;
int value;
};
void g(enum_wrap e);
enum_wrap h();
void f(Stuff::Modes mode)
{
enum_wrap wrap;
wrap.value = (int)mode;
wrap.type = static_cast<intptr_t>(GCHandle::ToIntPtr(GCHandle::Alloc(Stuff::Modes::typeid)));
g(wrap);
// ...
enum_wrap ret = h();
Type^ ret_type = safe_cast<Type^>(GCHandle::FromIntPtr(static_cast<IntPtr>(ret.type)).Target);
Enum^ e_obj = (Enum^)Enum::ToObject(ret_type, ret.value);
Console::Write(e_obj->GetType());
Console::Write(": ");
Console::WriteLine(e_obj);
}
#pragma unmanaged
enum_wrap g_enum;
void g(enum_wrap e) {
g_enum = e;
}
enum_wrap h() {
return g_enum;
}
I know this leaks because of never freeing GCHandles. I already have code that can deal with that if this is the best approach I can find. But I'm hoping to not need the Type objects at all.
Edit 1: Put Modes enum in a class so it works right.

Type casting in C++\CLI project

I have project which I am compiling with /clr. I have a class like below..
ref class A
{
public:
void CheckValue(void * test);
typedef ref struct val
{
std::string *x;
}val_t;
};
in my implementation I ahve to use something like below..
void A::CheckValue(void *test)
{
a::val_t^ allVal = (a::val_t^)test;
}
in my main I have used like..
int main()
{
A^ obj = gcnew A();
a::val_t valObj = new std::string("Test");
obj->CheckValue((void*)valObj);
}
I am getting type cast error and two places -
obj->CheckValue((void*)valObj);
and at
obj->CheckValue((void*)valObj);
error C2440: 'type cast' : cannot convert from 'void*' to 'A::val_t ^'
This snippet is just to show behavior at my end and I ahve to use it this way only. Earlier I was running it using non /clr so it compiled fine.
Now question I have how can I make this type casting work in C++/CLI type project?
Replace void * with Object^. You can also write a generic version of CheckValue but then there is not much point of having a weak-typed parameter when you have the type in the generic parameter.
A reference handle represents an object on the managed heap. Unlike a native pointer, CLR could move the object around during a function call, so the behavior of a pointer and a reference handle is different, and a type cast would fail. You can also pin the object being referenced using pin_ptr if you really need a void* so CLR would not be moving the object during the function call.
Here is how I would get around the limitation you are seeing, just remove the struct from the managed object, since it contains native pointer types.
struct val_t
{
char* x;
};
ref class A
{
public:
void CheckValue(void* test);
};
void A::CheckValue(void* test)
{
val_t* allVal = (val_t*)test;
}
int main()
{
A^ obj = gcnew A();
val_t valObj;
valObj.x = "Test";
obj->CheckValue((void*)&valObj);
}
Now, if you absolutely need the struct to be managed, here is how to do it:
ref class A
{
public:
void CheckValue(void * test);
value struct val_t
{
char* x;
};
};
void A::CheckValue(void *test)
{
a::val_t* allVal = (a::val_t*)test;
}
int main()
{
A^ obj = gcnew A();
a::val_t valObj;
valObj.x = "Test";
pin_ptr<a::val_t> valPin = &valObj;
obj->CheckValue((void*)valPin);
}

C++/CLI: Passing C++ class ptr to unmanaged method

I've been given a third party C/C++ library (.dll, .lib, .exp and .h) that I need to use in our C# app.
ThirdPartyLibrary.h contains...
class AClass {
public:
typedef enum {
green = 7,
blue = 16
} Color;
virtual int GetData()=0;
virtual int DoWork(Color, char *)=0;
};
void * Func1(int, AClass **aClass);
In my C++/CLI code I have done this...
#include "ThirdPartyLibrary.h"
using namespace System;
using namespace System::Runtime::InteropServices;
namespace Wrapper {
public ref class MyBridgeClass
{
private:
AClass* pAClass;
public:
// C# code will call this method
void AMethod (int x)
{
int y = x+10;
Func1 (y, &(this->pAClass)); // <-- error!
}
}
}
I get a build error that reads...
cannot convert parameter 2 from 'cli::interior_ptr<Type>' to 'AClass **'
with
[
Type=AClass *
]
Cannot convert a managed type to an unmanaged type
Any ideas? Maybe I need #pragma manage/unmanged tags in my C++/CLI?
The reason you're getting that error is because of how managed memory works.
In your managed class, you've got a pointer defined. The address of that pointer is part of the managed object, and can change when the garbage collector runs. That's why you can't just pass &pAClass to the method, the GC can change what that address actually is.
There's a couple things you can do to fix this:
You could create an unmanaged helper class to hold the AClass* member. I'd do this if that pointer needs to stay valid beyond the invocation of this method, or if you have a lot of unmanaged pointers to hold.
struct UnmanagedHolder
{
AClass* pAClass;
};
public ref class MyBridgeClass
{
private:
// must create in constructor, delete in destructor and finalizer.
UnmanagedHolder* unmanaged;
public:
// C# code will call this method
void AMethod (int x)
{
int y = x+10;
Func1 (y, &(this->unmanaged->pAClass));
}
};
If you only need the pointer to be valid within AMethod, and the pointer doesn't need to remain valid after the call to Func1, then you can use a pin_ptr.
void AMethod (int x)
{
int y = x+10;
pin_ptr<AClass*> pin = &(this->pAClass);
Func1 (y, pin);
}

Exposing unmanaged const static std::string in a managed C++ class

I have a non-.NET C++ class as follows:
Foo.h:
namespace foo {
const static std::string FOO;
...
}
Foo.cc:
using namespace foo;
const std::string FOO = "foo";
I want to expose this for use in a C# application, but I keep getting errors about mixed types when I try the following:
FooManaged.h:
namespace foo {
namespace NET {
public ref class Foo {
public:
const static std::string FOO;
}
}
}
FooManaged.cc:
using namespace foo::NET;
const std::string Foo::FOO = foo::FOO;
What's the right way to translate an unmanaged string constant to a managed string constant?
In C++/CLI, the literal keyword is used in place of static const where you want the constant definition to be included in the interface exposed to fully managed applications.
public:
literal String^ Foo = "foo";
Unfortunately, literal requires an immediate value, so using the std::string value is not possible. As an alternative, you can create a static read-only property that returns the string.
public:
static property String^ Foo
{
String^ get()
{
return gcnew String(Foo::FOO.c_str());
}
}
Personally, I believe rewriting the string again and using literal is the better option. However, if you are highly concerned about the constant changing (in a newer version, for example), the property will use the version of FOO in the native library.