PageRenderTime 4ms CodeModel.GetById 1ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Swig | 110 lines | 84 code | 26 blank | 0 comment | 0 complexity | af5d717fdbaf54a8855af257d34ef6c7 MD5 | raw file
  1// Tests classes passed by value, pointer and reference
  2// Note: C# module has a large runtime test
  3
  4#pragma SWIG nowarn=SWIGWARN_TYPEMAP_THREAD_UNSAFE,SWIGWARN_TYPEMAP_DIRECTOROUT_PTR
  5
  6%module(directors="1") director_classes
  7
  8%feature("director") Base;
  9%feature("director") Derived;
 10
 11%include "std_string.i"
 12
 13%{
 14#if defined(__SUNPRO_CC)
 15#pragma error_messages (off, hidevf)
 16#endif
 17%}
 18
 19%inline %{
 20#include <cstdio>
 21#include <iostream>
 22
 23
 24// Use for debugging
 25bool PrintDebug = false;
 26
 27
 28struct DoubleHolder
 29{
 30  DoubleHolder(double v = 0.0) : val(v) {}
 31  double val;
 32};
 33
 34class Base {
 35protected:
 36  double m_dd;
 37public:
 38
 39  Base(double dd) : m_dd(dd) {}
 40  virtual ~Base() {}
 41
 42  virtual DoubleHolder Val(DoubleHolder x) { if (PrintDebug) std::cout << "Base - Val(" << x.val << ")" << std::endl; return x; }
 43  virtual DoubleHolder& Ref(DoubleHolder& x) { if (PrintDebug) std::cout << "Base - Ref(" << x.val << ")" << std::endl; return x; }
 44  virtual DoubleHolder* Ptr(DoubleHolder* x) { if (PrintDebug) std::cout << "Base - Ptr(" << x->val << ")" << std::endl; return x; }
 45
 46  virtual std::string FullyOverloaded(int x) { if (PrintDebug) std::cout << "Base - FullyOverloaded(int " << x << ")" << std::endl; return "Base::FullyOverloaded(int)"; }
 47  virtual std::string FullyOverloaded(bool x) { if (PrintDebug) std::cout << "Base - FullyOverloaded(bool " << x << ")" << std::endl; return "Base::FullyOverloaded(bool)"; }
 48
 49  virtual std::string SemiOverloaded(int x) { if (PrintDebug) std::cout << "Base - SemiOverloaded(int " << x << ")" << std::endl; return "Base::SemiOverloaded(int)"; }
 50  virtual std::string SemiOverloaded(bool x) { if (PrintDebug) std::cout << "Base - SemiOverloaded(bool " << x << ")" << std::endl; return "Base::SemiOverloaded(bool)"; }
 51
 52  virtual std::string DefaultParms(int x, double y = 1.1) {
 53    if (PrintDebug) std::cout << "Base - DefaultParms(" << x << ", " << y << ")" << std::endl;
 54    std::string ret("Base::DefaultParms(int");
 55    if (y!=1.1)
 56      ret = ret + std::string(", double");
 57    ret = ret + std::string(")");
 58    return ret;
 59  }
 60};
 61
 62class Derived : public Base {
 63public:
 64  Derived(double dd) : Base(dd) {}
 65  virtual ~Derived() {}
 66
 67  virtual DoubleHolder Val(DoubleHolder x) { if (PrintDebug) std::cout << "Derived - Val(" << x.val << ")" << std::endl; return x; }
 68  virtual DoubleHolder& Ref(DoubleHolder& x) { if (PrintDebug) std::cout << "Derived - Ref(" << x.val << ")" << std::endl; return x; }
 69  virtual DoubleHolder* Ptr(DoubleHolder* x) { if (PrintDebug) std::cout << "Derived - Ptr(" << x->val << ")" << std::endl; return x; }
 70
 71  virtual std::string FullyOverloaded(int x) { if (PrintDebug) std::cout << "Derived - FullyOverloaded(int " << x << ")" << std::endl; return "Derived::FullyOverloaded(int)"; }
 72  virtual std::string FullyOverloaded(bool x) { if (PrintDebug) std::cout << "Derived - FullyOverloaded(bool " << x << ")" << std::endl; return "Derived::FullyOverloaded(bool)"; }
 73
 74  virtual std::string SemiOverloaded(int x) { if (PrintDebug) std::cout << "Derived - SemiOverloaded(int " << x << ")" << std::endl; return "Derived::SemiOverloaded(int)"; }
 75  // No SemiOverloaded(bool x)
 76
 77  virtual std::string DefaultParms(int x, double y = 1.1) { 
 78    if (PrintDebug) std::cout << "Derived - DefaultParms(" << x << ", " << y << ")" << std::endl;
 79    std::string ret("Derived::DefaultParms(int");
 80    if (y!=1.1)
 81      ret = ret + std::string(", double");
 82    ret = ret + std::string(")");
 83    return ret;
 84  }
 85};
 86
 87
 88class Caller {
 89private:
 90  Base *m_base;
 91  void delBase() { delete m_base; m_base = 0; }
 92public:
 93  Caller(): m_base(0) {}
 94  ~Caller() { delBase(); }
 95  void set(Base *b) { delBase(); m_base = b; }
 96  void reset() { m_base = 0; }
 97
 98  DoubleHolder ValCall(DoubleHolder x) { return m_base->Val(x); }
 99  DoubleHolder& RefCall(DoubleHolder& x) { return m_base->Ref(x); }
100  DoubleHolder* PtrCall(DoubleHolder* x) { return m_base->Ptr(x); }
101  std::string FullyOverloadedCall(int x) { return m_base->FullyOverloaded(x); }
102  std::string FullyOverloadedCall(bool x) { return m_base->FullyOverloaded(x); }
103  std::string SemiOverloadedCall(int x) { return m_base->SemiOverloaded(x); }
104  std::string SemiOverloadedCall(bool x) { return m_base->SemiOverloaded(x); }
105  std::string DefaultParmsCall(int x) { return m_base->DefaultParms(x); }
106  std::string DefaultParmsCall(int x, double y) { return m_base->DefaultParms(x, y); }
107};
108
109%}
110