Browse Source

switching to Boost.Python, starting Identity implementation

AllGroupDevicesShowHandshake
Volker Birk 6 years ago
parent
commit
1fd2926fa9
9 changed files with 151 additions and 71 deletions
  1. +8
    -4
      setup.py
  2. +50
    -0
      src/Identity.cc
  3. +46
    -0
      src/Identity.hh
  4. +0
    -11
      src/converting.cc
  5. +0
    -12
      src/converting.hh
  6. +7
    -17
      src/pEpmodule.cc
  7. +3
    -27
      src/pEpmodule.hh
  8. +24
    -0
      src/str_attr.cc
  9. +13
    -0
      src/str_attr.hh

+ 8
- 4
setup.py View File

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


+ 50
- 0
src/Identity.cc View File

@ -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
- 0
src/Identity.hh View File

@ -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; }
};
}
}

+ 0
- 11
src/converting.cc View File

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

+ 0
- 12
src/converting.hh View File

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

+ 7
- 17
src/pEpmodule.cc View File

@ -1,34 +1,24 @@
#include "pEpmodule.hh"
#include <string>
#include <pEp/pEpEngine.h>
namespace pEp {
namespace PythonAdapter {
using namespace std;
PyObject *about(PyObject *self, PyObject *args)
string about(void)
{
string version = string(version_string) + "\np≡p version "
+ 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);
if (status != PEP_STATUS_OK)
return NULL;
return PyModule_Create(&pEpmodule);
using namespace boost::python;
using namespace pEp::PythonAdapter;
def("about", about);
}

+ 3
- 27
src/pEpmodule.hh View File

@ -1,36 +1,12 @@
#pragma once
#include <Python.h>
#include <pEp/pEpEngine.h>
#include <boost/python.hpp>
namespace pEp {
namespace PythonAdapter {
using namespace std;
const char *version_string = "p≡p Python adapter version 0.1";
PyObject *about(PyObject *self, PyObject *args);
string about(void);
}
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
- 0
src/str_attr.cc View File

@ -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
- 0
src/str_attr.hh View File

@ -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