Welcome to mirror list, hosted at ThFree Co, Russian Federation.

vector_uint8.hpp « pyhelpers - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f1bebc5ff3b9a6d1051a9aff567d08abefe193f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#pragma once

#include "std/vector.hpp"

#include <boost/python.hpp>
#include <boost/python/suite/indexing/map_indexing_suite.hpp>

namespace
{
using namespace boost::python;

// Converts a vector<uint8_t> to/from Python str.
struct vector_uint8t_to_str
{
  static PyObject * convert(vector<uint8_t> const & v)
  {
    str s(reinterpret_cast<char const *>(v.data()), v.size());
    return incref(s.ptr());
  }
};

struct vector_uint8t_from_python_str
{
  vector_uint8t_from_python_str()
  {
    converter::registry::push_back(&convertible, &construct, type_id<vector<uint8_t>>());
  }

  static void * convertible(PyObject * obj_ptr)
  {
    if (!PyString_Check(obj_ptr))
      return nullptr;
    return obj_ptr;
  }

  static void construct(PyObject * obj_ptr, converter::rvalue_from_python_stage1_data * data)
  {
    const char * value = PyString_AsString(obj_ptr);
    if (value == nullptr)
      throw_error_already_set();
    void * storage =
        ((converter::rvalue_from_python_storage<vector<uint8_t>> *)data)->storage.bytes;
    new (storage) vector<uint8_t>(value, value + PyString_Size(obj_ptr));
    data->convertible = storage;
  }
};
}  // namespace