For example in this example, does the name of my shared object file (.so) need
to be zoo.so in order to see the attribute Animal? I am very confused about
all of this.
/*
* This inclusion should be put at the beginning. It will include <Python.h>.
*/
#include <boost/python.hpp>
#include <cstdint>
#include <string>
#include <vector>
#include <boost/utility.hpp>
#include <boost/shared_ptr.hpp>
/*
* This is the C++ function we write and want to expose to Python.
*/
const std::string hello() {
return std::string("hello, zoo");
}
/*
* Create a C++ class to represent animals in the zoo.
*/
class Animal {
public:
// Constructor. Note no default constructor is defined.
Animal(std::string const & in_name): m_name(in_name) {}
// Copy constructor.
Animal(Animal const & in_other): m_name(in_other.m_name) {}
// Copy assignment.
Animal & operator=(Animal const & in_other) {
this->m_name = in_other.m_name;
return *this;
}
// Utility method to get the address of the instance.
uintptr_t get_address() const {
return reinterpret_cast<uintptr_t>(this);
}
// Getter of the name property.
std::string get_name() const {
return this->m_name;
}
// Setter of the name property.
void set_name(std::string const & in_name) {
this->m_name = in_name;
}
private:
// The only property: the name of the animal.
std::string m_name;
};
/*
* This is a macro Boost.Python provides to signify a Python extension module.
*/
BOOST_PYTHON_MODULE(zoo) {
// An established convention for using boost.python.
using namespace boost::python;
// Expose the function hello().
def("hello", hello);
// Expose the class Animal.
class_<Animal>("Animal",
init<std::string const &>())
.def("get_address", &Animal::get_address)
.add_property("name", &Animal::get_name, &Animal::set_name)
;
}
// vim: set ai et nu sw=4 ts=4 tw=79:
> On Sep 8, 2016, at 6:00 PM, Stefan Seefeld <[email protected]> wrote:
>
> Hi Jon,
>
> please remove the dependency on the "opus/*" headers, so we can compile
> the module ourselves and try to reproduce what you are reporting.
> (That's what I meant with "self-contained test".)
>
>
> Thanks,
> Stefan
>
> On 08.09.2016 17:30, Jon Lederman wrote:
>> Hi,
>>
>> Thanks for responding. Here is my header file. I am compiling this to a
>> shared object called opus_strategy.so. If I set the argument of
>> BOOST_PYTHON_MODULE to opus_encoder_strategy, and compile my .so file to
>> have the name opus_encoder_strategy.so,I can load the boost python module
>> into my python interpreter and see the OpusEncoderStrategy class as an
>> attribute.
>>
>> However, if I choose other names such as opus_strategy for the argument to
>> BOOST_PYTHON_MODULE, when I load the boost python object it doesn’t appear
>> to have any recognized attributes. I don’t understand why the name should
>> matter. As I had noted in my previous email, I would like to have a shared
>> object file with the name opus_strategy.so that encompasses a set of
>> classes. Just can’t figure out how to get it to work. I am on OS X, BTW if
>> that matters.
>>
>> Any help would be greatly appreciated.
>>
>> Thanks.
>>
>> -Jon
>> #ifndef OPUS_ENCODER_STRATEGY_H_
>> #define OPUS_ENCODER_STRATEGY_H_
>>
>>
>>
>>
>>
>> #include "memory"
>> #include "opus/opus.h"
>> #include "opus/opus_defines.h"
>> #include "opus/opus_multistream.h"
>> #include "opus/opus_types.h"
>>
>> #include <boost/python.hpp>
>> #include <boost/numpy.hpp>
>> #include <exception>
>> #include <memory>
>> #include <map>
>> #include <vector>
>> #include <iostream>
>>
>> namespace bp = boost::python;
>> namespace np = boost::numpy;
>>
>> using namespace std;
>>
>>
>> class OpusEncoderStrategy {
>>
>> public:
>>
>> OpusEncoderStrategy(const int sample_rate, const int number_channels,
>> const int opus_application);
>> ~OpusEncoderStrategy();
>>
>> opus_int32 encode(const float* pcm, const int frame_size, const
>> unsigned char* data, opus_int32 max_data_bytes);
>>
>> bool setComplexity(const int c);
>> int getComplexity();
>>
>>
>> private:
>>
>> bool encoderCtl();
>> int getPacketDurationBytes();
>>
>>
>>
>> OpusEncoder* encoder;
>>
>> int fs;
>> int channels;
>> int application;
>> int error;
>>
>>
>> };
>>
>>
>> BOOST_PYTHON_MODULE(opus_strategy)
>> {
>> using namespace boost::python;
>>
>>
>> class_<OpusEncoderStrategy>("OpusEncoderStrategy", init<const int,
>> const int, const int>())
>> .def("setComplexity", &OpusEncoderStrategy::setComplexity)
>> .def("getComplexity", &OpusEncoderStrategy::getComplexity);
>> }
>>
>>
>> #endif
>>> On Sep 8, 2016, at 4:47 PM, Stefan Seefeld <[email protected]> wrote:
>>>
>>> Hi Jon,
>>>
>>> what you are describing makes perfect sense, and should work without
>>> problem. From your description it isn't clear why this isn't working, so
>>> I suggest you put together a self-contained test that doesn't work as
>>> you expect, and send that out so we can have a look. Otherwise we'd have
>>> to guess.
>>> The online docs at http://boostorg.github.io/python/doc/html/index.html
>>> should contain everything you need.
>>>
>>> Stefan
>>>
>>> --
>>>
>>> ...ich hab' noch einen Koffer in Berlin...
>>>
>>> _______________________________________________
>>> Cplusplus-sig mailing list
>>> [email protected]
>>> https://mail.python.org/mailman/listinfo/cplusplus-sig
>> _______________________________________________
>> Cplusplus-sig mailing list
>> [email protected]
>> https://mail.python.org/mailman/listinfo/cplusplus-sig
>
>
> --
>
> ...ich hab' noch einen Koffer in Berlin...
>
> _______________________________________________
> Cplusplus-sig mailing list
> [email protected]
> https://mail.python.org/mailman/listinfo/cplusplus-sig
_______________________________________________
Cplusplus-sig mailing list
[email protected]
https://mail.python.org/mailman/listinfo/cplusplus-sig