Skip to content

Commit

Permalink
Refine C api
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisspyB committed Jan 9, 2025
1 parent 1b324a3 commit 0c20677
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 169 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ cmake_minimum_required( VERSION 3.12 FATAL_ERROR )

find_package( ecbuild 3.7.2 REQUIRED HINTS ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/../ecbuild)

project( metkit LANGUAGES CXX )
project( metkit LANGUAGES CXX C )

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
Expand Down
2 changes: 2 additions & 0 deletions src/metkit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ ecbuild_generate_config_headers( DESTINATION ${INSTALL_INCLUDE_DIR}/metkit )

configure_file( metkit_config.h.in metkit_config.h )
configure_file( metkit_version.h.in metkit_version.h )
configure_file( metkit_version.c.in ${CMAKE_CURRENT_BINARY_DIR}/metkit_version.c )

install(FILES
${CMAKE_CURRENT_BINARY_DIR}/metkit_config.h
Expand All @@ -14,6 +15,7 @@ install(FILES
### metkit sources

list( APPEND metkit_srcs
${CMAKE_CURRENT_BINARY_DIR}/metkit_version.c
config/LibMetkit.cc
config/LibMetkit.h
mars/BaseProtocol.cc
Expand Down
106 changes: 16 additions & 90 deletions src/metkit/api/metkit_c.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,33 +37,6 @@ struct metkit_requestiterator_t {
std::vector<metkit::mars::MarsRequest>::iterator iterator_;
};

/// @comment: (maby)
/// Not sure if there is much value in having a param iterator. We could just return an array of
/// strings (char**) using metkit_marsrequest_params.
/// I think we should metkit_paramiterator_t.
struct metkit_paramiterator_t {
explicit metkit_paramiterator_t(std::vector<std::string> vec) :
vector_(std::move(vec)), iterator_(vector_.begin()) {}

int next() {
if (iterator_ == vector_.end()) {
return METKIT_ITERATION_COMPLETE;
}
++iterator_;

return iterator_ == vector_.end() ? METKIT_ITERATION_COMPLETE : METKIT_SUCCESS;
}

void current(const char** param) {
ASSERT(iterator_ != vector_.end());
*param = iterator_->c_str();
}

private:
std::vector<std::string> vector_;
std::vector<std::string>::iterator iterator_;
};

// ---------------------------------------------------------------------------------------------------------------------
// ERROR HANDLING

Expand Down Expand Up @@ -99,27 +72,22 @@ template <typename FN>
return innerWrapFn(fn);
}
catch (const eckit::UserError& e) {
eckit::Log::error() << "User Error: " << e.what() << std::endl;
g_current_error_string = e.what();
return METKIT_ERROR_USER;
}
catch (const eckit::AssertionFailed& e) {
eckit::Log::error() << "Assertion Failed: " << e.what() << std::endl;
g_current_error_string = e.what();
return METKIT_ERROR_ASSERT;
}
catch (const eckit::Exception& e) {
eckit::Log::error() << "METKIT Error: " << e.what() << std::endl;
g_current_error_string = e.what();
return METKIT_ERROR;
}
catch (const std::exception& e) {
eckit::Log::error() << "Unknown Error: " << e.what() << std::endl;
g_current_error_string = e.what();
return METKIT_ERROR_UNKNOWN;
}
catch (...) {
eckit::Log::error() << "Unknown Error!" << std::endl;
return METKIT_ERROR_UNKNOWN;
}
}
Expand All @@ -128,16 +96,6 @@ template <typename FN>
// HELPERS
// -----------------------------------------------------------------------------

int metkit_version(const char** version) {
*version = metkit_version_str();
return METKIT_SUCCESS;
}

int metkit_vcs_version(const char** sha1) {
*sha1 = metkit_git_sha1();
return METKIT_SUCCESS;
}

int metkit_initialise() {
return tryCatch([] {
static bool initialised = false;
Expand All @@ -159,7 +117,7 @@ int metkit_initialise() {
// PARSING
// -----------------------------------------------------------------------------

int metkit_parse_marsrequest(const char* str, metkit_requestiterator_t** requests, bool strict) {
int metkit_parse_marsrequests(const char* str, metkit_requestiterator_t** requests, bool strict) {
return tryCatch([requests, str, strict] {
ASSERT(requests);
ASSERT(str);
Expand Down Expand Up @@ -198,7 +156,7 @@ int metkit_marsrequest_set(metkit_marsrequest_t* request, const char* param, con
request->values(param_str, values_vec);
});
}

int metkit_marsrequest_set_one(metkit_marsrequest_t* request, const char* param, const char* value) {
return metkit_marsrequest_set(request, param, &value, 1);
}
Expand Down Expand Up @@ -228,11 +186,19 @@ int metkit_marsrequest_has_param(const metkit_marsrequest_t* request, const char
});
}

int metkit_marsrequest_params(const metkit_marsrequest_t* request, metkit_paramiterator_t** params) {
return tryCatch([request, params] {
int metkit_marsrequest_count_params(const metkit_marsrequest_t* request, size_t* count) {
return tryCatch([request, count] {
ASSERT(request);
ASSERT(params);
*params = new metkit_paramiterator_t(request->params());
ASSERT(count);
*count = request->params().size();
});
}

int metkit_marsrequest_param(const metkit_marsrequest_t* request, size_t index, const char** param) {
return tryCatch([request, index, param] {
ASSERT(request);
ASSERT(param);
*param = request->params()[index].c_str();
});
}

Expand All @@ -250,24 +216,10 @@ int metkit_marsrequest_value(const metkit_marsrequest_t* request, const char* pa
ASSERT(request);
ASSERT(param);
ASSERT(value);
*value = (request->values(param, false))[index].c_str();
*value = request->values(param, false)[index].c_str();
});
}

int metkit_marsrequest_values(const metkit_marsrequest_t* request, const char* param, const char** values[], size_t* numValues) {
return tryCatch([request, param, values, numValues] {
ASSERT(request);
ASSERT(param);
ASSERT(values);
ASSERT(numValues);
const std::vector<std::string>& values_str = request->values(param);
*numValues = values_str.size();
*values = new const char*[values_str.size()];
std::transform(values_str.begin(), values_str.end(), *values, [](const std::string& s) { return s.c_str(); });
});
}

int metkit_marsrequest_expand(const metkit_marsrequest_t* request, metkit_marsrequest_t* expandedRequest, bool inherit, bool strict) {
int metkit_marsrequest_expand(const metkit_marsrequest_t* request, bool inherit, bool strict, metkit_marsrequest_t* expandedRequest) {
return tryCatch([request, expandedRequest, inherit, strict] {
ASSERT(request);
ASSERT(expandedRequest);
Expand Down Expand Up @@ -312,30 +264,4 @@ int metkit_requestiterator_request(metkit_requestiterator_t* it, metkit_marsrequ
});
}

// -----------------------------------------------------------------------------
// PARAM ITERATOR
// -----------------------------------------------------------------------------

int metkit_delete_paramiterator(const metkit_paramiterator_t* it) {
return tryCatch([it] {
delete it;
});
}

int metkit_paramiterator_next(metkit_paramiterator_t* it) {
return tryCatch(std::function<int()>{[it] {
ASSERT(it);
return it->next();
}});
}

int metkit_paramiterator_param(metkit_paramiterator_t* it, const char** param) {
return tryCatch([it, param] {
ASSERT(it);
ASSERT(param);
it->current(param);
});
}


// ---------------------------------------------------------------------------------------------------------------------
81 changes: 28 additions & 53 deletions src/metkit/api/metkit_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,18 @@ const char* metkit_get_error_string(enum metkit_error_values_t err);
* ------- */

/**
* @brief Set MetKit version.
* @brief Get metkit version.
*
* @param version Version string
* @return int Error code
* @return const char* version string
*/
int metkit_version(const char** version);
const char* metkit_version();

/**
* @brief Set MetKit git sha1 version.
* @brief Get metkit git sha1 version.
*
* @param sha1 SHA1 version string
* @return int Error code
* @return const char* git sha1 version string
*/
int metkit_vcs_version(const char** sha1);
const char* metkit_git_sha1();

/**
* @brief Initialise Main() context.
Expand All @@ -75,17 +73,17 @@ int metkit_initialise();
* Parse MARS requests into RequestIterator of Request instances. Resulting RequestIterator
* must be deallocated with metkit_delete_requestiterator
* @param str MARS requests
* @param requests Allocates RequestIterator object
* @param[out] requests Allocates RequestIterator object
* @return int Error code
*/
int metkit_parse_marsrequest(const char* str, metkit_requestiterator_t** requests, bool strict);
int metkit_parse_marsrequests(const char* str, metkit_requestiterator_t** requests, bool strict);

/* ---------------------------------------------------------------------------------------------------------------------
* REQUEST
* --- */

/** Allocates new Request object. Must be deallocated with mekit_delete_request
* @param request new Request instance
* @param[out] request new Request instance
* @return int Error code
*/
int metkit_new_marsrequest(metkit_marsrequest_t** request);
Expand Down Expand Up @@ -122,31 +120,40 @@ int metkit_marsrequest_set_verb(metkit_marsrequest_t* request, const char* verb)

/** Returns the verb in Request object
* @param request Request instance
* @param verb verb in request
* @param[out] verb verb in request
* @return int Error code
*/
int metkit_marsrequest_verb(const metkit_marsrequest_t* request, const char** verb);

/** Returns whether parameter is in Request object
* @param request Request instance
* @param param parameter name
* @param has whether parameter exists in request
* @param[out] has whether parameter exists in request
* @return int Error code
*/
int metkit_marsrequest_has_param(const metkit_marsrequest_t* request, const char* param, bool* has);

/** Returns ParamIterator of parameters in request. Resulting ParamIterator
* must be deallocated with metkit_delete_paramiterator

/** Returns number of parameters in Request object
* @param request Request instance
* @param[out] count number of parameters in request
* @return int Error code
*/
int metkit_marsrequest_count_params(const metkit_marsrequest_t* request, size_t* count);

/** Returns parameter name for specific index in Request object
* @param request Request instance
* @param params Allocates ParamIterator object for parameter names in request
* @param index index of parameter to retrieve
* @param[out] param parameter name
* @return int Error code
*/
int metkit_marsrequest_params(const metkit_marsrequest_t* request, metkit_paramiterator_t** params);
int metkit_marsrequest_param(const metkit_marsrequest_t* request, size_t index, const char** param);


/** Returns number of values for specific parameter in Request object
* @param request Request instance
* @param param parameter name in request
* @param count number of values for param in request
* @param[out] count number of values for param in request
* @return int Error code
*/
int metkit_marsrequest_count_values(const metkit_marsrequest_t* request, const char* param, size_t* count);
Expand All @@ -155,28 +162,19 @@ int metkit_marsrequest_count_values(const metkit_marsrequest_t* request, const c
* @param request Request instance
* @param param parameter name in request
* @param index index of value to retrieve for param in request
* @param value retrieved value
* @param[out] value retrieved value
* @return int Error code
*/
int metkit_marsrequest_value(const metkit_marsrequest_t* request, const char* param, int index, const char** value);

/** Returns values for specific parameter Request object
* @param request Request instance
* @param param parameter name in request
* @param values array of values for param in request
* @param numValues number of values for param in request
* @return int Error code
*/
int metkit_marsrequest_values(const metkit_marsrequest_t* request, const char* param, const char** values[], size_t* numValues);

/** Populates empty Request object by expanding existing request
* @param request Request instance to be expanded
* @param expandedRequest empty Request instance to be populated
* @param inherit if true, populate expanded request with default values
* @param strict it true, raise error rather than warning on invalid values
* @param[out] expandedRequest empty Request instance to be populated
* @return int Error code
*/
int metkit_marsrequest_expand(const metkit_marsrequest_t* request, metkit_marsrequest_t* expandedRequest, bool inherit, bool strict);
int metkit_marsrequest_expand(const metkit_marsrequest_t* request, bool inherit, bool strict, metkit_marsrequest_t* expandedRequest);

/** Merges other Request object into existing request
* @param request Request instance to contain result of merge
Expand Down Expand Up @@ -208,29 +206,6 @@ int metkit_requestiterator_next(metkit_requestiterator_t* it);
*/
int metkit_requestiterator_request(metkit_requestiterator_t* it, metkit_marsrequest_t* request);

/* ---------------------------------------------------------------------------------------------------------------------
* PARAM ITERATOR
* --- */

/** Deallocates ParamIterator object and associated resources.
* @param it ParamIterator instance
* @return int Error code
*/
int metkit_delete_paramiterator(const metkit_paramiterator_t* it);

/** Moves to the next string element in ParamIterator
* @param it ParamIterator instance
* @return int Error code
*/
int metkit_paramiterator_next(metkit_paramiterator_t* it);

/** Returns the current parameter name in ParamIterator
* @param it ParamIterator instance
* @param param current parameter name in iterator
* @return int Error code
*/
int metkit_paramiterator_param(metkit_paramiterator_t* it, const char** param);

#ifdef __cplusplus
}
#endif
19 changes: 19 additions & 0 deletions src/metkit/metkit_version.c.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "metkit_version.h"

#ifdef __cplusplus
extern "C" {
#endif

const char * metkit_version() { return metkit_VERSION; }

unsigned int metkit_version_int() {
return 10000*metkit_VERSION_MAJOR + 100*metkit_VERSION_MINOR + 1*metkit_VERSION_PATCH;
}

const char * metkit_version_str() { return metkit_VERSION_STR; }

const char * metkit_git_sha1() { return "@metkit_GIT_SHA1@"; }

#ifdef __cplusplus
}
#endif
Loading

0 comments on commit 0c20677

Please sign in to comment.