PageRenderTime 8ms CodeModel.GetById 1ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Lib/octave/std_common.i

#
Swig | 72 lines | 57 code | 9 blank | 6 comment | 0 complexity | a20ecb92f22b6817c3c33fac26d856b7 MD5 | raw file
 1%include <std/std_except.i>
 2%include <octstdcommon.swg>
 3
 4
 5// Generate the traits for a 'primitive' type, such as 'double',
 6// for which the SWIG_AsVal and SWIG_From methods are already defined.
 7
 8%define %traits_ptypen(Type...)
 9  %fragment(SWIG_Traits_frag(Type),"header",
10	    fragment=SWIG_AsVal_frag(Type),
11	    fragment=SWIG_From_frag(Type),
12	    fragment="StdTraits") {
13namespace swig {
14  template <> struct traits<Type > {
15    typedef value_category category;
16    static const char* type_name() { return  #Type; }
17  };  
18  template <>  struct traits_asval<Type > {   
19    typedef Type value_type;
20    static int asval(octave_value obj, value_type *val) { 
21      return SWIG_AsVal(Type)(obj, val);
22    }
23  };
24  template <>  struct traits_from<Type > {
25    typedef Type value_type;
26    static octave_value from(const value_type& val) {
27      return SWIG_From(Type)(val);
28    }
29  };
30}
31}
32%enddef
33
34/* Traits for enums. This is bit of a sneaky trick needed because a generic template specialization of enums
35   is not possible (unless using template meta-programming which SWIG doesn't support because of the explicit
36   instantiations required using %template). The STL containers define the 'front' method and the typemap
37   below is used whenever the front method is wrapped returning an enum. This typemap simply picks up the
38   standard enum typemap, but additionally drags in a fragment containing the traits_asval and traits_from
39   required in the generated code for enums. */
40
41%define %traits_enum(Type...)
42  %fragment("SWIG_Traits_enum_"{Type},"header",
43	    fragment=SWIG_AsVal_frag(int),
44	    fragment=SWIG_From_frag(int),
45	    fragment="StdTraits") {
46namespace swig {
47  template <>  struct traits_asval<Type > {   
48    typedef Type value_type;
49    static int asval(octave_value obj, value_type *val) { 
50      return SWIG_AsVal(int)(obj, (int *)val);
51    }
52  };
53  template <>  struct traits_from<Type > {
54    typedef Type value_type;
55    static octave_value from(const value_type& val) {
56      return SWIG_From(int)((int)val);
57    }
58  };
59}
60}
61%typemap(out, fragment="SWIG_Traits_enum_"{Type}) const enum SWIGTYPE& front %{$typemap(out, const enum SWIGTYPE&)%}
62%enddef
63
64
65%include <std/std_common.i>
66
67//
68// Generates the traits for all the known primitive
69// C++ types (int, double, ...)
70//
71%apply_cpptypes(%traits_ptypen);
72