Add simple PySwigObject to handle the C/C++ instance pointers,

instead of using PyCObject or plain strings.

The new PySwigObject is even safer than PyCObject, and
more friendly than plain strings:

now you can do

  print a.this
  <Swig Object at _00691608_p_A>

  print str(a.this)
  _00691608_p_A

  print long(a.this)
  135686400

  print "%s 0x%x" % (a.this, a.this)
  _00691608_p_A 0x8166900


the last one is very useful when debugging the C/C++ side, since
is the pointer value you will usually get from the debugger.

Also, if you have some old code that uses the string representation
"_00691608_p_A", you can use it now again by calling str(ptr), or
maybe nothing special by just calling PyString_AsString(..).

This change is mainly for nostalgic swig users that miss the
string representation, but also allows to say again

  if a.this == b.this:
    return "a is b"

and well, since the change were really simple, maybe in the future
we will be able to do

    next = a.this + 1

or add native python iteration over native C/C++ arrays, ie, no
need to create/copy new tuples when returning and array or vector.


git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@6759 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Marcelo Matus 2004-11-19 20:40:20 +00:00
commit 0881d3c861
6 changed files with 384 additions and 44 deletions

View file

@ -30,12 +30,19 @@
#endif
/*
You can use this macro for creating a static or dynamic library.
Swig just needs to use it as 'static'
You can use the SWIGRUNTIME and SWIGRUNTIMEINLINE macros for
creating a static or dynamic library from the swig runtime code.
In 99.9% of the cases, swig just needs to declare them as 'static'.
But only do this if is strictly necessary, ie, if you have problems
with your compiler or so.
*/
#ifndef SWIGRUNTIME
#define SWIGRUNTIME static
#endif
#ifndef SWIGRUNTIMEINLINE
#define SWIGRUNTIMEINLINE static SWIGINLINE
#endif
#ifdef __cplusplus
extern "C" {
@ -172,10 +179,9 @@ SWIG_TypeCheck(char *c, swig_type_info *ty) {
/*
Cast a pointer up an inheritance hierarchy
*/
SWIGRUNTIME SWIGINLINE void *
SWIGRUNTIMEINLINE void *
SWIG_TypeCast(swig_type_info *ty, void *ptr) {
if ((!ty) || (!ty->converter)) return ptr;
return (*ty->converter)(ptr);
return ((!ty) || (!ty->converter)) ? ptr : (*ty->converter)(ptr);
}
/*
@ -195,7 +201,7 @@ SWIG_TypeDynamicCast(swig_type_info *ty, void **ptr) {
/*
Return the name associated with this type
*/
SWIGRUNTIME SWIGINLINE const char *
SWIGRUNTIMEINLINE const char *
SWIG_TypeName(const swig_type_info *ty) {
return ty->name;
}
@ -327,6 +333,20 @@ SWIG_PropagateClientDataTL(swig_type_info *tl, swig_type_info *type) {
}
}
/*
Pack 'void *' into a string buffer.
*/
SWIGRUNTIME char *
SWIG_PackVoidPtr(char *buff, void *ptr, const char *name, size_t bsz) {
char *r = buff;
if ((2*sizeof(void *) + 2) > bsz) return 0;
*(r++) = '_';
r = SWIG_PackData(r,&ptr,sizeof(void *));
if (strlen(name) + 1 > (bsz - (r - buff))) return 0;
strcpy(r,name);
return buff;
}
#ifdef __cplusplus
}
#endif