SWIG issue, empty files being generated
I am trying to wrap a C++ class using SWIG to create a Java interface, but when I run SWIG on my files it generates empty files.
I am following the example found here: http://www.opensource.apple.com/source/swig/swig-4/swig/Examples/java/class/index.html?txt
I have a header file like this:
开发者_Go百科#include <list>
#include <set>
#include <map>
#include <Heap.h>
#include "Controller.h"
namespace Arbitrator
{
template <class _Tp,class _Val>
class Arbitrator
{
public:
Arbitrator();
bool setBid(Controller<_Tp,_Val>* c, _Tp obj, _Val bid);
bool setBid(Controller<_Tp,_Val>* c, std::set<_Tp> objs, _Val bid);
bool removeBid(Controller<_Tp,_Val>* c, _Tp obj);
bool removeBid(Controller<_Tp,_Val>* c, std::set<_Tp> objs);
bool removeAllBids(Controller<_Tp,_Val>* c);
bool accept(Controller<_Tp,_Val>* c, _Tp obj, _Val bid);
bool accept(Controller<_Tp,_Val>* c, std::set<_Tp> objs, _Val bid);
bool accept(Controller<_Tp,_Val>* c, _Tp obj);
bool accept(Controller<_Tp,_Val>* c, std::set<_Tp> objs);
bool decline(Controller<_Tp,_Val>* c, _Tp obj, _Val bid);
bool decline(Controller<_Tp,_Val>* c, std::set<_Tp> objs, _Val bid);
bool hasBid(_Tp obj) const;
const std::pair<Controller<_Tp,_Val>*, _Val>& getHighestBidder(_Tp obj) const;
const std::list< std::pair<Controller<_Tp,_Val>*, _Val> > getAllBidders(_Tp obj) const;
const std::set<_Tp>& getObjects(Controller<_Tp,_Val>* c) const;
void onRemoveObject(_Tp obj);
_Val getBid(Controller<_Tp,_Val>* c, _Tp obj) const;
void update();
private:
std::map<_Tp,Heap<Controller<_Tp,_Val>*, _Val> > bids;
std::map<_Tp,Controller<_Tp,_Val>* > owner;
std::map<Controller<_Tp,_Val>*, std::set<_Tp> > objects;
std::set<_Tp> updatedObjects;
std::set<_Tp> objectsCanIncreaseBid;
std::set<_Tp> unansweredObjected;
bool inUpdate;
bool inOnOffer;
bool inOnRevoke;
};
template <class _Tp,class _Val>
Arbitrator<_Tp,_Val>::Arbitrator()
{
inUpdate=false;
inOnOffer=false;
inOnRevoke=false;
}
//other code removed to save space
And an interface file I created that looks like this:
/* arb.i */
%module arb
%{
#include "Arbitrator.h"
%}
/* grab header file */
%include "Arbitrator.h"
But when I run SWIG:
swig -c++ -java arb.i
The Java files that SWIG creates are empty.
Has anyone had this issue/know how to resolve this?
Thanks
You have to explicitly instruct SWIG to generate code for the template, with the %template directive.
From the SWIG 2.0 documentation on templates:
SWIG provides support for dealing with templates, but by default, it will not generate any member variable or function wrappers for templated classes. In order to create these wrappers, you need to explicitly tell SWIG to instantiate them. This is done via the %template directive.
Some simple examples from the SWIG 2.0 Templates chapter:
/* Instantiate a few different versions of the template */
%template(intList) List<int>;
%template(doubleList) List<double>;
精彩评论