/trunk/Examples/test-suite/cpp_namespace.i

# · Swig · 105 lines · 78 code · 26 blank · 1 comment · 0 complexity · 28c898dd9d97e95552060e3c4ab928b5 MD5 · raw file

  1. // C++ namespace tests
  2. %module cpp_namespace
  3. %inline %{
  4. typedef int Bad;
  5. /* A very basic namespace */
  6. namespace example {
  7. typedef char *Bad;
  8. int fact(int n) {
  9. if (n <= 0) return 1;
  10. else return n*fact(n-1);
  11. }
  12. int Foo = 42;
  13. class Test {
  14. public:
  15. Test() { }
  16. ~Test() { }
  17. char *method() {
  18. return (char *) "Test::method";
  19. }
  20. };
  21. typedef Test *TestPtr;
  22. void weird(Bad x, ::Bad y) { }
  23. }
  24. char *do_method(example::TestPtr t) {
  25. return t->method();
  26. }
  27. namespace ex = example;
  28. char *do_method2(ex::TestPtr t) {
  29. return t->method();
  30. }
  31. %}
  32. // Some more complicated namespace examples
  33. %inline %{
  34. namespace Foo {
  35. typedef int Integer;
  36. class Test2 {
  37. public:
  38. virtual ~Test2() { }
  39. virtual char *method() {
  40. return (char *) "Test2::method";
  41. }
  42. };
  43. typedef Test2 *Test2Ptr;
  44. }
  45. namespace Foo2 {
  46. using Foo::Integer;
  47. using Foo::Test2;
  48. class Test3 : public Test2 {
  49. public:
  50. virtual char *method() {
  51. return (char *) "Test3::method";
  52. }
  53. };
  54. typedef Test3 *Test3Ptr;
  55. typedef Test3 Test3Alt;
  56. }
  57. namespace Foo3 {
  58. using namespace Foo2;
  59. class Test4 : public Test3 {
  60. public:
  61. virtual char *method() {
  62. return (char *) "Test4::method";
  63. }
  64. };
  65. Integer foo3(Integer x) { return x; }
  66. typedef Test4 *Test4Ptr;
  67. }
  68. using Foo2::Test3Alt;
  69. using Foo3::Integer;
  70. class Test5 : public Test3Alt {
  71. public:
  72. virtual char *method() {
  73. return (char *) "Test5::method";
  74. }
  75. };
  76. char *do_method3(Foo::Test2 *t, Integer x) {
  77. return t->method();
  78. }
  79. %}