上一篇在这 C++混合编程之idlcpp教程Lua篇(7)

第一篇在这 C++混合编程之idlcpp教程(一)

与前面的工程相似,工程LuaTutorial6中,同样加入了四个文件:LuaTutorial6.cpp, Tutorial6.cpp, Tutorial6.i, tutorial6.lua。其中LuaTutorial6.cpp的内容基本和LuaTutorial5.cpp雷同,不再赘述。

首先看一下Tutorial6.i的内容:

#import "../../paf/src/pafcore/typedef.i"

namespace tutorial
{
  template<N>
struct Vector3
{
Vector3();
Vector3(const Vector3& v);
Vector3(N a, N b, N c);
Vector3(const N* p);
N getLength();
N length get;
N lengthSquare get; static Vector3<N> s_zero; nocode N x;
nocode N y;
nocode N z;
nocode N v[#]; #{
union
{
struct
{
N x,y,z;
};
N v[];
};
#}
}; export Vector3<float>;
export Vector3<double>;
typedef Vector3<float> Vector3f;
typedef Vector3<double> Vector3d; #{
template<typename N>
Vector3<N> Vector3<N>::s_zero(, , ); template<typename N>
inline Vector3<N>::Vector3()
{
} template<typename N>
inline Vector3<N>::Vector3(const Vector3<N>& v) : x(v.x), y(v.y), z(v.z)
{} template<typename N>
inline Vector3<N>::Vector3(N a, N b, N c) : x(a), y(b), z(c)
{} template<typename N>
inline Vector3<N>::Vector3(const N* p) : x(p[]), y(p[]), z(p[])
{} template<typename N>
inline N Vector3<N>::getLength()
{
return N(sqrt(x * x + y * y + z * z));
} template<typename N>
inline N Vector3<N>::get_length()
{
return N(sqrt(x * x + y * y + z * z));
} template<typename N>
inline N Vector3<N>::get_lengthSquare()
{
return (x * x + y * y + z * z);
}
#}
}

template<N>

struct Vector3

这是一个模板类,C++的模板功能复杂强大,编译器实在难写。所以大多数C++模板的高级特性在idlcpp中都没有做支持,毕竟idlcpp只负责对脚本语言提供接口,有一些简单的模板功能就够用了。另外由于不准备支持数值做为模板参数,只支持类型做为模板参数。

static Vector3 s_zero;

这一行声明了一个静态成员变量。idlcpp支持静态成员变量,静态成员函数,静态属性(实际上也是静态成员函数)。

nocode N x;

nocode N y;

nocode N z;

nocode N v[#3];

#{

union

{

  struct

  {

    N x,y,z;

  };

  N v[3];

};

#}

idlcpp 没有提供 union。好在可以通过nocode 和 #{#} 分别在生成的元数据描述代码和C++头文件提供各自的内容。

下面两行代码

export Vector3<float>;

export Vector3<double>;

用于生成元数据代码。idlcpp中通过这样的声明语句才会生成相应类型的元数据信息。

再下面两行代码

typedef Vector3<float> Vector3f;

typedef Vector3<double> Vector3d;

为模板类实例类型声明了类型别名。因为这两个类型名分别是::tutorial::Vector3<float> 和 ::tutorial::Vector3<double>,在脚本中使用不方便,有了类型别名之后就可以通过::tutorial::Vector3f和::tutorial::Vector3d来使用。

后面就是成员函数的实现代码,不在赘述。

编译后生成的Tutorial6.h的内容如下:

//DO NOT EDIT THIS FILE, it is generated by idlcpp
//http://www.idlcpp.org #pragma once namespace tutorial
{
template<typename N>
struct Vector3
{
public: Vector3();
Vector3(const Vector3& v);
Vector3(N a,N b,N c);
Vector3(const N* p);
N getLength();
N get_length();
N get_lengthSquare(); static Vector3<N> s_zero; union
{
struct
{
N x,y,z;
};
N v[];
}; }; typedef Vector3<float> Vector3f;
typedef Vector3<double> Vector3d; template<typename N>
Vector3<N> Vector3<N>::s_zero(, , ); template<typename N>
inline Vector3<N>::Vector3()
{
} template<typename N>
inline Vector3<N>::Vector3(const Vector3<N>& v) : x(v.x), y(v.y), z(v.z)
{} template<typename N>
inline Vector3<N>::Vector3(N a, N b, N c) : x(a), y(b), z(c)
{} template<typename N>
inline Vector3<N>::Vector3(const N* p) : x(p[]), y(p[]), z(p[])
{} template<typename N>
inline N Vector3<N>::getLength()
{
return N(sqrt(x * x + y * y + z * z));
} template<typename N>
inline N Vector3<N>::get_length()
{
return N(sqrt(x * x + y * y + z * z));
} template<typename N>
inline N Vector3<N>::get_lengthSquare()
{
return (x * x + y * y + z * z);
} }

idlcpp会为只读属性length和lengthSquare 生成对应的函数声明get_length和get_lengthSquare。

其他的内容,C++和idl基本上都是一样的。

然后是Tutorial6.cpp

#include "Tutorial6.h"
#include "Tutorial6.mh"
#include "Tutorial6.ic"
#include "Tutorial6.mc"

因为模板类的代码都写在头文件中了,所以Tutorial6.cpp只需要包含对应的四个文件即可。

最后看一下Tutorial6.lua的内容

v1 = paf.tutorial.Vector3f(,,);
v1.z = ;
print(v1.length._); v2 = paf.tutorial.Vector3d(,,);
v2.v[] = ;
print(v2:getLength()._);

编译执行,结果如下图:

C++混合编程之idlcpp教程Lua篇(8)的更多相关文章

  1. C++混合编程之idlcpp教程Lua篇(9)

    上一篇在这 C++混合编程之idlcpp教程Lua篇(8) 第一篇在这 C++混合编程之idlcpp教程(一) 与前面的工程相比,工程LuaTutorial7中除了四个文件LuaTutorial7.c ...

  2. C++混合编程之idlcpp教程Lua篇(7)

    上一篇在这 C++混合编程之idlcpp教程Lua篇(6) 第一篇在这 C++混合编程之idlcpp教程(一) 与LuaTutorial4工程相似,工程LuaTutorial5中,同样加入了四个文件: ...

  3. C++混合编程之idlcpp教程Lua篇(6)

    上一篇在这 C++混合编程之idlcpp教程Lua篇(5) 第一篇在这 C++混合编程之idlcpp教程(一) 工程LuaTutorial4中加入了四个文件:LuaTutorial4.cpp, Tut ...

  4. C++混合编程之idlcpp教程Lua篇(5)

    上一篇在这 C++混合编程之idlcpp教程Lua篇(4) 第一篇在这 C++混合编程之idlcpp教程(一) 与前面的工程相似,工程LuaTutorial3中,同样加入了三个文件:LuaTutori ...

  5. C++混合编程之idlcpp教程Lua篇(4)

    上一篇在这  C++混合编程之idlcpp教程Lua篇(3) 与前面的工程相似,工程LuaTutorial2中,同样加入了三个文件 LuaTutorial2.cpp, Tutorial2.i, tut ...

  6. C++混合编程之idlcpp教程Lua篇(3)

    上一篇 C++混合编程之idlcpp教程Lua篇(2) 是一个 hello world 的例子,仅仅涉及了静态函数的调用.这一篇会有新的内容. 与LuaTutorial0相似,工程LuaTutoria ...

  7. C++混合编程之idlcpp教程Lua篇(2)

    在上一篇 C++混合编程之idlcpp教程(一) 中介绍了 idlcpp 工具的使用.现在对 idlcpp 所带的示例教程进行讲解,这里针对的 Lua 语言的例子.首先看第一个示例程序 LuaTuto ...

  8. C++混合编程之idlcpp教程Python篇(7)

    上一篇在这 C++混合编程之idlcpp教程Python篇(6) 第一篇在这 C++混合编程之idlcpp教程(一) 与PythonTutorial4工程相似,工程PythonTutorial5中,同 ...

  9. C++混合编程之idlcpp教程Python篇(6)

    上一篇在这 C++混合编程之idlcpp教程Python篇(5) 第一篇在这 C++混合编程之idlcpp教程(一) 工程PythonTutorial4中加入了四个文件:PythonTutorial4 ...

随机推荐

  1. [刘阳Java]_快速搭建MyBatis环境_第2讲

    1.MyBatis的环境配置 导入MyBatis包, mybatis-3.2.8.jar 导入MySQL驱动包, mysql-connector-java-5.1.24-bin.jar 创建表的实体类 ...

  2. Kinect2在线重建(Tracking and Mapping)

    前言      个人理解错误的地方还请不吝赐教,转载请标明出处,内容如有改动更新,请看原博:http://www.cnblogs.com/hitcm/      如有任何问题,feel free to ...

  3. c# long转 datetime

    ; DateTime start = , , , , , , DateTimeKind.Utc); DateTime date = start.AddMilliseconds(time).ToLoca ...

  4. Android框架之AndroidAnnotations实战

    方案一: 下载 androidannotations-bundle-3.3.2.zip 方案二:   楼主选用开发环境:android studio 新建项目  修改app 下的build.gradl ...

  5. [翻译][erlang]cowboy路由模块使用

    Cowboy是基于Erlang实现的一个轻量级.快速.模块化的http web服务器. 本文官方原文:http://ninenines.eu/docs/en/cowboy/1.0/guide/rout ...

  6. DOCTYPE的详细图解

    之前有一次写代码的时候忘记写了<!DOCTYPE html> 导致样式的效果一直有点问题,查了很久才发现时候这个的锅.之后自己详细的来查找了DOCTYPE的作用. 在目前,基本上都是采用浏 ...

  7. .NET J2EE APP全局会话架构运用场景

    .NET J2EE APP全局会话架构运用场景, 全局会话运用拓扑图代码核心架构为.NET架构开发C#语言为主代码架构分为全局会话中心.ASP.NET会话节点..NET会话节点针对WCF服务器与APP ...

  8. 增强拉格朗日乘子法(Augmented Lagrange Method)

    增强拉格朗日乘子法的作用是用来解决等式约束下的优化问题, 假定需要求解的问题如下: minimize f(X) s.t.: h(X)=0 其中,f:Rn->R; h:Rn->Rm 朴素拉格 ...

  9. Number()数据类型转换

    Number() 如果是 Boolean 值, 和.如果是数字值,只是简单的传入和返回.如果是 .如果是 undefined,返回NaN. 如果是字符串,遵循下列规则:    如果字符串中只包含数字( ...

  10. ArcGIS Server 10 Java 版的Rest服务手动配置方法

    Java版的Manager中发布的服务默认只发布了该服务的SOAP接口,而REST接口需要用户在信息服务器,如Tomcat. Apache.WebLogic等中手工配置.由于在Java版的Server ...