improvements to the C wrapper and close a memory leak

This commit is contained in:
Chris Cromer 2023-02-21 21:31:04 -03:00
parent 0e9cc0f832
commit e665ff044f
Signed by: cromer
GPG Key ID: FA91071797BEEEC2
4 changed files with 46 additions and 18 deletions

View File

@ -1,25 +1,47 @@
#ifndef OBELISK_INCLUDE_OBELISK_PROGRAM_H #ifndef OBELISK_INCLUDE_OBELISK_PROGRAM_H
#define OBELISK_INCLUDE_OBELISK_PROGRAM_H #define OBELISK_INCLUDE_OBELISK_PROGRAM_H
/**
* @brief Struct wrapper around Obelisk class.
*
*/
typedef struct CObelisk CObelisk;
#ifdef __cplusplus #ifdef __cplusplus
extern "C" extern "C"
{ {
#endif #endif
/**
* @brief Create an obelisk object.
*
* @return CObelisk* Returns an obelisk object.
*/
extern CObelisk* obelisk_open();
/**
* @brief Delete an obelisk object.
*
* @param[in] obelisk The obelisk object.
*/
extern void obelisk_close(CObelisk* obelisk);
/** /**
* @brief Get the obelisk version. * @brief Get the obelisk version.
* *
* @param[in] obelisk The obelisk object.
* @return const char* Returns a string containing the version. This must be * @return const char* Returns a string containing the version. This must be
* freed by the caller. * freed by the caller.
*/ */
extern const char* obelisk_get_version(); extern char* obelisk_get_version(CObelisk* obelisk);
/** /**
* @brief Get the obelisk library so version. * @brief Get the obelisk library so version.
* *
* @param[in] obelisk The obelisk object.
* @return int Returns the so version. * @return int Returns the so version.
*/ */
extern int obelisk_get_lib_version(); extern int obelisk_get_lib_version(CObelisk* obelisk);
#ifdef __cplusplus #ifdef __cplusplus
}; };

View File

@ -4,20 +4,22 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
const char* obelisk_get_version() CObelisk* obelisk_open()
{ {
CObelisk* obelisk = create_obelisk(); return create_obelisk();
size_t len = strlen(call_obelisk_getVersion(obelisk)) + 1;
char* version = malloc(len);
memcpy(version, call_obelisk_getVersion(obelisk), len);
destroy_obelisk(obelisk);
return version;
} }
int obelisk_get_lib_version() void obelisk_close(CObelisk* obelisk)
{ {
CObelisk* obelisk = create_obelisk();
int version = call_obelisk_getLibVersion(obelisk);
destroy_obelisk(obelisk); destroy_obelisk(obelisk);
return version; }
char* obelisk_get_version(CObelisk* obelisk)
{
return call_obelisk_getVersion(obelisk);
}
int obelisk_get_lib_version(CObelisk* obelisk)
{
return call_obelisk_getLibVersion(obelisk);
} }

View File

@ -1,6 +1,8 @@
#include "obelisk.h" #include "obelisk.h"
#include "obelisk_wrapper.h" #include "obelisk_wrapper.h"
#include <string.h>
extern "C" extern "C"
{ {
CObelisk* create_obelisk() CObelisk* create_obelisk()
@ -9,11 +11,12 @@ extern "C"
return reinterpret_cast<CObelisk*>(obelisk); return reinterpret_cast<CObelisk*>(obelisk);
} }
const char* call_obelisk_getVersion(CObelisk* p_obelisk) char* call_obelisk_getVersion(CObelisk* p_obelisk)
{ {
obelisk::Obelisk* obelisk obelisk::Obelisk* obelisk
= reinterpret_cast<obelisk::Obelisk*>(p_obelisk); = reinterpret_cast<obelisk::Obelisk*>(p_obelisk);
return obelisk->getVersion().c_str(); auto version = strdup(obelisk->getVersion().c_str());
return version;
} }
int call_obelisk_getLibVersion(CObelisk* p_obelisk) int call_obelisk_getLibVersion(CObelisk* p_obelisk)

View File

@ -1,7 +1,7 @@
#ifndef OBELISK_INCLUDE_OBELISK_WRAPPER_H #ifndef OBELISK_INCLUDE_OBELISK_WRAPPER_H
#define OBELISK_INCLUDE_OBELISK_WRAPPER_H #define OBELISK_INCLUDE_OBELISK_WRAPPER_H
typedef struct CObelisk CObelisk; #include "include/obelisk_c.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" extern "C"
@ -19,9 +19,10 @@ extern "C"
* @brief Calls the obelisk method getVersion. * @brief Calls the obelisk method getVersion.
* *
* @param[in] p_obelisk The obelisk object pointer. * @param[in] p_obelisk The obelisk object pointer.
* @return const char* Returns the version. Must be freed by caller. * @return const char* Returns the version. This must be freed by the
* caller.
*/ */
const char *call_obelisk_getVersion(CObelisk *p_obelisk); char *call_obelisk_getVersion(CObelisk *p_obelisk);
/** /**
* @brief Calls the obelisk method getLibVersion. * @brief Calls the obelisk method getLibVersion.