William S Fulton | 2 Jan 2007 21:41
Picon
Favicon
Gravatar

Re: How to add custom code into selected methods of proxy classes?

The "shadow" feature is on my list of things to do. Full control of the
code can be achieved by using the javaout typemap, as per the examples
in previous email, so there is no for these other features. For your
purposes, the "shadow" feature wouldn't give you anything extra over the
javaout typemap though. The only reason to add in the "shadow" feature
would be to completely modify the method signature (which you don't need).

William

Andras Varga wrote:
> Hi William,
> 
> Thanks for the answer! I didn't know that typemap matches the
> parameter name too. But this is not that useful for me, because in the
> .i file I'm %including the verbatim headers of the class library I'm
> wrapping (plus some %rename and %ignore in the .i file), and the
> parameter names in there are pretty much uniform for all methods (i.e.
> don't depend on whether disown() is needed or not). OTOH I don't like
> the idea of %ignoring the whole class then manually defining it again
> in the .i file with different param names either...
> 
> So for now I've resorted to post-processing the generated Java files
> with a Perl script. The input of the Perl script is basically
> equivalent to a bunch of the following fictitious %feature directives:
> 
> %feature("javadisown", args="obj")  FooContainer::insert;    // disown
> the obj parameter
> 
> Which would be a really nice SWIG feature... But alternatively, I
> could get by with a %feature that would let me inject some code into
(Continue reading)

Nathan Shair | 2 Jan 2007 22:41

Namespace and Swig

Hi,

 

I am getting the following errors when compiling a .h file that has a namespace in it. (The interface file includes the .h)

 

PythonInterfaceToC_wrap.c(17239) : error C2882: 'nDIR' : illegal use of namespace identifier in expression

PythonInterfaceToC_wrap.c(17239) : error C2059: syntax error : 'namespace'

PythonInterfaceToC_wrap.c(17251) : error C2882: 'nDIR' : illegal use of namespace identifier in expression

 

 

I read online that the above error messages suggests that the name of the namespace is being used in an expression. However, the code is generated by SWIG, so I’m not sure how I can modify it to not illegally use the name of the namespace. I am using SWIG 1.3.31 and Python 2.4.     

 

 

Example Namespace

 

namespace nDIR

{

                static const char *XIDLL = "dll";

                // (more code here)

};

 

Example of Generated Code (the lines with errors)

 

nDIR = *((namespace *)(argp));

 

pyobj = SWIG_NewPointerObj(SWIG_as_voidptr(&nDIR), SWIGTYPE_p_namespace,  0 );

 

 

Has anyone else encountered this problem? How can I fix this problem?



Thanks

_____________________
Nathan Shair
Software Developer
XI Technologies Inc.

Direct:        (403) 296-1339
XI Support: (403) 296-0077
www.xitechnologies.com
"enhanced energy information"

 

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user
David Beazley | 2 Jan 2007 22:43

Re: Namespace and Swig


On Jan 2, 2007, at 3:41 PM, Nathan Shair wrote:

> Hi,
>
>
>
> I am getting the following errors when compiling a .h file that has  
> a namespace in it. (The interface file includes the .h)
>
>
>
> PythonInterfaceToC_wrap.c(17239) : error C2882: 'nDIR' : illegal  
> use of namespace identifier in expression
>
> PythonInterfaceToC_wrap.c(17239) : error C2059: syntax error :  
> 'namespace'
>
> PythonInterfaceToC_wrap.c(17251) : error C2882: 'nDIR' : illegal  
> use of namespace identifier in expression
>
>
>
>
>
> I read online that the above error messages suggests that the name  
> of the namespace is being used in an expression. However, the code  
> is generated by SWIG, so I’m not sure how I can modify it to not  
> illegally use the name of the namespace. I am using SWIG 1.3.31 and  
> Python 2.4.
>
>
>
>
>
> Example Namespace
>
>
>
> namespace nDIR
>
> {
>
>                 static const char *XIDLL = "dll";
>
>                 // (more code here)
>
> };
>
>
>
> Example of Generated Code (the lines with errors)
>
>
>
> nDIR = *((namespace *)(argp));
>
>
>
> pyobj = SWIG_NewPointerObj(SWIG_as_voidptr(&nDIR),  
> SWIGTYPE_p_namespace,  0 );
>
>
>
>
>
> Has anyone else encountered this problem? How can I fix this problem?

Are you running SWIG with the -c++ option?

-Dave

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
Umberto Nicoletti | 3 Jan 2007 08:46
Picon
Gravatar

Re: How to add custom code into selected methods of proxy classes?

On 12/30/06, Andras Varga <avarga71 <at> gmail.com> wrote:
> I'm wrapping C++ code into Java, and I'm trying to give pointer
> ownership to wrapped code (like Petr Novotny recently). This is needed with
> insertion methods of wrapped container objects:
>
>    Foo foo = new Foo();  // wrapped object
>    FooContainer container = new FooContainer(); // wrapped object
>    container.insert(foo);  // here, I want foo's swigMemOwn to be changed to
> false

Hi,
I solved the same problem by using %javacode to inject a wrapper
method that disowns the object and then invokes the real insert. I
renamed the real insert to something like doInsert and I will
eventually make private.

You should end up with something like this:

%typemap(javacode) FooContainer %{
     public void insert(FooObject foo) {
       foo.swigCmemOwn=false;
       doInsert(foo)
     }

%}

HTH,
Umberto

>
> I am willing to manually annotate FooContainer.insert() and other such
> methods in
> the SWIG interface file, but so far I found no solution.
>
> I recognize there isn't a direct way to do it, i.e. there isn't anything like
>    %disown_argument("1")  FooContainer.insert(Foo);
>
> Currently I would be happy with any means that would let me
> inject some custom code into the generated Java insert() method to take care
> of that. (I would insert a foo.disown() call or something like that).
>
> So my question is:
>
>         *** Is there a way to insert custom code into selected Java wrapper
> methods? ***
>
> Typemaps are no good here, because they would apply to all methods taking
> Foo, which is not what I need.
>
> Features (%feature) look like they were designed for purposes like
> this, but adding a custom %feature had no effect. I tried several
> variants of the Object::allocate example in the
> manual ("11.3 Features and the %feature directive"), but it never caused any
> change in the generated code, e.g. after
>
>      %feature("test1") FooContainer::insert   { $action BUBU }
>
> BUBU didn't appear anywhere in the generated files. Am I doing something
> wrong?
>
> Any help is appreciated.
> Andras
>
> -------------------------------------------------------------------------
> Take Surveys. Earn Cash. Influence the Future of IT
> Join SourceForge.net's Techsay panel and you'll get the chance to share your
> opinions on IT & business topics through brief surveys - and earn cash
> http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
> _______________________________________________
> Swig-user mailing list
> Swig-user <at> lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/swig-user
>

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
Andras Varga | 3 Jan 2007 11:45
Picon

Re: How to add custom code into selected methods of proxy classes?

William,

> The "shadow" feature is on my list of things to do. Full control of the
> code can be achieved by using the javaout typemap, as per the examples
> in previous email, so there is no for these other features.

In my understanding, the javaout typemap (1) cannot be method specific
(2) matches the return type which is usually "void" with my container
insertion methods.  How could it possibly be used to insert "disown"
calls into specific methods?

Even, it is possible to get javaout generate different codes for the
following two functions?
     Foo *Someclass::method1();
and
     Foo *Someclass::method2();

Actually, I already solved my problem with a couple of Perl scripts.
The only reason I'm posting this is that SWIG has been very useful for
me, and I'd like to give my 2 cents into improving it.

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
Andras Varga | 3 Jan 2007 13:18
Picon

Re: How to add custom code into selected methods of proxy classes?

Please ignore my previous post. Reading again the example you wrote
and trying it out, I just realized that %typemap(javaout) is
essentially the same as %feature(shadow), and together with $jnicall
it can implement my proposed %feature("javaproxymethod-prependcode")
too.

I wish the documentation would be a bit more clear on this... E.g. it
doesn't contain a method-specific %typemap(javaout) example. Now that
I've learned the tricks, I could contribute a "disown" example into
the "19.4.3.1 Memory management" chapter if you wish.

Thanks,
Andras

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
Aaron Dalton | 3 Jan 2007 16:06
Picon
Favicon

Perl and Subroutine Refrences

I'm not quite sure where to start.  I'm trying to wrap Sourceforge's
libfov project (http://sourceforge.net/projects/libfov).  I'm running
into difficulty in regards to callbacks.  The library allows you to set
the two callbacks as follows:

void fov_settings_set_opacity_test_function(fov_settings_type *settings,
                                            bool (*f)(void *map,
                                                      int x, int y)) {
    settings->opaque = f;
}

void fov_settings_set_apply_lighting_function(fov_settings_type *settings,
                                              void (*f)(void *map,
                                                        int x, int y,
                                                        int dx, int dy,
                                                        void *src)) {
    settings->apply = f;
}

In the example simple.cc file this is handled as follows:

void apply(void *map, int x, int y, int dx, int dy, void *src) {
	if (((MAP *)map)->onMap(x, y))
		((MAP *)map)->setSeen(x, y);
}

bool opaque(void *map, int x, int y) {
	return ((MAP *)map)->blockLOS(x, y);
}

How on Earth can I usefully wrap this with Perl?  Ideally, the end-user
would create their own Map object that implements the setSeen() and
blockLOS() routines and they would simply be magically called.  I cannot
simply add the above 2 declarations to the main fov.h file because MAP
is a specific C type.  Do I need to also wrap some basic MAP object that
the end-user would have to inherit to make the above work?  I'm really
kinda confused.

Anyway, I thank you for your time and attention.

P.S.  Yes, this is a resend.  It is the first and last.  I just wanted
to see if anybody could help me.

--

-- 
Aaron Dalton       |   Super Duper Games
aaron <at> daltons.ca   |   http://superdupergames.org

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
Michele Joyce | 3 Jan 2007 19:50
Favicon

help with templates and perl

Hi,
I'm trying to wrap perl with swig 1.39 for linux...





I'm sure that this problem has an easy answer, I just can't come up with it.  Relevant code is in RED
The relevant header file:

event.h

#ifndef MYA_EVENT_H
#define MYA_EVENT_H

////////////////////////////////////////////////////////////////////////////////
/*! \file event.h
 * \brief Contains MYA channel event class definitions
 * \author Christopher J. Slominski
 * \par Revision History:
 * - 09/01/2006 Initial development
 */

#include "eventtype.h"
#include <time.h>
#include <string>
#include <iostream>
#include <vector>

// Provide forward references to make the compiler happy.
namespace MYA { class BasicEvent; }
std::ostream & operator << (std::ostream &, const MYA::BasicEvent &);


namespace MYA {

/*! \brief Abstract base for channel events.
 *
 * This class serves as the base for the set of concrete MYA
 * channel events. It is responsible for the handling of event time
 * and event type code common to all channel events. Note that event
 * timestamps are kept in the MYA time format, which is a 64-bit
 * integer, scaled as seconds * 2^32, offset from the UNIX epoch.
 */
class BasicEvent
{ friend std::ostream & ::operator << (std::ostream &, const BasicEvent &);

  public:                                 // Construction/Destruction.
    //! Zero all fields on construction.
    BasicEvent(void) { memset(this, 0, sizeof(BasicEvent)); }
    //! Only provided for proper polymorphic destruction.
    virtual ~BasicEvent(void) {}
   
  public:                                 // Timestamp methods.
    //! Return time difference in seconds.
    inline double operator - (const BasicEvent &);
    //! Return timestamp in UNIX struct format.
    inline struct timespec & DateTime(struct timespec &) const;
    //! Return timestamp in seconds past UNIX epoch.
    inline time_t Seconds(void) const;
    //! Return timestamp as formatted text string.
    std::string DateString(void) const;
    //! Return timestamp in MYA format.
    long long DateTime(void) const { return m_time; };

  public:
    unsigned Code(void) const { return m_code; }   //!< Event type code
    bool Empty(void) const { return m_time == 0; } //!< Uninitialized event?
    static unsigned m_precision;                   //!< Output formatting
    static char m_separator;                       //!< Output formatting

  protected:
    //! Insert event's time into a stream.
    void StreamTime(std::ostream &) const;
    //! Insert event's value into a stream.
    virtual void StreamValue(std::ostream &) const = 0;
   
  protected:
    long long m_time;    //!< MYA Timestamp associated with channel event.
    unsigned m_code;     //!< Event type code.
};




/*! \brief Array type of channel event.
 *
 * This class represents multiple value channel events of any data type.
 * It can be used for single value channel events, but adds a little
 * complexity to get the value and is less efficient than the ScalarEvent.
 */
template <typename Type>
class ArrayEvent : public BasicEvent
{ friend class ArchivePortal;
 
  public:
    //! Accessor of channel event values.
    const std::vector<Type> & Value(void) const { return m_value; }

  private:
    //! Insert event values into the passed stream.
    void StreamValue(std::ostream &) const;
   
  private:
    std::vector<Type> m_value;  // Collection of values for this event.
};


/*!
 * The timespec UNIX struct provides seconds past the epoch and
 * nano-seconds within the second.
 * \return structure with .tv_sec and .tv_nsec
 */
inline struct timespec & BasicEvent::DateTime(struct timespec &ts) const
{
  // The seconds if the upper 32 bits of the MYA time. The lower
  // 32-bits must be convert to nano-seconds by scaling with
  // 10^9/2^32 = 0.232830643.
  ts.tv_sec = m_time >> 32;
  unsigned fraction = static_cast<unsigned>(m_time & 0xFFFFFFFF);
  ts.tv_nsec = static_cast<int>(fraction * 0.232830643);
  return ts;
}


/*!
 * The seconds value is simply the upper 32 bits of MYA time.
 * \return UNIX time_t
 */
inline time_t BasicEvent::Seconds(void) const
{
  return m_time >> 32;
}


/*!
 * \return Time difference in floating point seconds.
 */
inline double BasicEvent::operator - (const BasicEvent &ev)
{
  // Subtract the MYA times and scale by 1/2^32 to get seconds.
  return static_cast<double>(m_time - ev.m_time) * 2.328306437e-10;
}


/*!
 * Insert each of the set of values associated with this event into
 * the passed stream, separating then by the chosen seperator character.
 */
template <typename Type>
void ArrayEvent<Type>::StreamValue(std::ostream &ostr) const
{
  // The event's values are in a stl vector.
  typename std::vector<Type>::const_iterator it = m_value.begin();
 
  // Iterate through the vector, inserting values and seperators.
  while (it != m_value.end())
  { if (it != m_value.begin()) ostr << m_separator;
    ostr << *it++;
  }
}

}

#endif

My interface file:

%module event.i
#pragma SWIG nowarn=503
%include std_string.i
%include std_vector.i
%include std_map.i   
%include typemaps.i
%{
#bunch of include files that I don't think are relevant to this problem
#include "/cs/certified/apps/myapi/v2.0/inc/event.h"
%}

%{
using namespace MYA;
%}

#this is creating a a vector of Array Events (which is itself a vector of std::string)
template <typename T, typename E>
struct Era : public std::vector< E > {};

#not sure what's really necessary here. But, I this combination is how I got it to compile...(help is appreciated)
%typedef MYA::ArrayEvent<std::string> ArrayEvent<std::string>;
%template(stringArrayEvent) MYA::ArrayEvent<std::string>;

%typedef std::vector<ArrayEvent<std::string> > arrayVector;
%template(arrayVector) std::vector<ArrayEvent<std::string> >;
%template(arrayEra) Era<std::string, ArrayEvent<std::string> >;
%template(eraPast) MYA::ArchivePortal::Past<std::string>;

Perl code:
    #create an ERA (vector of array events)
    my $era = new myapi::arrayEra();
    print $era, "\n";
    #gives me myapi::arrayEra=HASH(0x8e54178)
    #now, pass this hash into eraPast so that eraPast can fill the era with events...an event should have
    $ap->eraPast($channelkey, $ago, $now, $era, 24);
   
    for (my $i = 0; $i < $era->size(); $i++) {
        my $event = $era->get($i);
        print "$event \n";
        #gives me a scalar foreach event in the era!!  _p_ArrayEventTstd__string_t=SCALAR(0x8e54220)
        print "$$event\n";
        #gives me an integer that i can't decipher: 141225152
    }


So, basically, I'm either not accessing the event information correctly, or I've messed up with the templates and typedefs in my initialization file. I'm so confused now, I don't know where to begin. Any help is appreciated.

Michele

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user
William S Fulton | 3 Jan 2007 22:35
Picon
Favicon
Gravatar

Re: How to add custom code into selected methods of proxy classes?

Andras Varga wrote:
> Please ignore my previous post. Reading again the example you wrote
> and trying it out, I just realized that %typemap(javaout) is
> essentially the same as %feature(shadow), and together with $jnicall
> it can implement my proposed %feature("javaproxymethod-prependcode")
> too.
> 
> I wish the documentation would be a bit more clear on this... E.g. it
> doesn't contain a method-specific %typemap(javaout) example. Now that
> I've learned the tricks, I could contribute a "disown" example into
> the "19.4.3.1 Memory management" chapter if you wish.
> 

Any documentation mods are welcome. Please post any improvements by
submitting a patch to SourceForge patch/bug system.

William

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
Teruhiko Kurosaka | 4 Jan 2007 00:57
Favicon

C or C++ ?

Hi,
I'm new to Swig.
I'd like to to use Swig to create a PHP API for
my library, which is written in C++.
I have a hand-written C API for this library,
which is just an adopter to it.  

That is, I have:
C++ API:
class Foo{
	Foo();
	~Foo();
	setX(int x);
	int getX();
privat:
	int x;
}
and
C API (implementation code after //):
typedef struct FooC Fooc;
FooC *Foo_new(); // return (FooC *) (new Foo());  
void Foo_delete(FooC *self); // delete ((Foo *) self); 
void Foo_setX(FooC *self, int x); // ((Foo *) self) -> setX(x); 
int Foo_getX(FooC *self); // return ((Foo *) self) -> getX(); 

I think I have two choices: give Swig the
C++ API interface file, or the C API interface file.

The Swig documentation seems to emphasize that
Swig is better at handling C, so
I thought I should take the latter approach (C API),
but I would like to hear opinions from Swig experts 
before taking this project.  

Thanks in advance.

-kuro

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

Gmane