PageRenderTime 8ms CodeModel.GetById 2ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 1ms

/trunk/Lib/lua/_std_common.i

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