Export type into python - c++

I want to export singleton into python using boost.python and use it there. Here is the code:
class ConfigManager : public boost::serialization::singleton<ConfigManager> {};
inline ConfigManager &configManager() { return ConfigManager::get_mutable_instance(); }
BOOST_PYTHON_MODULE(ConfigManager)
{
bp::class_<ConfigManager, boost::noncopyable>("ConfigManager", bp::no_init);
bp::def("getHandle", &configManager, bp::return_value_policy<bp::copy_non_const_reference>());
}
Now, when I call getHandle in python, I get:
TypeError: No to_python (by-value)
converter found for C++ type:
ConfigManager
What I did wrong?

copy_non_const_reference will try to copy your reference to a Python object, you should use instead bp::reference_existing_object, I tried here and the error message disappeared

Related

Compile Error(C2440)Using CEFbrowser & VS2015

there. I'm new to CEFbrowser.
I'm developing the download model of My CefBrowser.
I've written some code but error while compiling.
class CefClient : public virtual CefBase {
public:
///
// Return the handler for download events. If no handler is returned downloads
// will not be allowed.
///
/*--cef()--*/
virtual CefRefPtr<CefDownloadHandler> GetDownloadHandler(){
return this;
}
But VS2015 says C2440:
"return":cannot convert from 'CefClient *const' to 'CefRefPtr<CefDownloadHandler>'
I'm new. and when i change return this to return null it runs, but can't download.
What can i do to solve this problem?
Thank you!
It looks like your CefClient has to inherit from CefDownloadHandler, i.e. class CefClient: public virtual CefBase, public CefDownloadHandler: CEF C++ Implementing download handler
Once you inherit from CefDownloadHandler, returning this from an instance of CefClient will fit correctly as a CefRefPtr<CefDownloadHandler>.

SWIG: Unable to ignore a method in a C++ class

I'm trying to wrap a C++ library using Swig, and I'm having trouble with an overloaded method (and it doesn't help that I haven't touched C++ in 18 years :)
Here's a much simplified class structure:
class TView : public TObject, public TStreamable {
public:
virtual TPalette& getPalette() const;
}
class TGroup : public TView {
// ...
}
class TWindow: public TGroup {
virtual TPalette& getPalette() const;
}
The swig file (again, simplified, looks like this):
%module(directors="1") tvision
%feature("director") TApplication;
%feature("director") TProgram;
%feature("director") TProgInit;
%feature("director") TWindow;
%feature("director") TWindowInit;
%feature("director") TView;
%feature("director") TGroup;
// Obviously a shotgun approach hoping one of these works :P
%rename ("$ignore") TWindow::getPalette;
%ignore TWindow::getPalette;
%rename ("getPalette2") "TWindow::getPalette";
//...
%include "tv/view.h"
%include "tv/group.h"
%include "tv/window.h"
When I generate the Java wrappers and try to compile it, I get this error:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project tvsample: Compilation failure: Compilation failure:
[ERROR] /home/jdlee/src/turbovision/swig/src/generated/java/com/steeplesoft/turbovision/internal/tvisionJNI.java:[1348,45] incompatible types: short cannot be converted to com.steeplesoft.turbovision.internal.TPalette
[ERROR] /home/jdlee/src/turbovision/swig/src/generated/java/com/steeplesoft/turbovision/internal/TWindow.java:[129,16] getPalette() in com.steeplesoft.turbovision.internal.TWindow cannot override getPalette() in com.steeplesoft.turbovision.internal.TView
[ERROR] return type short is not compatible with com.steeplesoft.turbovision.internal.TPalette
Two things. The first is that TWindow::getPalette should be ignored, but it's not. The second is that SWIG is generating this Java method:
public short getNumber() {
return tvisionJNI.TWindow_number_get(swigCPtr, this);
}
Does anybody have an answer for either issue? Ideally, there should be a single TWindow#getPalette() method that returns TPalette. If I can fix that, it might fix the ignore issue, as I'm only trying to ignore the method to see if I can just get past the compilation issue.
Help? Please? :)

Error C2059 syntax error: 'public'

I'm getting this error "Error C2059 syntax error: 'public'" in the first "public" of the HelloWorld.h file when I try to build the project. Looks like it's expecting something else but I'm a rookie on this. I have also tried using ref instead of __gc as the new syntax rules.
Does anybody have a clue of what can be missing here?
Thanks in advance.
HelloWorld.h
#using <mscorlib.dll>
#using "CSharpHelloWorld.netmodule"
using namespace System;
public __gc class HelloWorldC
{
public:
// Provide .NET interop and garbage collecting to the pointer.
CSharpHelloWorld __gc *t;
HelloWorldC() {
t = new CSharpHelloWorld();
// Assign the reference a new instance of the object
}
// This inline function is called from the C++ Code
void callCSharpHelloWorld() {
t->displayHelloWorld();
}
};
use ref class instead of __gc class in line 6
use ^ instead of __gc * in line 10
use gcnew instead of new in line 12 change
Common Language Runtime Support To (/clr) in your project Properties

TextureCache::addImageAsync selector typecast issue

I'm trying to use addImageAsync for the first time, but i cannot get the syntax working. I'm using Cocos2dx 3.3 final and Xcode 6.1.1.
My code is as follows :
(GFXManager.h)
#include "cocos2d.h"
class GFXManager
{
...
void loadStuff();
void textureLoaded(Ref* pObj);
}
(GFXManager.cpp)
...
void GFXManager::loadStuff()
{
std::string path = "whatever.png";
Director::getInstance()->getTextureCache()->addImageAsync(path, callfuncO_selector(GFXManager::textureLoaded));
}
void GFXManager::textureLoaded(Ref* pObj)
{
...
}
The above is based on the "Texture2dTest" sample from Cocos2dx.
But at the line with the addImageAsync instruction Xcode keeps saying this:
Static_cast from 'void (GFXManager::*)(cocos2d::Ref * )' to 'cocos2d::SEL_CallFuncO' (aka 'void (cocos2d::Ref::*)(cocos2d::Ref *)') is not allowed
I tried making 'GFXManager' a class derived from 'Layer' (as in Texture2dTest), and using 'CCObject' in place of 'Ref' (as in Texture2dTest...But 'CCObject' is deprecated and now it is called 'Ref'), with no luck.
But every example i've found on the web about using addImageAsync calls the selector with that syntax.
So what am i doing wrong ?
You need to change callfuncO_selector with std::bind or CC_CALLBACK_1:
void GFXManager::loadStuff()
{
std::string path = "whatever.png";
Director::getInstance()->getTextureCache()->addImageAsync(path, CC_CALLBACK_1(GFXManager::textureLoaded, this));
}
because TextureCache::addImageAsync accepts std::function not the function pointer

boost python question

I'm trying to expose one of my classes to python using boost's python module. I have a class called StatusEffect that I'd like to expose to python. To do so, I'm creating this in the StatusEffect.h file after the class definition of StatusEffect
BOOST_PYTHON_MODULE(StatusEffect)
{
boost::python::class_<StatusEffect>("StatusEffect")
.def("GetPriority", &StatusEffect::GetPriority)
.def("GetDescription", &StatusEffect::GetDescription)
.def("GetName", &StatusEffect::GetName);
}
I want to use it inside python from a function declared inside a header as so:
Primaries.h
#include <boost\python.hpp>
#include <StatusEffects\StatusEffect.h>
#include "ScriptDeclarations.h";
extern void Test();
and in Primaries.cpp
#include "Primaries.h"
class StatusEffect;
using namespace boost::python;
class CppClass {
public:
int getNum() {
return 7;
}
};
BOOST_PYTHON_MODULE(CppMod) {
class_<CppClass>("CppClass")
.def("getNum",&CppClass::getNum);
}
void Test()
{
CppClass mClass;
try
{
PyImport_AppendInittab("CppMod", &initCppMod);
PyImport_AppendInittab("StatusEffect", &initStatusEffect);
Py_Initialize();
object main_module((
handle<>(borrowed(PyImport_AddModule("__main__")))));
object main_namespace = main_module.attr("__dict__");
StatusEffect effect("Haste");
main_namespace["eff"] = ptr(&effect);
handle<> ignored((PyRun_String("print eff.GetName()\n",
Py_file_input,
main_namespace.ptr(),
main_namespace.ptr() ) ));
/*main_namespace["CppClass"] = class_<CppClass>("CppClass")
.def("getNum",&CppClass::getNum);
main_namespace["cpp"] = ptr(&mClass);
handle<> ignored(( PyRun_String("print cpp.getNum()\n",
Py_file_input,
main_namespace.ptr(),
main_namespace.ptr() ) ));*/
}
catch( error_already_set )
{
PyErr_Print();
}
}
however, when I do this, I get the following error:
Error 16 error LNK2001: unresolved external symbol "public: class std::basic_string,class std::allocator > __thiscall StatusEffect::GetDescription(void)" (?GetDescription#StatusEffect##QAE?AV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##XZ) C:\KHMP\Game_Cpp\KHMPCpp\KHMPCpp\GameCharacter.obj KHMPCpp
I'm quite sure this is related to my python declaration as when I remove that, and the corresponding code from Test() it compiles correctly. If I uncomment the code concerning the CppCLass in Primaries.cpp and use CppClass however, everything works fine. Do python module declarations need to be in the same source file as where they're used? If so, is there some way around that? I'd like to ultimately be able to put all the Python wrappers for my classes in a single header file.
Can anyone guide me as to what I'm doing wrong?
thanks
One thing to note is that your wrapper should be in your StatusEffect.cpp, not StatusEffect.h or a separate CPP file. However, it should be OK anyway, since everything should still get exported. I would also write a test script in Python, instead of trying to test it from C.
There is not enough information to figure out why your code is not working. Some of the things to consider:
Does GetDescription have an implementation? Is it a pure-virtual function?
Is your StatusEffect class in a different module? If it is, is it __dllexport'ed (if you're on Windows)?
The error seems to point at a problem before you get to the boost::python wrapper. Have you tried commenting out your python stuff and just trying to call StatusEffect::GetDescription directly from your test program?