Skip to content

Instantly share code, notes, and snippets.

@tritao
Created April 29, 2013 18:23
Show Gist options
  • Save tritao/5483612 to your computer and use it in GitHub Desktop.
Save tritao/5483612 to your computer and use it in GitHub Desktop.
C++/CLI examples
#define CLI_TESTS 1
#define CLI_TESTS_FORM 1
#if 1
#using <mscorlib.dll>
using namespace System;
using namespace System::Text;
using namespace System::Collections::Generic;
#ifdef CLI_TESTS_FORM
#using <System.Windows.Forms.dll>
using namespace System::Windows::Forms;
#endif
// CLI assembly-level attributes
[assembly:CLSCompliant(true)];
#endif
#if 1
// CLI reference class type definitions
ref class Foo {
int Do();
};
#endif
#if CLI_TESTS
void TestStaticMethod()
{
// C++/CLI 14.2.1.1 Ranking handle conversions
Console::WriteLine("Hello World!");
Console::WriteLine("Hello {0}{1}{2}{3}!", "1", 2, "3", 4);
}
void TestArray()
{
// CLI array creation & indexing (loads and stores)
auto A = gcnew array<Object^> {"1", "2"};
A[0] = "3";
Console::WriteLine("Array: {0}", A[0]);
// CLI array method calls (System::Array)
Console::WriteLine("Array: {0}", A->ToString());
// CLI array type conversion
System::Array^ Arr = A;
Object^ OA = A;
Console::WriteLine("Array Count: {0} {1}", Arr, OA);
}
void TestRefClass()
{
// CLI reference class type creation
auto SB = gcnew StringBuilder();
// CLI reference class type method calls
SB->AppendFormat("{0}{1}{2}{3}!", "a", "b", "c", "d");
Console::WriteLine(SB->ToString());
// CLI reference class safe casts
//Object^ Obj = safe_cast<Object^>(SB);
//Obj->ToString();
}
void TestValueClass()
{
// Value Type zero-initialization
UIntPtr Z;
// Value Type implicit parameter-less ctor
UIntPtr Z2{};
// Value Type ctor-initialization
UIntPtr C(2U);
// Value Type copy-initialization
UIntPtr CI = UIntPtr(2U);
// Value Type instance calls
Console::WriteLine("{0} {1} {2} {3}", Z.ToUInt32(), Z2.ToString(),
C.ToString(), CI);
// Value Type boxing
UIntPtr^ HI = CI;
Console::WriteLine("{0}", HI);
// Value Type GC allocation & boxing
//UIntPtr^ HV = gcnew UIntPtr();
}
void TestProperty()
{
// CLI array type properties access
auto ObjArray = gcnew array<Object^> {};
Console::WriteLine("Array Count: {0}", ObjArray->Length);
// CLI class type properties access (getter & setter)
auto SB = gcnew StringBuilder();
SB->Capacity = 20;
Console::WriteLine("String Builder Capacity: {0}", SB->Capacity);
}
void TestGenerics()
{
auto L = gcnew List<int>();
L->Add(20);
L->Add(30);
Console::WriteLine("List Count: {0}", L->Count);
for (unsigned I = 0, E = L->Count; I != E; ++I) {
//Console::WriteLine(" Element: {0}", L[I]);
}
}
void TestExceptions()
{
}
void TestForms()
{
#ifdef CLI_TESTS_FORM
Form ^F = gcnew Form();
Application::Run(F);
#endif
}
#endif
int main()
{
#if CLI_TESTS
TestStaticMethod();
TestArray();
TestRefClass();
TestValueClass();
TestProperty();
TestGenerics();
TestExceptions();
TestForms();
#endif
Console::WriteLine("Hello World!");
// UNIMPLEMENTED FEATURES:
#if 0
ref class C {
public:
static void Print(System::Boolean) {}
};
C::Print("LOL");
#endif
#if 0
// CLI array foreach statement
for each (Object^ o in ObjArray)
{
Console::WriteLine(o->ToString());
}
#endif
#if 0
// CLI reference class type automatic allocation
//Object O;
//Console::WriteLine(O.ToString());
#endif
#if 0
// Builtin types as value class types
//Int32 I = 2;
//I.ToString();
//Int32^ HI = I;
//int BI = 2;
//int^ HBI = BI;
//Console::WriteLine(I);
//Console::WriteLine(BI);
#endif
#if 0
// CLI default indexers
//Console::WriteLine(" Element: {0}", L->default[I]);
#endif
#if 0
// CLI reference class safe casts
//Object^ Obj = safe_cast<Object^>(SB);
//Obj->ToString();
#endif
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment