pybind.cc 2.4 KB
Newer Older
S
superjom 已提交
1 2
#include <ctype.h>
#include <pybind11/pybind11.h>
S
superjom 已提交
3
#include <pybind11/stl.h>
S
superjom 已提交
4

S
superjom 已提交
5
#include "visualdl/logic/sdk.h"
S
superjom 已提交
6 7 8

namespace py = pybind11;
namespace vs = visualdl;
S
superjom 已提交
9
namespace cp = visualdl::components;
S
superjom 已提交
10

Q
Qiao Longfei 已提交
11 12
PYBIND11_PLUGIN(core) {
  py::module m("core", "C++ core of VisualDL");
S
superjom 已提交
13

S
superjom 已提交
14 15 16 17
#define ADD_SCALAR(T)                                      \
  py::class_<cp::ScalarReader<T>>(m, "ScalarReader__" #T)  \
      .def("records", &cp::ScalarReader<T>::records)       \
      .def("timestamps", &cp::ScalarReader<T>::timestamps) \
S
superjom 已提交
18
      .def("caption", &cp::ScalarReader<T>::caption);
S
superjom 已提交
19 20 21 22 23
  ADD_SCALAR(int);
  ADD_SCALAR(float);
  ADD_SCALAR(double);
  ADD_SCALAR(int64_t);
#undef ADD_SCALAR
S
superjom 已提交
24

S
superjom 已提交
25 26 27 28 29 30 31 32
#define ADD_SCALAR_WRITER(T)                          \
  py::class_<cp::Scalar<T>>(m, "ScalarWriter__" #T)   \
      .def("set_caption", &cp::Scalar<T>::SetCaption) \
      .def("add_record", &cp::Scalar<T>::AddRecord);
  ADD_SCALAR_WRITER(int);
  ADD_SCALAR_WRITER(float);
  ADD_SCALAR_WRITER(double);
#undef ADD_SCALAR_WRITER
S
superjom 已提交
33

S
superjom 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47
#define ADD_SCALAR(T)                                                   \
  .def("get_scalar_" #T, [](vs::Reader& self, const std::string& tag) { \
    auto tablet = self.tablet(tag);                                     \
    return vs::components::ScalarReader<T>(std::move(tablet));          \
  })
  py::class_<vs::Reader>(m, "Reader")
      .def(
          "__init__",
          [](vs::Reader& instance,
             const std::string& mode,
             const std::string& dir) { new (&instance) vs::Reader(mode, dir); })
      // clang-format off
    ADD_SCALAR(float)
    ADD_SCALAR(double)
S
superjom 已提交
48
    ADD_SCALAR(int);
S
superjom 已提交
49 50
// clang-format on
#undef ADD_SCALAR
S
superjom 已提交
51

S
superjom 已提交
52 53 54 55 56
#define ADD_SCALAR(T)                                                   \
  .def("new_scalar_" #T, [](vs::Writer& self, const std::string& tag) { \
    auto tablet = self.AddTablet(tag);                                  \
    return cp::Scalar<T>(tablet);                                       \
  })
S
superjom 已提交
57

S
superjom 已提交
58
  py::class_<vs::Writer>(m, "Writer")
S
superjom 已提交
59 60 61 62 63 64 65 66
      .def("__init__",
           [](vs::Writer& instance,
              const std::string& dir,
              int sync_cycle) {
             new (&instance) vs::Writer(dir);
             instance.storage().meta.cycle = sync_cycle;
           })
      .def("as_mode", &vs::Writer::AsMode)
S
superjom 已提交
67
      // clang-format off
S
superjom 已提交
68 69 70
      ADD_SCALAR(float)
      ADD_SCALAR(double)
      ADD_SCALAR(int);
S
superjom 已提交
71 72 73 74
// clang-format on
#undef ADD_SCALAR

}  // end pybind