|
楼主 |
发表于 2013-3-12 22:35:20
|
显示全部楼层
本帖最后由 reziarfg 于 2013-3-12 22:46 编辑
基本原理:
基于C++类编写C包装器,通过PINVOKE由C#类调用。
C++类 -> C包装器-> PINVOKE-> C#类
示例1 生存周期
顺序图:
示例2 操作符== 顺序图:
1、C++端代码片段(osg.dll):
- namespace osg {
- class Vec3f // 部分代码
- {
- public:
- /** Data type of vector components.*/
- typedef float value_type;
- /** Constructor that sets all components of the vector to zero */
- Vec3f() { _v[0]=0.0f; _v[1]=0.0f; _v[2]=0.0f;}
- Vec3f(value_type x,value_type y,value_type z);
- Vec3f(const Vec2f& v2,value_type zz);
- /** Number of vector components. */
- enum { num_components = 3 };
- value_type _v[3];
- inline value_type x() const;
- inline value_type y() const;
- inline value_type z() const;
- /** Returns true if all components have values that are not NaN. */
- inline bool valid();
- inline bool operator == (const Vec3f& v) const;
- inline bool operator < (const Vec3f& v) const;
- inline bool operator + (const Vec3f& v) const;
- /** Negation operator. Returns the negative of the Vec3f. */
- inline const Vec3f operator - () const;
- /** Length of the vector = sqrt( vec . vec ) */
- inline value_type length() const;
- /** Normalize the vector so that it has length unity.
- * Returns the previous length of the vector.
- */
- inline value_type normalize();
- }; // end of class Vec3f
- } // end of namespace osg
复制代码
2、C包装器代码片段(osg_wrapper(d).dll):- #ifndef SWIGEXPORT
- # if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)
- # if defined(STATIC_LINKED)
- # define SWIGEXPORT
- # else
- # define SWIGEXPORT __declspec(dllexport)
- # endif
- # else
- # if defined(__GNUC__) && defined(GCC_HASCLASSVISIBILITY)
- # define SWIGEXPORT __attribute__ ((visibility("default")))
- # else
- # define SWIGEXPORT
- # endif
- # endif
- #endif
- #ifdef __cplusplus
- extern "C" {
- #endif
- SWIGEXPORT void * __stdcall CSharp_new_Vec3f__SWIG_0() {
- return new osg::Vec3f();
- }
- SWIGEXPORT void * __stdcall CSharp_new_Vec3f__SWIG_1(float jarg1, float jarg2, float jarg3) {
- return new osg::Vec3f(jarg1, jarg2, jarg3);
- }
- SWIGEXPORT void * __stdcall CSharp_new_Vec3f__SWIG_2(void * jarg1, float jarg2) {
- osg::Vec2f *arg1 = (osg::Vec2f *)jarg1;
- if (!arg1) {
- SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentNullException, "osg::Vec2f const & type is null", 0);
- return 0;
- }
- return new osg::Vec3f((osg::Vec2f const &)*arg1, jarg2);
- }
- SWIGEXPORT void __stdcall CSharp_delete_Vec3f(void * jarg1) {
- osg::Vec3f *arg1 = (osg::Vec3f *)jarg1;
- delete arg1;
- }
- SWIGEXPORT int __stdcall CSharp_Vec3f_num_components_get() {
- int result = (int)osg::Vec3f::num_components;
- return jresult;
- }
- SWIGEXPORT void __stdcall CSharp_Vec3f__v_set(void * jarg1, void * jarg2) {
- osg::Vec3f *arg1 = (osg::Vec3f *) 0 ;
- osg::Vec3f::value_type *arg2 ;
- arg1 = (osg::Vec3f *)jarg1;
- arg2 = (osg::Vec3f::value_type *)jarg2;
- {
- size_t ii;
- osg::Vec3f::value_type *b = (osg::Vec3f::value_type *) arg1->_v;
- for (ii = 0; ii < (size_t)3; ii++) b[ii] = *((osg::Vec3f::value_type *) arg2 + ii);
- }
- }
- SWIGEXPORT float __stdcall CSharp_Vec3f_x(void * jarg1) {
- osg::Vec3f *arg1 = (osg::Vec3f *)jarg1;
- osg::Vec3f::value_type result;
- result = (osg::Vec3f::value_type)(arg1->x());
- return result;
- }
- SWIGEXPORT void __stdcall CSharp_Vec3f_set_x(void * jarg1,float jarg2) {
- osg::Vec3f *arg1 = (osg::Vec3f *) 0 ;
- arg1 = (osg::Vec3f *)jarg1;
- arg1->x()=jarg2;
- }
- SWIGEXPORT float __stdcall CSharp_Vec3f_y(void * jarg1) {
- osg::Vec3f *arg1 = (osg::Vec3f *)jarg1;
- osg::Vec3f::value_type result;
- result = (osg::Vec3f::value_type)(arg1->y());
- return result;
- }
- SWIGEXPORT void __stdcall CSharp_Vec3f_set_y(void * jarg1,float jarg2) {
- osg::Vec3f *arg1 = (osg::Vec3f *) 0 ;
- arg1 = (osg::Vec3f *)jarg1;
- arg1->y()=jarg2;
- }
- SWIGEXPORT float __stdcall CSharp_Vec3f_z(void * jarg1) {
- osg::Vec3f *arg1 = (osg::Vec3f *)jarg1;
- osg::Vec3f::value_type result;
- result = (osg::Vec3f::value_type)(arg1->z());
- return result;
- }
- SWIGEXPORT void __stdcall CSharp_Vec3f_set_z(void * jarg1,float jarg2) {
- osg::Vec3f *arg1 = (osg::Vec3f *) 0 ;
- arg1 = (osg::Vec3f *)jarg1;
- arg1->z()=jarg2;
- }
- SWIGEXPORT unsigned int __stdcall CSharp_Vec3f_valid(void * jarg1) {
- unsigned int jresult ;
- osg::Vec3f *arg1 = (osg::Vec3f *)jarg1;
- bool result;
- result = (bool)( arg1->valid());
- jresult = result;
- return jresult;
- }
- SWIGEXPORT unsigned int SWIGSTDCALL CSharp_Vec3f_Equal(void * jarg1, void * jarg2) {
- unsigned int jresult ;
- osg::Vec3f *arg1 = (osg::Vec3f *)jarg1;
- osg::Vec3f *arg2 = arg2 = (osg::Vec3f *)jarg2 ;
- if (!arg2) {
- SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentNullException, "osg::Vec3f const & type is null", 0);
- return 0;
- }
- bool result = arg1->operator ==((osg::Vec3f const &)*arg2);
- jresult = (unsigned int)result;
- return jresult;
- }
- SWIGEXPORT unsigned int SWIGSTDCALL CSharp_Vec3f_Less(void * jarg1, void * jarg2) {
- unsigned int jresult ;
- osg::Vec3f *arg1 = (osg::Vec3f *)jarg1;
- osg::Vec3f *arg2 = arg2 = (osg::Vec3f *)jarg2 ;
- if (!arg2) {
- SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentNullException, "osg::Vec3f const & type is null", 0);
- return 0;
- }
- bool result = arg1->operator <((osg::Vec3f const &)*arg2);
- jresult = (unsigned int)result;
- return jresult;
- }
- SWIGEXPORT void * SWIGSTDCALL CSharp_Vec3f_Add(void * jarg1, void * jarg2) {
- void * jresult ;
- osg::Vec3f *arg1 = (osg::Vec3f *)jarg1;
- osg::Vec3f *arg2 = arg2 = (osg::Vec3f *)jarg2 ;
- if (!arg2) {
- SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentNullException, "osg::Vec3f const & type is null", 0);
- return 0;
- }
- osg::Vec3f result = arg1->operator +((osg::Vec3f const &)*arg2);
- jresult = new osg::Vec3f((const osg::Vec3f &)result);
- return jresult;
- }
- SWIGEXPORT void * SWIGSTDCALL CSharp_Vec3f_Neg(void * jarg1) {
- void * jresult ;
- osg::Vec3f *arg1 = (osg::Vec3f *)jarg1;
- osg::Vec3f result = arg1->operator -();
- jresult = new osg::Vec3f((const osg::Vec3f &)result);
- return jresult;
- }
- SWIGEXPORT float __stdcall CSharp_Vec3f_length(void * jarg1) {
- osg::Vec3f *arg1 = (osg::Vec3f *)jarg1;
- osg::Vec3f::value_type result = arg1)->length();
- return result;
- }
- SWIGEXPORT float __stdcall CSharp_Vec3f_normalize(void * jarg1) {
- osg::Vec3f *arg1 = (osg::Vec3f *)jarg1;
- return arg1->normalize();
- }
- #ifdef __cplusplus
- }
- #endif
复制代码
3、C# PINVOKE代码片段(osg_cs.dll):
- public class osgPINVOKE
- {
- #if DEBUG
- public const string LIBNAME = "osg_cs_wrapperd";
- #else
- public const string LIBNAME = "osg_cs_wrapper";
- #endif
- [DllImport(LIBNAME, EntryPoint="CSharp_Vec3f_num_components_get")]
- public static extern int Vec3f_num_components_get();
- [DllImport(LIBNAME, EntryPoint="CSharp_new_Vec3f__SWIG_0")]
- public static extern IntPtr new_Vec3f__SWIG_0();
- [DllImport(LIBNAME, EntryPoint="CSharp_new_Vec3f__SWIG_1")]
- public static extern IntPtr new_Vec3f__SWIG_1(float jarg1, float jarg2, float jarg3);
- [DllImport(LIBNAME, EntryPoint="CSharp_new_Vec3f__SWIG_2")]
- public static extern IntPtr new_Vec3f__SWIG_2(HandleRef jarg1, float jarg2);
- [DllImport(LIBNAME, EntryPoint="CSharp_delete_Vec3f")]
- public static extern void delete_Vec3f(HandleRef jarg1);
- [DllImport(LIBNAME, EntryPoint="CSharp_Vec3f_x")]
- public static extern float Vec3f_x(HandleRef jarg1);
- [DllImport(LIBNAME, EntryPoint = "CSharp_Vec3f_set_x")]
- public static extern void Vec3f_set_x(HandleRef jarg1,float jarg2);
- [DllImport(LIBNAME, EntryPoint="CSharp_Vec3f_y")]
- public static extern float Vec3f_y(HandleRef jarg1);
- [DllImport(LIBNAME, EntryPoint = "CSharp_Vec3f_set_y")]
- public static extern void Vec3f_set_y(HandleRef jarg1, float jarg2);
- [DllImport(LIBNAME, EntryPoint="CSharp_Vec3f_z")]
- public static extern float Vec3f_z(HandleRef jarg1);
- [DllImport(LIBNAME, EntryPoint = "CSharp_Vec3f_set_z")]
- public static extern void Vec3f_set_z(HandleRef jarg1, float jarg2);
- [DllImport(LIBNAME, EntryPoint="CSharp_Vec3f_valid")]
- public static extern bool Vec3f_valid(HandleRef jarg1);
- [DllImport(LIBNAME, EntryPoint="CSharp_Vec3f_Equals")]
- public static extern bool Vec3f_Equals(HandleRef jarg1, HandleRef jarg2);
- [DllImport(LIBNAME, EntryPoint="CSharp_Vec3f_Less")]
- public static extern bool Vec3f_Less(HandleRef jarg1, HandleRef jarg2);
- [DllImport(LIBNAME, EntryPoint="CSharp_Vec3f_Add")]
- public static extern IntPtr Vec3f_Add(HandleRef jarg1, HandleRef jarg2);
- [DllImport(LIBNAME, EntryPoint="CSharp_Vec3f_Add")]
- public static extern IntPtr Vec3f_Neg(HandleRef jarg1);
- [DllImport(LIBNAME, EntryPoint="CSharp_Vec3f_length")]
- public static extern float Vec3f_length(HandleRef jarg1);
- [DllImport(LIBNAME, EntryPoint="CSharp_Vec3f_normalize")]
- public static extern float Vec3f_normalize(HandleRef jarg1);
- } // end of class osgPINVIKKE
复制代码
4、C#类代码片段(osg_cs.dll):
- namespace osg
- {
- using System;
- using System.Runtime.InteropServices;
- public class Vec3f : IDisposable
- {
- private HandleRef swigCPtr;
- protected bool swigCMemOwn;
- public Vec3f(IntPtr cPtr, bool cMemoryOwn)
- {
- swigCMemOwn = cMemoryOwn;
- swigCPtr = new HandleRef(this, cPtr);
- }
- public static HandleRef getCPtr(Vec3f obj)
- {
- return (obj == null) ? new HandleRef(null, IntPtr.Zero) : obj.swigCPtr;
- }
- ~Vec3f()
- {
- Dispose();
- }
- public virtual void Dispose()
- {
- lock (this)
- {
- if (swigCPtr.Handle != IntPtr.Zero)
- {
- if (swigCMemOwn)
- {
- swigCMemOwn = false;
- osgPINVOKE.delete_Vec3f(swigCPtr);
- }
- swigCPtr = new HandleRef(null, IntPtr.Zero);
- }
- GC.SuppressFinalize(this);
- }
- }
- public Vec3f()
- : this(osgPINVOKE.new_Vec3f__SWIG_0(), true)
- {
- }
- public Vec3f(float x, float y, float z)
- : this(osgPINVOKE.new_Vec3f__SWIG_1(x, y, z), true)
- {
- }
- public Vec3f(Vec2f v2, float zz)
- : this(osgPINVOKE.new_Vec3f__SWIG_2(Vec2f.getCPtr(v2), zz), true)
- {
- if (osgPINVOKE.SWIGPendingException.Pending) throw osgPINVOKE.SWIGPendingException.Retrieve();
- }
- public float x
- {
- get { return osgPINVOKE.Vec3f_x(swigCPtr); }
- set { osgPINVOKE.Vec3f_set_x(swigCPtr, value); }
- }
- public float y
- {
- get { return osgPINVOKE.Vec3f_y(swigCPtr); }
- set { osgPINVOKE.Vec3f_set_y(swigCPtr, value); }
- }
- public float z
- {
- get { return osgPINVOKE.Vec3f_z(swigCPtr); }
- set { osgPINVOKE.Vec3f_set_z(swigCPtr, value); }
- }
- public bool valid()
- {
- bool ret = osgPINVOKE.Vec3f_valid(swigCPtr);
- return ret;
- }
- public bool Equals(Vec3f v)
- {
- bool ret = osgPINVOKE.Vec3f_Equals(swigCPtr, Vec3f.getCPtr(v));
- if (osgPINVOKE.SWIGPendingException.Pending) throw osgPINVOKE.SWIGPendingException.Retrieve();
- return ret;
- }
- public bool Less(Vec3f v)
- {
- bool ret = osgPINVOKE.Vec3f_Less(swigCPtr, Vec3f.getCPtr(v));
- if (osgPINVOKE.SWIGPendingException.Pending) throw osgPINVOKE.SWIGPendingException.Retrieve();
- return ret;
- }
- public Vec3f Add(Vec3f rhs)
- {
- Vec3f ret = new Vec3f(osgPINVOKE.Vec3f_Add(swigCPtr, Vec3f.getCPtr(rhs)), true);
- if (osgPINVOKE.SWIGPendingException.Pending) throw osgPINVOKE.SWIGPendingException.Retrieve();
- return ret;
- }
- public Vec3f Neg(Vec3f rhs)
- {
- Vec3f ret = new Vec3f(osgPINVOKE.Vec3f_Neg(swigCPtr, Vec3f.getCPtr(rhs)), true);
- }
- public float length()
- {
- float ret = osgPINVOKE.Vec3f_length(swigCPtr);
- return ret;
- }
- public float normalize()
- {
- float ret = osgPINVOKE.Vec3f_normalize(swigCPtr);
- return ret;
- }
- public static readonly int num_components = osgPINVOKE.Vec3f_num_components_get();
- #region Operators
- public float this[int index]
- {
- get
- { //检查索引范围
- if (index == 0)
- return this.x;
- else if (index == 1)
- return this.y;
- else if (index == 2)
- return this.z;
- else
- throw new ArgumentOutOfRangeException("[]",index.ToString());
- }
- set
- {
- if (index == 0)
- x = value;
- else if (index == 1)
- y = value;
- else if (index == 2)
- z = value;
- }
- }
- public override bool Equals(object obj)
- {
- if (obj == null || GetType() != obj.GetType()) return false;
- Vec3f v2 = (Vec3f)obj;
- return this.Equals(v2);
- }
- public override int GetHashCode()
- {
- return x.GetHashCode() ^ y.GetHashCode() ^ z.GetHashCode();
- }
- public static bool operator ==(Vec3f v1, Vec3f v2)
- {
- return Object.Equals(v1, v2);
- }
- public static bool operator !=(Vec3f v1, Vec3f v2)
- {
- return !Object.Equals(v1, v2);
- }
- public static bool operator <(Vec3f v1, Vec3f v2)
- {
- return v1.Less(v2);
- }
- public static Vec3f operator +(Vec3f v1, Vec3f v2)
- {
- return v1.Add(v2);
- }
- public static Vec3f operator -(Vec3f v)
- {
- return v.Neg();
- }
- #endregion // Operators
- } // end of class Vec3f
- } // end of namespace osg
复制代码
|
|