From d8d171baa2a96a2db0f9e051325993d9abde4f11 Mon Sep 17 00:00:00 2001 From: Prem Chaitanya Prathi Date: Fri, 16 Feb 2024 15:15:59 +0530 Subject: [PATCH] feat: add C wrapper and makefile --- cpp/Makefile | 12 ++++++--- cpp/negentropy_wrapper.h | 56 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 cpp/negentropy_wrapper.h diff --git a/cpp/Makefile b/cpp/Makefile index d90464a..c37e418 100644 --- a/cpp/Makefile +++ b/cpp/Makefile @@ -1,12 +1,16 @@ # Build a shared library of negentropy #TODO: Need to add compilation flags based on OS -INCS = -I./ -I/opt/homebrew/include/ +INCS = -I./ -I/opt/homebrew/include/ TARGET = libnegentropy.so -.PHONY: all clean +.PHONY: all clean install-deps -all: negentropy-cpp shared-lib +all: negentropy-cpp c-wrapper shared-lib + +#TODO: Need to add compilation flags based on OS +install-deps: + brew install lmdb openssl shared-lib: negentropy.o g++ -o $(TARGET) negentropy.o -shared @@ -14,6 +18,8 @@ shared-lib: negentropy.o negentropy-cpp: g++ negentropy.h --std=c++20 -o negentropy.o $(INCS) +c-wrapper: + g++ negentropy_wrapper.h --std=c++20 -o negentropy_wrapper.o $(INCS) -I../test/cpp/lmdbxx/include/ clean: rm -f negentropy.o \ No newline at end of file diff --git a/cpp/negentropy_wrapper.h b/cpp/negentropy_wrapper.h new file mode 100644 index 0000000..b59d90f --- /dev/null +++ b/cpp/negentropy_wrapper.h @@ -0,0 +1,56 @@ +#pragma once + +#ifdef __cplusplus +#define EXTERNC extern "C" +#else +#define EXTERNC +#endif + +#include "negentropy.h" +#include "negentropy/storage/BTreeLMDB.h" + +//This is a C-wrapper for the C++ library that helps in integrating negentropy with nim code. +//TODO: Do error handling by catching exceptions + +EXTERNC void* storage_new(){ + negentropy::storage::BTreeLMDB* storage; + //storage = new negentropy::storage::BTreeLMDB(); + return storage; +} + +EXTERNC void* negentropy_new(void* storage, uint64_t frameSizeLimit){ + negentropy::storage::BTreeLMDB* lmdbStorage; + lmdbStorage = reinterpret_cast(storage); + + Negentropy* ne; + try{ + ne = new Negentropy(*lmdbStorage, frameSizeLimit); + }catch(negentropy::err e){ + //TODO:Find a way to return this error + return NULL; + } + return ne; +} + +EXTERNC const char* negentropy_initiate(void* negentropy){ + Negentropy* ngn_inst; + ngn_inst = reinterpret_cast*>(negentropy); + + std::string* output = new std::string(); + try { + *output = ngn_inst->initiate(); + } catch(negentropy::err e){ + //TODO:Find a way to return this error + return NULL; + } + return output->c_str(); +} + +EXTERNC void negentropy_setinitiator(void* negentropy){ + Negentropy *ngn_inst; + ngn_inst = reinterpret_cast*>(negentropy); + + ngn_inst->setInitiator(); + +} +