feat: add C wrapper and makefile

This commit is contained in:
Prem Chaitanya Prathi 2024-02-16 15:15:59 +05:30
parent d3463be25d
commit d8d171baa2
No known key found for this signature in database
2 changed files with 65 additions and 3 deletions

View File

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

56
cpp/negentropy_wrapper.h Normal file
View File

@ -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<negentropy::storage::BTreeLMDB*>(storage);
Negentropy<negentropy::storage::BTreeLMDB>* ne;
try{
ne = new Negentropy<negentropy::storage::BTreeLMDB>(*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<negentropy::storage::BTreeLMDB>* ngn_inst;
ngn_inst = reinterpret_cast<Negentropy<negentropy::storage::BTreeLMDB>*>(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<negentropy::storage::BTreeLMDB> *ngn_inst;
ngn_inst = reinterpret_cast<Negentropy<negentropy::storage::BTreeLMDB>*>(negentropy);
ngn_inst->setInitiator();
}