conversion of boost::shared_ptr in boost::python function call - c++

Consider the following example:
#include "Python.h"
#include <boost/python.hpp>
#include <boost/shared_ptr.hpp>
class A {};
class B : public A{};
void foo(boost::shared_ptr<A>& aptr) { }
BOOST_PYTHON_MODULE(mypy)
{
using namespace boost::python;
class_<A, boost::shared_ptr<A> >("A", init<>());
class_<B, boost::shared_ptr<B>, bases<A> >("B", init<>());
def("foo", foo);
}
if I call the python code
import mypy
b = mypy.B()
mypy.foo(b)
I get
ArgumentError: Python argument types in
mypy.foo(B)
did not match C++ signature:
foo(boost::shared_ptr<A> {lvalue})
I have googled around quite alot, but I can't find a good explanation / fix / workaround for this. Any help is quite welcome!

The problem is that you're asking for a non-const reference to a shared_ptr<A>, and your b instance in Python simply doesn't contain one; it contains a shared_ptr<B>. While shared_ptr<B> can be implicitly converted to shared_ptr<A>, shared_ptr<B>& cannot be implicitly converted to shared_ptr<A>&.
If you can modify foo to take a shared_ptr<A>, or shared_ptr<A> const &, that will solve your problem.
If not, you'll need to also wrap a version that accepts shared_ptr<B>&.

Related

Can't set values in an std::map<std::string,std::shared_ptr<class>>

I'm trying to make a map as showed in the title of this post to link certain bass classes to MQTT topic titles.
I've got I working with raw (c style) pointers, but as I've learned in school smart pointers would be a better design option.
The problem doesn't occur when instantiating the map, but when I want to assign data to it.
The way I've got it working perfectly is:
std::map<std::string,sensors*> factory;
factory["sensorTwo"]= new sensor1;
In this code "sensors" is the baseclass of sensor1;
When I try to do it like this:
std::map<std::string,std::shared_ptr<sensors>> factory;
factory["sensorOne"]= std::make_shared<sensors> (sensor1);
The compiler asks for a primary expression before ';' on the second line.
The assignment in the first example obviously doesn't work with the shared_ptr, but does anybody have an idea on how to do this?
main looks as follows:
#include <string>
#include <memory>
#include <map>
#include "sensors.h"
#include "sensor1.h"
#include "sensor2.h"
int main()
{
//std::map<std::string,sensors*> factory;
std::map<std::string,std::shared_ptr<sensors>> factory;
factory["sensorOne"]= std::make_shared<sensor1> ();
return 0;
}
Given you have declared structures Foo and Bar as follows:
struct Foo {};
struct Bar : Foo {};
Correct syntax for std::make_shared to make a std::shared_ptr<Foo> pointing to a Bar instance is:
std::shared_ptr<Foo> sp = std::make_shared<Bar>();
From your first example, Foo is sensors and Bar is sensor1. So your code should not be:
factory["sensorOne"]= std::make_shared<sensors> (sensor1);
But:
factory["sensorOne"]= std::make_shared<sensor1> ();
You are missing an include to <map>, it can cause this error message.

VS2013 is having trouble with base class being in unnamed namespace

The following code compiles fine and runs as expected:
#include <iostream>
namespace
{
struct Base
{
void print() const { std::cout << "test"; };
};
};
class Derived : public Base
{
};
int main()
{
Derived d;
d.print();
return 0;
}
But when looking at d during runtime using QuickWatch, IntelliSense seems to be unable to find
Base.
I solved this by putting Base in a named namespace instead of an unnamed.
So is it a bug in Visual Studio, or am I missing something out?
This problem with anonymous namepaces has been an issue in VC++ for a while - see http://msdn.microsoft.com/en-us/library/0888kc6a%28VS.80%29.aspx. From the linked doc:
The native C++ expression evaluator does not support anonymous namespaces.
and
The only way to watch the symbol test in this example is to use the decorated name:
e.g. (int*)?test#?A0xccd06570#mars##3HA (using the namespace hierarchy given in the example to illustrate the point). Just use the decorated name? That's so convenient! Thanks, Microsoft.

What does *New() mean when declaring member function?

I'm working on a VTK program and have found a class (specifically this one: Image Region) which i need to incorporate into my code. To do so I have made a separate ImageRegion.h and ImageRegion.cpp files so they can be easily included in the project. My Problem here is the
static vtkBorderCallback *New()
function which i do not know how to implement in the .cpp file or, to be quite honest, what purpose it serves at all. What does it do? Is it even necessary to have it?
When compiling I get the error:
/home/Desktop/test/src/ImageRegion.cpp:7:10: error: ‘vtkBorderCallback::vtkBorderCallback’ names the constructor, not the type
My .h file:
//ImageRegion.h
#pragma once
#include <vtkSmartPointer.h>
#include <vtkActor.h>
#include <vtkAssemblyNode.h>
#include <vtkAssemblyPath.h>
#include <vtkBorderRepresentation.h>
#include <vtkCommand.h>
#include <vtkCoordinate.h>
#include <vtkImageMapper3D.h>
#include <vtkImageActor.h>
#include <vtkInteractorStyleImage.h>
#include <vtkPolyData.h>
#include <vtkPropPicker.h>
#include <vtkProperty2D.h>
#include <vtkBorderWidget.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
class vtkBorderCallback : public vtkCommand
{
public:
vtkBorderCallback();
static vtkBorderCallback *New();
virtual void Execute(vtkObject *caller, unsigned long, void*);
void SetRenderer(vtkSmartPointer<vtkRenderer> ren);
void SetImageActor(vtkSmartPointer<vtkImageActor> im);
double coords[6];
private:
vtkSmartPointer<vtkRenderer> Renderer;
vtkSmartPointer<vtkImageActor> ImageActor;
};
And my .cpp file:
//ImageRegion.cpp
#include "ImageRegion.h"
vtkBorderCallback::vtkBorderCallback(){}
static vtkBorderCallback::vtkBorderCallback* New()
{
return new vtkBorderCallback;
}
void vtkBorderCallback::Execute(vtkObject *caller, unsigned long, void*)
{
//Do stuff, from original VTK example code
}
void vtkBorderCallback::SetRenderer(vtkSmartPointer<vtkRenderer> ren) {this->Renderer = ren;}
void vtkBorderCallback::SetImageActor(vtkSmartPointer<vtkImageActor> im) {this->ImageActor = im;}
Any help is much appreciated.
This
static vtkBorderCallback *New();
is a static member function called New, taking no arguments, and returning a pointer to vtkBorderCallback.
In the implementation, you should omit the static. You also need to place the function in the scope of its class:
vtkBorderCallBack* vtkBorderCallback::New()
{// ^^^^^^^^^^^^^^^^^^^
return new vtkBorderCallback; // danger! Caller needs to delete this eventually
}
In VTK nearly all of the classes derive from vtkObjectBase. They should use New() and Delete() to create and delete the objects (the constructor and destructor are protected). These methods include referencing counting to make sure that they get properly shared among other vtkObjects that may use them. There is a VTK macro (vtkStandardNewMacro) that takes care of the implementation of New() and the base class implements Delete(). So for VTK, the best way to implement the static New() method is to use that macro. For your class called vtkBorderCallBack it would look like:
vtkStandardNewMacro(vtkBorderCallBack);
This should go in your .cpp file.
To solve the error, put vtkBorderCallBack:: before New():
vtkBorderCallBack* vtkBorderCallBack::New()
~~~~~~~~~~~~~~~~~~~
{
...
}
He should not omit static since New() is meant as constructor. In this scenario I would rather expect the real constructor to be private. The implementation
static vtkBorderCallback::vtkBorderCallBack* New()
{
return new vtkBorderCallback;
}
is syntactically wrong. It has so be
vtkBorderCallBack* vtkBorderCallback::New()
{
return new vtkBorderCallback;
}
Finally the whole approach is strange. New() is not really required here, and possibly leads to a memory leak. To establish a class-specific memory management overload operators new and delete on a per-class basis. Alternatively, to prevent leaks, do not return a raw pointer; return std::auto_ptr (deprecated) or std::unique_ptr:
std::unique_ptr<vtkBorderCallBack> vtkBorderCallback::New()
{
return std::unique_ptr<vtkBorderCallBack>(new vtkBorderCallback); // uses move c'tor
}
However, std::unique_ptrs are movable but not copyable. But that's the point when leaks have to be prevented. When the pointer returned by New() is spreaded all over the code better use a std::shared_ptr.
If you have only a C++03 compiler I recommend Herb Sutter's Using auto_ptr Effectively.

Calling a member function with member data by using for_each

Dear all, I would like to call a member function (that expects a reference) for each object of (let's say) a vector that is a member of the same class, as the following code shows:
#include <functional>
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
struct Stuff {
double x;
};
class Test {
public:
void f1(Stuff & thing);
void f2(void);
vector<Stuff> things;
};
void Test::f1(Stuff & thing) {
; // do nothing
}
void Test::f2(void) {
for_each(things.begin(), things.end(), f1);
}
int main(void)
{
return 0;
}
This codes gives me a compiler error related to unresolved overloaded function type . I have tried also with bind, but it seems that the references requisite in f1 is one problem. I know I am missing something important here, so I take this opportunity to solve my problem and to learn. At the moment, I can't install boost, but I would like to know also if boost is useful to solve this problem. Thanks in advance.
The function you want to call cannot be simply identified by f1 but should be referred to as &Test::f1 (as in : member function f1 of class Test)
Function f1 does not take a single argument : as any non-static member function it has an implicit this parameter of type Test * const
Finally, a standard bind won't be able to do the trick because it doesn't handle parameters passed by reference.
Boost.Bind would indeed be a great option :
std::for_each(things.begin(), things.end(), boost::bind(&Test::f1, this, _1));

Macro to improve callback registration readability

I'm trying to write a macro to make a specific usage of callbacks in C++ easier. All my callbacks are member functions and will take this as first argument and a second one whose type inherits from a common base class.
The usual way to go is:
register_callback(boost::bind(&my_class::member_function, this, _1));
I'd love to write:
register_callback(HANDLER(member_function));
Note that it will always be used within the same class.
Even if typeof is considered as a bad practice, it sounds like a pretty solution to the lack of __class__ macro to get the current class name.
The following code works:
typedef typeof(*this) CLASS;
boost::bind(& CLASS :: member_function, this, _1)(my_argument);
but I can't use this code in a macro which will be given as argument to register_callback.
I've tried:
#define HANDLER(FUN) \
boost::bind(& typeof(*this) :: member_function, this, _1);
which doesn't work for reasons I don't understand. Quoting GCC documentation:
A typeof-construct can be used anywhere a typedef name could be used.
My compiler is GCC 4.4, and even if I'd prefer something standard, GCC-specific solutions are accepted.
Your problem might be that typeof yields my_class&. It appears to work with boost::remove_reference:
#include <boost/bind.hpp>
#include <boost/type_traits.hpp>
#include <iostream>
struct X
{
void foo(int i) { std::cout << i << '\n'; }
void bar() {boost::bind(&boost::remove_reference<typeof(*this)>::type::foo, this, _1)(10); }
};
int main()
{
X x;
x.bar();
}
It might be more portable to use BOOST_TYPEOF, and in C++0x decltype