initial implementation

This commit is contained in:
Roland Conybeare 2023-10-18 12:31:38 -04:00
commit c96029fa1b
8 changed files with 204 additions and 0 deletions

44
CMakeLists.txt Normal file
View file

@ -0,0 +1,44 @@
# xo-pyprintjson/CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(xo_pyprintjson VERSION 1.0)
enable_language(CXX)
# common XO cmake macros (see github.com:Rconybea/xo-cmake)
include(xo_macros/xo_cxx)
include(xo_macros/code-coverage)
# ----------------------------------------------------------------
# unit test setup
enable_testing()
# activate code coverage for all executables + libraries (when configured with -DCODE_COVERAGE=ON)
add_code_coverage()
# 1. assuming that /nix/store/ prefixes .hpp files belonging to gcc, catch2 etc.
# we're not interested in code coverage for these sources.
# 2. exclude the utest/ subdir, we don't need coverage on the unit tests themselves;
# rather, want coverage on the code that the unit tests exercise.
#
# NOTE: this seems to work only with the 'ccov-all' target. In particular, doesn't seem to do anything with the 'ccov' target
#
add_code_coverage_all_targets(EXCLUDE /nix/store/* ${PROJECT_SOURCE_DIR}/utest/* ${PROJECT_BINARY_DIR}/local/* ${PROJECT_SOURCE_DIR}/repo/*)
# ----------------------------------------------------------------
# c++ settings (usually temporary)
set(PROJECT_CXX_FLAGS "")
add_definitions(${PROJECT_CXX_FLAGS})
xo_toplevel_compile_options()
# ----------------------------------------------------------------
# sources
add_subdirectory(src/pyprintjson)
#add_subdirectory(utest)
# ----------------------------------------------------------------
# provide find_package() support
xo_export_cmake_config(${PROJECT_NAME} ${PROJECT_VERSION} ${PROJECT_NAME}Targets)

46
README.md Normal file
View file

@ -0,0 +1,46 @@
# python bindings for c++ printjson library (xo-printjson)
## Getting Started
### build + install dependencies
- [github/Rconybea/xo-pyutil](https://github.com/Rconybea/xo-pyutil)
- [github/Rconybea/xo-printjson](https://github.com/Rconybea/xo-printjson)
### build + install
```
$ cd xo-pyprintjson
$ mkdir build
$ cd build
$ INSTALL_PREFIX=/usr/local # or wherever you prefer, e.g. ~/local
$ cmake \
-DCMAKE_MODULE_PATH=${INSTALL_PREFIX}/share/cmake \
-DCMAKE_PREFIX_PATH=${INSTALL_PREFIX} \
-DCMAKE_INSTALL_PREFIX=${INSTALL_PREFIX} ..
$ make
$ make install
```
(also see .github/workflows/main.yml)
### build for unit test coverage
```
$ cd xo-pyprintjson
$ mkdir build-ccov
$ cd build-ccov
$ cmake \
-DCMAKE_MODULE_PATH=${INSTALL_PREFIX}/share/cmake \
-DCMAKE_PREFIX_PATH=${INSTALL_PREFIX} \
-DCODE_COVERAGE=ON \
-DCMAKE_BUILD_TYPE=Debug ..
```
### LSP (language server) support
LSP looks for compile commands in the root of the source tree;
while Cmake creates them in the root of its build directory.
```
$ cd xo-pyprintjson
$ ln -s build/compile_commands.json # supply compile commands to LSP
```

View file

@ -0,0 +1,4 @@
@PACKAGE_INIT@
include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake")
check_required_components("@PROJECT_NAME@")

View file

@ -0,0 +1,25 @@
/* @file pyprintjson.hpp
*
* automatically generated from src/pyprintjson/pyprintjson.hpp.in
* see src/pyprintjson/CMakeLists.txt
*/
/* python requires module name = library name
* example:
* PYBIND11_MODULE(PYPRINTJSON_MODULE_NAME(), m) { ... }
*/
#define PYPRINTJSON_MODULE_NAME() pyprintjson
/* example:
* py::module_::import(PYPRINTJSON_MODULE_NAME_STR)
*/
#define PYPRINTJSON_MODULE_NAME_STR "pyprintjson"
/* example:
* PYPRINTJSON_IMPORT_MODULE()
* replaces
* py::module_::import("pyprintjson")
*/
#define PYPRINTJSON_IMPORT_MODULE() py::module_::import("pyprintjson")
/* end pyprintjson.hpp */

View file

@ -0,0 +1,8 @@
# xo_pyprintjson/src/pyprintjson/CMakeLists.txt
set(SELF_LIB pyprintjson)
set(SELF_SRCS pyprintjson.cpp)
xo_pybind11_library(${SELF_LIB} ${PROJECT_NAME}Targets ${SELF_SRCS})
xo_pybind11_dependency(${SELF_LIB} printjson)

2
src/pyprintjson/EXAMPLES Normal file
View file

@ -0,0 +1,2 @@
import pyprintjson
pj=pyprintjson.PrintJson()

View file

@ -0,0 +1,50 @@
/* @file pyprintjson.cpp */
// note: need pyreflect/ here bc pyreflect.hpp is generated, located in build directory
#include "pyprintjson.hpp"
#include "xo/pyreflect/pyreflect.hpp"
#include "xo/printjson/PrintJson.hpp"
#include "xo/reflect/TaggedRcptr.hpp"
//#include "reflect/SelfTagging.hpp"
//#include "refcnt/Refcounted.hpp"
//#include "refcnt/Unowned.hpp"
#include "xo/pyutil/pyutil.hpp"
//#include <pybind11/pybind11.h>
//#include <pybind11/stl.h>
//#include <pybind11/chrono.h>
//#include <pybind11/operators.h>
namespace xo {
namespace py = pybind11;
namespace json {
using xo::reflect::SelfTagging;
using xo::reflect::TaggedRcptr;
using xo::ref::rp;
using xo::ref::unowned_ptr;
PYBIND11_MODULE(PYPRINTJSON_MODULE_NAME(), m) {
PYREFLECT_IMPORT_MODULE();
py::class_<PrintJson, rp<PrintJson>>(m, "PrintJson")
.def_static("instance", &PrintJsonSingleton::instance)
.def("print",
[](PrintJson & pj, TaggedRcptr p)
{
pj.print_tp(p, &std::cout); std::cout << "\n";
},
py::arg("value"))
.def("print",
[](PrintJson & pj, rp<SelfTagging> const & p)
{
pj.print_obj(p, &std::cout); std::cout << "\n";
},
py::arg("value"));
//m.def("print_json", [](){ return PrintJsonSingleton::instance_ptr(); });
} /*pyprintjson*/
} /*namespace json*/
} /*namespace xo*/
/* end pyprintjson.cpp */

View file

@ -0,0 +1,25 @@
/* @file pyprintjson.hpp
*
* automatically generated from src/pyprintjson/pyprintjson.hpp.in
* see src/pyprintjson/CMakeLists.txt
*/
/* python requires module name = library name
* example:
* PYBIND11_MODULE(PYPRINTJSON_MODULE_NAME(), m) { ... }
*/
#define PYPRINTJSON_MODULE_NAME() @SELF_LIB@
/* example:
* py::module_::import(PYPRINTJSON_MODULE_NAME_STR)
*/
#define PYPRINTJSON_MODULE_NAME_STR "@SELF_LIB@"
/* example:
* PYPRINTJSON_IMPORT_MODULE()
* replaces
* py::module_::import("pyprintjson")
*/
#define PYPRINTJSON_IMPORT_MODULE() py::module_::import("@SELF_LIB@")
/* end pyprintjson.hpp */