PageRenderTime 39ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Lib/lua/_std_common.i

#
Swig | 93 lines | 61 code | 6 blank | 26 comment | 0 complexity | 1b6f517289f49c284306e02b55658ee4 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. /* -----------------------------------------------------------------------------
  2. * _std_common.i
  3. *
  4. * std::helpers for LUA
  5. * ----------------------------------------------------------------------------- */
  6. %include <std_except.i> // the general exepctions
  7. /*
  8. The basic idea here, is instead of trying to feed SWIG all the
  9. horribly templated STL code, to give it a neatened version.
  10. These %defines cover some of the more common methods
  11. so the class declarations become just a set of %defines
  12. */
  13. /* #define for basic container features
  14. note: I allow front(), back() & pop_back() to throw execptions
  15. upon empty containers, rather than coredump
  16. (as we have'nt defined the methods, we can use %extend to add with
  17. new features)
  18. */
  19. %define %STD_CONTAINER_METHODS(CLASS,T)
  20. public:
  21. CLASS();
  22. CLASS(const CLASS&);
  23. unsigned int size() const;
  24. unsigned int max_size() const;
  25. bool empty() const;
  26. void clear();
  27. %extend { // the extra stuff which must be checked
  28. T front()const throw (std::out_of_range){ // only read front & back
  29. if (self->empty())
  30. throw std::out_of_range("in "#CLASS"::front()");
  31. return self->front();
  32. }
  33. T back()const throw (std::out_of_range){ // not write to them
  34. if (self->empty())
  35. throw std::out_of_range("in "#CLASS"::back()");
  36. return self->back();
  37. }
  38. }
  39. %enddef
  40. /* push/pop for front/back
  41. also note: front & back are read only methods, not used for writing
  42. */
  43. %define %STD_FRONT_ACCESS_METHODS(CLASS,T)
  44. public:
  45. void push_front(const T& val);
  46. %extend { // must check this
  47. void pop_front() throw (std::out_of_range){
  48. if (self->empty())
  49. throw std::out_of_range("in "#CLASS"::pop_front()");
  50. self->pop_back();
  51. }
  52. }
  53. %enddef
  54. %define %STD_BACK_ACCESS_METHODS(CLASS,T)
  55. public:
  56. void push_back(const T& val);
  57. %extend { // must check this
  58. void pop_back() throw (std::out_of_range){
  59. if (self->empty())
  60. throw std::out_of_range("in "#CLASS"::pop_back()");
  61. self->pop_back();
  62. }
  63. }
  64. %enddef
  65. /*
  66. Random access methods
  67. */
  68. %define %STD_RANDOM_ACCESS_METHODS(CLASS,T)
  69. %extend // this is a extra bit of SWIG code
  70. {
  71. // [] is replaced by __getitem__ & __setitem__
  72. // simply throws a string, which causes a lua error
  73. T __getitem__(unsigned int idx) throw (std::out_of_range){
  74. if (idx>=self->size())
  75. throw std::out_of_range("in "#CLASS"::__getitem__()");
  76. return (*self)[idx];
  77. }
  78. void __setitem__(unsigned int idx,const T& val) throw (std::out_of_range){
  79. if (idx>=self->size())
  80. throw std::out_of_range("in "#CLASS"::__setitem__()");
  81. (*self)[idx]=val;
  82. }
  83. };
  84. %enddef