PageRenderTime 40ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Examples/go/template/index.html

#
HTML | 113 lines | 89 code | 24 blank | 0 comment | 0 complexity | 96aca3733e4b835b92c1f732c9867c42 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. <html>
  2. <head>
  3. <title>SWIG:Examples:go:template</title>
  4. </head>
  5. <body bgcolor="#ffffff">
  6. <tt>SWIG/Examples/go/template/</tt>
  7. <hr>
  8. <H2>C++ template support</H2>
  9. <p>
  10. This example illustrates how C++ templates can be used from Go using
  11. SWIG.
  12. <h2>The C++ Code</h2>
  13. Lets take a templated function and a templated class as follows:
  14. <blockquote>
  15. <pre>
  16. /* File : example.h */
  17. // Some template definitions
  18. template<class T> T max(T a, T b) { return a&gt;b ? a : b; }
  19. template<class T> class vector {
  20. T *v;
  21. int sz;
  22. public:
  23. vector(int _sz) {
  24. v = new T[_sz];
  25. sz = _sz;
  26. }
  27. T &amp;get(int index) {
  28. return v[index];
  29. }
  30. void set(int index, T &amp;val) {
  31. v[index] = val;
  32. }
  33. #ifdef SWIG
  34. %addmethods {
  35. T getitem(int index) {
  36. return self-&gt;get(index);
  37. }
  38. void setitem(int index, T val) {
  39. self-&gt;set(index,val);
  40. }
  41. }
  42. #endif
  43. };
  44. </pre>
  45. </blockquote>
  46. The %addmethods is used for a neater interface from Go as the
  47. functions <tt>get</tt> and <tt>set</tt> use C++ references to
  48. primitive types. These are tricky to use from Go as they end up as
  49. pointers, which only work when the C++ and Go types correspond
  50. precisely.
  51. <h2>The SWIG interface</h2>
  52. A simple SWIG interface for this can be built by simply grabbing the
  53. header file like this:
  54. <blockquote>
  55. <pre>
  56. /* File : example.i */
  57. %module example
  58. %{
  59. #include "example.h"
  60. %}
  61. /* Let's just grab the original header file here */
  62. %include "example.h"
  63. /* Now instantiate some specific template declarations */
  64. %template(maxint) max<int>;
  65. %template(maxdouble) max<double>;
  66. %template(vecint) vector<int>;
  67. %template(vecdouble) vector<double>;
  68. </pre>
  69. </blockquote>
  70. Note that SWIG parses the templated function <tt>max</tt> and
  71. templated class <tt>vector</tt> and so knows about them. However to
  72. generate code for use from Go, SWIG has to be told which class/type to
  73. use as the template parameter. The SWIG directive %template is used
  74. for this.
  75. <h2>A sample Go program</h2>
  76. Click <a href="runme.go">here</a> to see a Go program that calls the
  77. C++ functions from Go.
  78. <h2>Notes</h2> Use templated classes just like you would any other
  79. SWIG generated Go class. Use the classnames specified by the %template
  80. directive.
  81. <blockquote>
  82. <pre>
  83. vecdouble dv = new vecdouble(1000);
  84. dv.setitem(i, 12.34));
  85. </pre>
  86. </blockquote>
  87. <hr>
  88. </body>
  89. </html>