Click here to Skip to main content
15,892,927 members
Articles / Programming Languages / C++/CLI

Native under Managed

Rate me:
Please Sign up or sign in to vote.
5.00/5 (10 votes)
26 Jan 2012CPOL5 min read 71.8K   932   38  
A guide about how to mix native and managed code in one solution
#include <assert.h>
#include <sstream>

#include "num_native.h"

namespace num {

void NUM_EXCEPT(ConstStr fmt, ...) {
	char _text[_MAX_PATH];
	va_list argptr;
	va_start(argptr, fmt);
	vsprintf_s(_text, fmt, argptr);
	va_end(argptr);

#ifdef _DEBUG
	OutputDebugStringA(_text);
	OutputDebugStringA("\n");
#endif

	throw NumBaseException(_text);
}

void NUM_ENSURE(bool exp, ConstStr fmt, ...) {
	char _text[_MAX_PATH];
	va_list argptr;
	va_start(argptr, fmt);
	vsprintf_s(_text, fmt, argptr);
	va_end(argptr);

	if(!exp) NUM_EXCEPT("%s", _text);
}

Obj Obj::NIL = Obj();

StdStr Obj::ToStr(void) const {
	StdStr result;
	std::stringstream ss;
	switch(type) {
		case OT_NIL:
			ss << "(nil)";
			break;
		case OT_S32:
			ss << data._s32;
			break;
		case OT_U32:
			ss << data._u32;
			break;
		case OT_S64:
			ss << data._s64;
			break;
		case OT_U64:
			ss << data._u64;
			break;
		case OT_F32:
			ss << data._f32;
			break;
		case OT_F64:
			ss << data._f64;
			break;
		case OT_STR:
			ss << data._str;
			break;
		case OT_PTR:
			union { Ptr ptr; u32 u; } d;
			d.ptr = data._ptr;
			ss << d.u;
			break;
		case OT_BLN:
			ss << (data._bln ? "true" : "false");
			break;
		default:
			assert(!"Invalid type");
			break;
	}
	ss >> result;

	return result;
}

MyNativeClass::Listener::Listener() {
}

MyNativeClass::Listener::~Listener() {
}

void MyNativeClass::Listener::Callback(const ObjParamDict &p) {
}

MyNativeClass::MyNativeClass()
	: mFuncPtr(NULL), mListener(NULL), mInteger(0) {
}

MyNativeClass::~MyNativeClass() {
}

void MyNativeClass::SetFuncPtr(FuncPointer fp) {
	mFuncPtr = fp;
}

void MyNativeClass::SetListener(Listener* lsn) {
	mListener = lsn;
}

void MyNativeClass::Fun0(const ParamList &p) {
	f32 f0 = p.at(0);
	f32 f1 = p.at(1);
	f32 f2 = p.at(2);

	f32 sum = f0 + f1 + f2;
	ParamList _p;
	_p.push_back(sum);
	(*mFuncPtr)(_p);
}

void MyNativeClass::Fun1(const ObjParamDict &p) {
	StdStr ret;
	for(ObjParamDict::const_iterator it = p.begin(); it != p.end(); ++it) {
		ret += "[";
		ret += it->first.GetStr();
		ret += ": ";
		ret += it->second.GetStr();
		ret += "]; ";
	}

	ObjParamDict _p;
	_p["RET"] = ret.c_str();
	mListener->Callback(_p);
}

s32 MyNativeClass::GetIntegerProp(void) const {
	return mInteger;
}

void MyNativeClass::SetIntegerProp(s32 val) {
	mInteger = val;
}

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
China China
Video game player & creator; Hardware geek & maker.

Comments and Discussions