Browse Source

switching to Boost.Python, starting Identity implementation

PYADPT-55
Volker Birk 9 years ago
parent
commit
1fd2926fa9
  1. 12
      setup.py
  2. 50
      src/Identity.cc
  3. 46
      src/Identity.hh
  4. 11
      src/converting.cc
  5. 12
      src/converting.hh
  6. 24
      src/pEpmodule.cc
  7. 30
      src/pEpmodule.hh
  8. 24
      src/str_attr.cc
  9. 13
      src/str_attr.hh

12
setup.py

@ -1,12 +1,16 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from distutils.core import setup, Extension from distutils.core import setup, Extension
from glob import glob
prefix = '/Users/vb'
boost = '/opt/local'
module_pEp = Extension('pEp', module_pEp = Extension('pEp',
sources = ['src/pEpmodule.cc',], sources = glob('src/*.cc'),
include_dirs = ['/Users/vb/include',], include_dirs = [prefix+'/include', boost+'/include',],
library_dirs = ['/Users/vb/lib',], library_dirs = [prefix+'/lib', boost+'/lib',],
libraries = ['pEpEngine',], libraries = ['pEpEngine', 'boost_python-mt',],
) )
setup( setup(

50
src/Identity.cc

@ -0,0 +1,50 @@
#include "Identity.hh"
#include <typeinfo>
namespace pEp {
namespace PythonAdapter {
using namespace std;
Identity::Identity()
: _ident(new_identity(NULL, NULL, NULL, NULL))
{
if (!_ident)
throw bad_alloc();
}
Identity::Identity(Identity& second)
: _ident(identity_dup(second._ident))
{
if (!_ident)
throw bad_alloc();
}
Identity::~Identity()
{
free_identity(_ident);
}
Identity::operator pEp_identity *()
{
if (!_ident)
throw bad_cast();
return _ident;
}
void Identity::lang(string value)
{
if (value == "")
memset(_ident->lang, 0, 3);
else if (value.length() != 2)
throw length_error("length of lang must be 2");
else
memcpy(_ident->lang, value.data(), 2);
}
string Identity::lang()
{
return _ident->lang;
}
}
}

46
src/Identity.hh

@ -0,0 +1,46 @@
#pragma once
#include <pEp/pEpEngine.h>
#include <string>
#include "str_attr.hh"
namespace pEp {
namespace PythonAdapter {
using namespace utility;
class Identity {
pEp_identity *_ident;
public:
Identity();
Identity(Identity& second);
~Identity();
operator pEp_identity *();
void address(string value) { str_attr(_ident->address, value); }
string address() { return str_attr(_ident->address); }
void fpr(string value) { str_attr(_ident->fpr, value); }
string fpr() { return str_attr(_ident->fpr); }
void user_id(string value) { str_attr(_ident->user_id, value); }
string user_id() { return str_attr(_ident->user_id); }
void username(string value) { str_attr(_ident->username, value); }
string username() { return str_attr(_ident->username); }
void comm_type(PEP_comm_type value) { _ident->comm_type = value; };
PEP_comm_type comm_type() { return _ident->comm_type; }
void lang(std::string value);
std::string lang();
void me(bool value) { _ident->me = value; }
bool me() { return _ident->me; }
void me(identity_flags_t flags) { _ident->flags = flags; }
identity_flags_t flags() { return _ident->flags; }
};
}
}

11
src/converting.cc

@ -1,11 +0,0 @@
#include "converting.hh"
#include <new>
#include <stdexcept>
namespace pEp {
namespace utility {
using namespace std;
}
}

12
src/converting.hh

@ -1,12 +0,0 @@
#pragma once
#include <Python.h>
#include <pEp/pEpEngine.h>
#include <pEp/message_api.h>
namespace pEp {
namespace utility {
}
}

24
src/pEpmodule.cc

@ -1,34 +1,24 @@
#include "pEpmodule.hh" #include "pEpmodule.hh"
#include <string> #include <string>
#include <pEp/pEpEngine.h>
namespace pEp { namespace pEp {
namespace PythonAdapter { namespace PythonAdapter {
using namespace std; using namespace std;
PyObject *about(PyObject *self, PyObject *args) string about(void)
{ {
string version = string(version_string) + "\np≡p version " string version = string(version_string) + "\np≡p version "
+ PEP_VERSION + "\n"; + PEP_VERSION + "\n";
return PyUnicode_FromString(version.c_str()); return version;
} }
} }
PEP_SESSION session;
void module_free(void *)
{
release(session);
} }
}
using namespace pEp;
PyMODINIT_FUNC PyInit_pEp(void) BOOST_PYTHON_MODULE(pEp)
{ {
PEP_STATUS status = init(&session); using namespace boost::python;
if (status != PEP_STATUS_OK) using namespace pEp::PythonAdapter;
return NULL; def("about", about);
return PyModule_Create(&pEpmodule);
} }

30
src/pEpmodule.hh

@ -1,36 +1,12 @@
#pragma once #pragma once
#include <Python.h> #include <boost/python.hpp>
#include <pEp/pEpEngine.h>
namespace pEp { namespace pEp {
namespace PythonAdapter { namespace PythonAdapter {
using namespace std;
const char *version_string = "p≡p Python adapter version 0.1"; const char *version_string = "p≡p Python adapter version 0.1";
string about(void);
PyObject *about(PyObject *self, PyObject *args);
} }
void module_free(void *);
struct PyMethodDef pEpMethods[] = {
{"about", pEp::PythonAdapter::about, METH_VARARGS, "about p≡p"},
{NULL, NULL, 0, NULL}
};
struct PyModuleDef pEpmodule = {
PyModuleDef_HEAD_INIT,
"pEp",
"p≡p Python adapter",
-1,
pEpMethods,
NULL,
NULL,
NULL,
pEp::module_free
};
extern PEP_SESSION session;
} }
PyMODINIT_FUNC PyInit_pEp(void);

24
src/str_attr.cc

@ -0,0 +1,24 @@
#include "str_attr.hh"
#include <stdlib.h>
namespace pEp {
namespace utility {
using namespace std;
void str_attr(char *&str, string value)
{
free(str);
str = strdup(value.c_str());
if (!str)
throw bad_alloc();
}
string str_attr(char *&str)
{
if (!str)
return string("");
return string(str);
}
}
}

13
src/str_attr.hh

@ -0,0 +1,13 @@
#pragma once
#include <string>
namespace pEp {
namespace utility {
using namespace std;
void str_attr(char *&str, string value);
string str_attr(char *&str);
}
}
Loading…
Cancel
Save