Skip to content
This repository was archived by the owner on Nov 3, 2023. It is now read-only.
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,41 @@ Minimalistic C++ app example: [https://github.com/adonmo/meos-cpp-example](https

C++ API Reference: [https://adonmo.github.io/meos/](https://adonmo.github.io/meos/)

## C

MEOS can also be accessed from C.

Here is an example code on how to do this:

```c
#include "meos/meos_c.h"
#include <stdio.h>

int main(int argc, char* argv[]) {
struct MEOS_TBox* tbox = MEOS_newTBox(12.34, 56.78);
double xmin = MEOS_TBox_xmin(tbox);
printf("%lf\n", xmin);
MEOS_deleteTBox(tbox);
}
```
To compile the above code, you would need `libmeos_c.so`. If you do not have it, build it first by running:
```sh
cmake -B build/capi -S capi
cmake --build build/capi
```

After saving the above file as `main.c`, run the following commands to build and run it:

```sh
gcc -I/<path_to_meos>/capi/include -c main.c -o main.o
g++ -L/<path_to_meos>/build/capi main.o -l:libmeos_c.so -o main
LD_LIBRARY_PATH=/<path_to_meos>/build/capi ./main
```
If you see the following output, you have succesfully used MEOS from C!
```
12.340000
```

## Contributing

Issues and pull requests are welcome.
Expand Down
52 changes: 52 additions & 0 deletions capi/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)

project(
meos_c
LANGUAGES C CXX
)
# Get version from VERSION file instead of hardcoding here
# This is so that we can maintain a single version across C/C++ and Python
file(STRINGS ../VERSION LIBMEOS_VERSION)
set(PROJECT_VERSION ${LIBMEOS_VERSION})
message(STATUS "MEOS: v${LIBMEOS_VERSION}")

# ---- Dependencies ----

include(../cmake/CPM.cmake)

CPMAddPackage(
NAME libmeos
SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/..
)

# ---- Create library ----

file(GLOB_RECURSE headers CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/include/*.h")
file(GLOB_RECURSE sources CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp")
add_library(meos_c SHARED ${headers} ${sources})
target_link_libraries(meos_c libmeos)

if(MSVC)
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
endif()

# being a cross-platform target, we enforce standards conformance on MSVC
target_compile_options(meos_c PUBLIC "$<$<BOOL:${MSVC}>:/permissive->")

target_include_directories(meos_c
PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include/${PROJECT_NAME}-${PROJECT_VERSION}>
)

message(${PROJECT_SOURCE_DIR}/include)

packageProject(
NAME ${PROJECT_NAME}
VERSION ${PROJECT_VERSION}
BINARY_DIR ${PROJECT_BINARY_DIR}
INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include
INCLUDE_DESTINATION include/${PROJECT_NAME}-${PROJECT_VERSION}
VERSION_HEADER "${VERSION_HEADER_LOCATION}"
DEPENDENCIES ""
)
104 changes: 104 additions & 0 deletions capi/include/meos/meos_c.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#pragma once

#include <string.h>
#include <time.h>

#ifdef __cplusplus
extern "C" {
#endif

typedef struct MEOS_Period MEOS_Period;
typedef struct MEOS_TBox MEOS_TBox;
typedef struct MEOS_TFloatInst MEOS_TFloatInst;
typedef struct MEOS_TFloatSeq MEOS_TFloatSeq;
typedef struct MEOS_TFloatSeqSet MEOS_TFloatSeqSet;

typedef enum {
MEOS_Interpolation_Stepwise,
MEOS_Interpolation_Linear,
} MEOS_Interpolation;

/*****************************************************************************
* TBox
*****************************************************************************/

MEOS_TBox *MEOS_newTBox(double const xmin, double const xmax);

double MEOS_TBox_xmin(MEOS_TBox *tbox);
double MEOS_TBox_xmax(MEOS_TBox *tbox);

void MEOS_deleteTBox(MEOS_TBox *tbox);

/*****************************************************************************
* Period
*****************************************************************************/

// Constructor with a string
MEOS_Period *MEOS_newPeriod(char *serialized);
// Constructor with two timestamps
MEOS_Period *MEOS_newPeriod_TT(time_t lower, time_t upper);
// Constructor with two timestamps and two boolean bounds
MEOS_Period *MEOS_newPeriod_TTBB(time_t lower, time_t upper, bool lower_inc, bool upper_inc);

char *MEOS_Period_str(MEOS_Period *period);

void MEOS_deletePeriod(MEOS_Period *period);

/*****************************************************************************
* TFloatInst
*****************************************************************************/

// Constructor with a string
MEOS_TFloatInst *MEOS_newTFloatInst(char *serialized);
// Constructor with a value and timestamp
MEOS_TFloatInst *MEOS_newTFloatInst_VT(float value, time_t timestamp);

float MEOS_TFloatInst_value(MEOS_TFloatInst *tfloatinst);
time_t MEOS_TFloatInst_timestamp(MEOS_TFloatInst *tfloatinst);
char *MEOS_TFloatInst_str(MEOS_TFloatInst *tfloatinst);

void MEOS_deleteTFloatInst(MEOS_TFloatInst *tfloatinst);

/*****************************************************************************
* TFloatSeq
*****************************************************************************/

// Constructor with a string
MEOS_TFloatSeq *MEOS_newTFloatSeq(char *serialized);
// Constructor with a set of instants, two boolean bounds and interpolation
MEOS_TFloatSeq *MEOS_newTFloatSeq_IBBI(MEOS_TFloatInst **instants, int count, bool lower_inc,
bool upper_inc, MEOS_Interpolation interpolation);
// Constructor with a set of instants as strings, two boolean bounds and interpolation
MEOS_TFloatSeq *MEOS_newTFloatSeq_IsBBI(char **instants, int count, bool lower_inc, bool upper_inc,
MEOS_Interpolation interpolation);

bool MEOS_TFloatSeq_lower_inc(MEOS_TFloatSeq *tfloatseq);
bool MEOS_TFloatSeq_upper_inc(MEOS_TFloatSeq *tfloatseq);
MEOS_Interpolation MEOS_TFloatSeq_interpolation(MEOS_TFloatSeq *tfloatseq);
MEOS_TFloatInst **MEOS_TFloatSeq_instants(MEOS_TFloatSeq *tfloatseq, int *count);
char *MEOS_TFloatSeq_str(MEOS_TFloatSeq *tfloatseq);

void MEOS_deleteTFloatSeq(MEOS_TFloatSeq *tfloatseq);

/*****************************************************************************
* TFloatSeqSet
*****************************************************************************/

// Constructor with a string
MEOS_TFloatSeqSet *MEOS_newTFloatSeqSet(char *serialized);
// Constructor with a set of sequences and interpolation
MEOS_TFloatSeqSet *MEOS_newTFloatSeqSet_SI(MEOS_TFloatSeq **sequences, int count,
MEOS_Interpolation interpolation);
// Constructor with a set of sequences as strings and interpolation
MEOS_TFloatSeqSet *MEOS_newTFloatSeqSet_SsI(char **sequences, int count,
MEOS_Interpolation interpolation);

MEOS_TFloatSeq **MEOS_TFloatSeqSet_sequences(MEOS_TFloatSeqSet *tfloatseqset, int *count);
MEOS_TFloatSeqSet *MEOS_TFloatSeqSet_atPeriod(MEOS_TFloatSeqSet *tfloatseqset, MEOS_Period *period);
char *MEOS_TFloatSeqSet_str(MEOS_TFloatSeqSet *tfloatseqset);

void MEOS_deleteTFloatSeqSet(MEOS_TFloatSeqSet *tfloatseqset);

#ifdef __cplusplus
}
#endif
Loading