Advertisement
vipulraheja

Untitled

Jun 14th, 2011
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.61 KB | None | 0 0
  1. /* ----------------------------------------------------------------------------
  2. // File: pyossim.i
  3. // License: See top level LICENSE.txt file
  4. // Author: Vipul Raheja
  5. // Description: SWIG Module to build ossim-python bindings
  6. //--------------------------------------------------------------------------- */
  7.  
  8. /* Start pyOSSIM Wrappers */
  9. %module pyossim
  10. %{
  11. #include <ossimjni/Constants.h>
  12. #include <ossimjni/Info.h>
  13. #include <ossimjni/Init.h>
  14. #include <ossimjni/SingleImageChain.h>
  15.  
  16. #include <exception>
  17. %}
  18.  
  19.  
  20. /* Handling the std::exception */
  21. %include "exception.i"
  22. %exception
  23. {
  24.         try
  25.         {
  26.                 $action
  27.         }
  28.         catch()
  29.         {
  30.                 SWIG_exception(SWIG_RuntimeError, "Runtime Error");
  31.         }
  32. }
  33.  
  34. /* This tells SWIG to treat char ** as a special case */
  35. %typemap(in) char **
  36. {
  37.         /* Check if input is a list */
  38.         if (PyList_Check($input))
  39.         {
  40.                 int size = PyList_Size($input);
  41.                 int i = 0;
  42.                
  43.                 /* Allocate memory */
  44.                 $1 = (char **) malloc((size+1)*sizeof(char *));
  45.                
  46.                 for (i = 0; i < size; i++)
  47.                 {
  48.                         PyObject *o = PyList_GetItem($input,i);
  49.                        
  50.                         if (PyString_Check(o))
  51.                         {
  52.                                 $1[i] = PyString_AsString(PyList_GetItem($input,i));
  53.                         }
  54.  
  55.                         else
  56.                         {
  57.                                 PyErr_SetString(PyExc_TypeError,"List must contain strings");
  58.                                 free($1);
  59.                                 return NULL;
  60.                         }
  61.                 }
  62.                 $1[i] = 0;
  63.         }
  64.        
  65.         else
  66.         {
  67.                 PyErr_SetString(PyExc_TypeError,"not a list");
  68.                 return NULL;
  69.         }
  70. }
  71.  
  72. /* To return char** from a C function as Python List */
  73. %typemap(out) char**
  74. {
  75.         int i;
  76.         int len = 0;
  77.  
  78.         while ($1[len])
  79.         {
  80.                 len++;
  81.         }
  82.  
  83.         $result = PyList_New(len);
  84.        
  85.         for (i = 0; i < len; i++)
  86.         {
  87.                 PyList_SetItem($result, i, PyString_FromString($1[i]));
  88.         }
  89. }
  90.  
  91.  
  92. /* Include library header files */
  93. %include <ossimjni/Constants.h>
  94. %include <ossimjni/Info.h>
  95. %include <ossimjni/Init.h>
  96. %include <ossimjni/SingleImageChain.h>
  97.  
  98. %include "typemaps.i"
  99.  
  100.  
  101. /* Typemaps to convert values from Python to C++ */
  102. %typemap(in) ossimjni_int8
  103. {
  104.         $1 = PyString_AsString($input);
  105. }
  106.  
  107. %typemap(in) ossimjni_int32
  108. {
  109.         $1 = PyInt_AsLong($input);
  110. }
  111.  
  112. %typemap(in) ossimjni_int64
  113. {
  114.         $1 = PyLong_AsLongLong($input);
  115. }
  116.  
  117. %typemap(in) ossimjni_uint64
  118. {
  119.         $1 = PyLong_AsUnsignedLongLong
  120. }
  121.  
  122. %typemap(in) ossimjni_float64
  123. {
  124.         $1 = PyFloat_AsDouble($input);
  125. }
  126.  
  127.  
  128. /* Typemaps to convert from C++ to Python */
  129. %typemap(out) ossimjni_int32
  130. {
  131.         $result = PyInt_FromLong((long) $1);
  132. }
  133.  
  134. %typemap(out) ossimjni_int64
  135. {
  136.         $result = PyLong_FromLongLong($1);
  137. }
  138.  
  139. %typemap(out) ossimjni_uint64
  140. {
  141.         $result = PyLong_FromUnsignedLongLong($1);
  142. }
  143.  
  144. %typemap(out) ossimjni_float64
  145. {
  146.         $result = PyFloat_FromDouble($1);
  147. }
  148.  
  149.  
  150. /* Applying the typemaps to other basic primitives */
  151. %apply ossimjni_int8 { ossimjni_sint8, ossimjni_uint8 };
  152. %apply ossimjni_int32 { ossimjni_int16, ossimjni_uint16, ossimjni_uint16, ossimjni_uint32, ossimjni_sint32 };
  153. %apply ossimjni_int64 { ossimjni_sint64 };
  154. %apply ossimjni_float64 { ossimjni_float32 };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement