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

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

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

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

  1. #import "../../paf/src/pafcore/typedef.i"
  2.  
  3. namespace tutorial
  4. {
      template<N>
  5. struct Vector3
  6. {
  7. Vector3();
  8. Vector3(const Vector3& v);
  9. Vector3(N a, N b, N c);
  10. Vector3(const N* p);
  11. N getLength();
  12. N length get;
  13. N lengthSquare get;
  14.  
  15. static Vector3<N> s_zero;
  16.  
  17. nocode N x;
  18. nocode N y;
  19. nocode N z;
  20. nocode N v[#];
  21.  
  22. #{
  23. union
  24. {
  25. struct
  26. {
  27. N x,y,z;
  28. };
  29. N v[];
  30. };
  31. #}
  32. };
  33.  
  34. export Vector3<float>;
  35. export Vector3<double>;
  36. typedef Vector3<float> Vector3f;
  37. typedef Vector3<double> Vector3d;
  38.  
  39. #{
  40. template<typename N>
  41. Vector3<N> Vector3<N>::s_zero(, , );
  42.  
  43. template<typename N>
  44. inline Vector3<N>::Vector3()
  45. {
  46. }
  47.  
  48. template<typename N>
  49. inline Vector3<N>::Vector3(const Vector3<N>& v) : x(v.x), y(v.y), z(v.z)
  50. {}
  51.  
  52. template<typename N>
  53. inline Vector3<N>::Vector3(N a, N b, N c) : x(a), y(b), z(c)
  54. {}
  55.  
  56. template<typename N>
  57. inline Vector3<N>::Vector3(const N* p) : x(p[]), y(p[]), z(p[])
  58. {}
  59.  
  60. template<typename N>
  61. inline N Vector3<N>::getLength()
  62. {
  63. return N(sqrt(x * x + y * y + z * z));
  64. }
  65.  
  66. template<typename N>
  67. inline N Vector3<N>::get_length()
  68. {
  69. return N(sqrt(x * x + y * y + z * z));
  70. }
  71.  
  72. template<typename N>
  73. inline N Vector3<N>::get_lengthSquare()
  74. {
  75. return (x * x + y * y + z * z);
  76. }
  77. #}
  78. }

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的内容如下:

  1. //DO NOT EDIT THIS FILE, it is generated by idlcpp
  2. //http://www.idlcpp.org
  3.  
  4. #pragma once
  5.  
  6. namespace tutorial
  7. {
  8. template<typename N>
  9. struct Vector3
  10. {
  11. public:
  12.  
  13. Vector3();
  14. Vector3(const Vector3& v);
  15. Vector3(N a,N b,N c);
  16. Vector3(const N* p);
  17. N getLength();
  18. N get_length();
  19. N get_lengthSquare();
  20.  
  21. static Vector3<N> s_zero;
  22.  
  23. union
  24. {
  25. struct
  26. {
  27. N x,y,z;
  28. };
  29. N v[];
  30. };
  31.  
  32. };
  33.  
  34. typedef Vector3<float> Vector3f;
  35. typedef Vector3<double> Vector3d;
  36.  
  37. template<typename N>
  38. Vector3<N> Vector3<N>::s_zero(, , );
  39.  
  40. template<typename N>
  41. inline Vector3<N>::Vector3()
  42. {
  43. }
  44.  
  45. template<typename N>
  46. inline Vector3<N>::Vector3(const Vector3<N>& v) : x(v.x), y(v.y), z(v.z)
  47. {}
  48.  
  49. template<typename N>
  50. inline Vector3<N>::Vector3(N a, N b, N c) : x(a), y(b), z(c)
  51. {}
  52.  
  53. template<typename N>
  54. inline Vector3<N>::Vector3(const N* p) : x(p[]), y(p[]), z(p[])
  55. {}
  56.  
  57. template<typename N>
  58. inline N Vector3<N>::getLength()
  59. {
  60. return N(sqrt(x * x + y * y + z * z));
  61. }
  62.  
  63. template<typename N>
  64. inline N Vector3<N>::get_length()
  65. {
  66. return N(sqrt(x * x + y * y + z * z));
  67. }
  68.  
  69. template<typename N>
  70. inline N Vector3<N>::get_lengthSquare()
  71. {
  72. return (x * x + y * y + z * z);
  73. }
  74.  
  75. }

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

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

然后是Tutorial6.cpp

  1. #include "Tutorial6.h"
  2. #include "Tutorial6.mh"
  3. #include "Tutorial6.ic"
  4. #include "Tutorial6.mc"

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

最后看一下Tutorial6.lua的内容

  1. v1 = paf.tutorial.Vector3f(,,);
  2. v1.z = ;
  3. print(v1.length._);
  4.  
  5. v2 = paf.tutorial.Vector3d(,,);
  6. v2.v[] = ;
  7. 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. javascript函数声明方式

    javascript中函数的声明有三种方式: 最常见的函数声明: fun();//可以调用,因为这种声明方式会被浏览器优先加载. function fun() { alert("声明式的函数 ...

  2. PHP从mysqli中获取的资源$result是不是不能while($row = $result->fetch_assoc())这样两次?【坑】

    PHP从mysqli中获取的资源$result是不是不能while($row = $result->fetch_assoc())这样两次? 因为我这样做,结果后面的查询结果就无法显示了,目前尚不 ...

  3. HTML超标记语言

     Html超文本标记语言,负责描绘Web世界的骨架. 〇.工具 http;//www.w3cchool.com.cn 一.Tim Bemers Lee 万维网之父: Html设计者: W3C创始人: ...

  4. NC nc5.x笔记(编辑中)

    一.设置卡片界面下 金额字段负数为红色! /** * 设置卡片界面下 金额字段负数为红色! */ private void repaintBodyMoneyColor(){ if(!isListPan ...

  5. 安装pillow错误的解决方案

    错误信息: ValueError: jpeg is required unless explicitly disabled using --disable-jpeg, aborting        ...

  6. dedecms内容管理系统学习

    在复习完基础知识和学习了tp3.2之后,我们开始学习一些开源的产品,如dedecms:

  7. [转]Snappy压缩库安装和使用之一

    Snappy压缩库安装和使用之一 原文地址:http://blog.csdn.net/luo6620378xu/article/details/8521223 近日需要在毕业设计中引入一个压缩库,要求 ...

  8. 先学习下一些基础的js和xpath语法

    这两个方法到底是在做什么呢?其实就是克隆了当前指令的节点,并生成子作用域.克隆的节点由transclude定义,如果你的属性是true,则克隆的是指令模板中的ng-transclude所在的DOM节点 ...

  9. C语言打印最长字符串

    编程在一个已知的字符串中找最长单词,假定字符串中只含字母和空格,空格用来分隔不同单词. ]; printf("请输入字符串:"); fgets(p, , stdin); ; ; ; ...

  10. k次出现与一次出现的数字

    原始的题目是这样的: Single Number II Given an array of integers, every element appears three times except for ...