ppt - わんくま同盟

Download Report

Transcript ppt - わんくま同盟

(゚Д゚)ウマー
C++/CLI カクテル・レシピ
episthmh
Microsoft MVP, Visual C++
[email protected]
わんくま同盟 東京勉強会 #2
C++/CLI ってば…
• Cであり
• C++であり
• .NETである
Visual Basic / C# にはマネできんこと
やってやろうぜぃ!
わんくま同盟 東京勉強会 #2
(1.1) Native を Managed の中に混ぜるには
class Native { … };
ref class Managed {
Native* native;
public:
Managed() { native = new Native(); }
~Managed() { delete native; }
…
};
勝手に捨ててはくれません!
• クラスの場合、ポインタに限る
• new/delete をお忘れなく
• auto_ptr<Native> 使用不可
• インスタンスのコピー時に注意!
わんくま同盟 東京勉強会 #2
(1.2) Managed を Native の中に混ぜるには
ref class Managed { … };
class Native {
gcroot<Managed^> managed;
public:
Native() { managed = gcnew Managed(); }
~Native() { delete native; }
…
};
#include <vcclr.h> しませう!
• クラスの場合、ポインタに限る
• gcnew/delete をお忘れなく
• インスタンスのコピー時に注意!
※#include <msclr/auto_gcroot.h>
auto_gcroot<M^> → Managed版 auto_ptr
わんくま同盟 東京勉強会 #2
(2.1) System::String^ ⇔ std::wstring
// String^ ← wstring
String^ to_cli(const wstring& str) {
return gcnew String(str.c_str());
}
// wstring ← String^
wstring from_cli(String^ str) {
pin_ptr<wchar_t> result =
&(str->ToCharArray()[0]);
return result;
}
わんくま同盟 東京勉強会 #2
(2.2) System::String^ ⇔ std::string
// String^ ← string
String^ to_cli(const string& str) {
return gcnew String(str.c_str());
}
( ゜Д゜)マンドクセー
// string ← String^
string from_cli(String^ str) {
void* ptr = Marshal::StringToHGlobalAnsi(str).ToPointer();
string result(static_cast<const char*>(ptr));
Marshal::FreeHGlobal(System::IntPtr(ptr));
return result;
}
※ System::Runtime::InteropServices::Marshal
わんくま同盟 東京勉強会 #2
でもんすとらしおん
• Native と Managed の混在
• .Net を(ちょびっと)利用した C++
• View を C# ,
Model を C++/CLI で実装
• C++ を NUnit でテストする
わんくま同盟 東京勉強会 #2
Let’s enjoy OOPing!
わんくま同盟 東京勉強会 #2