Skip to content

Commit 9a86e07

Browse files
committed
C API passes struct FdpDataPipeline
Rather than having the C API modify a global DataPipeline, now uses a struct 'FdpDataPipeline' which is passed to init, finalise, link_read and link_write. - Updated naming conventions - Fixed repeat value enum bug
1 parent b28cda1 commit 9a86e07

File tree

3 files changed

+152
-68
lines changed

3 files changed

+152
-68
lines changed

include/fdp/fdp.h

Lines changed: 67 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,42 @@
33

44

55
#ifdef __cplusplus
6+
7+
#include "fdp.hxx"
8+
69
extern "C" {
10+
711
#endif
812

13+
/**
14+
* @brief Struct providing an interface to the pipeline.
15+
*
16+
* Defined in the implementation file, as it depends on C++ features. A pointer to this
17+
* struct should be passed to functions in the C API. Set up by the function fdp_init,
18+
* and finalised by fdp_finalise. Can also be generated from a C++ DataPipeline
19+
* using to_c_struct.
20+
*/
21+
struct FdpDataPipeline;
22+
typedef struct FdpDataPipeline FdpDataPipeline;
923

1024
/**
1125
* @brief Enumeration used to denote different error types.
1226
*
1327
* The underlying C++ API will raise a number of different exception types. These map to
1428
* integer error codes for C compatibility.
1529
*/
16-
enum FDP_ERR_T {
30+
enum FdpError {
1731
FDP_ERR_NONE = 0,
1832
FDP_ERR_CONFIG_PARSE = 1,
1933
FDP_ERR_REST_API_QUERY = 2,
2034
FDP_ERR_JSON_PARSE = 3,
2135
FDP_ERR_VALIDATION = 4,
2236
FDP_ERR_SYNC = 5,
2337
FDP_ERR_WRITE = 6,
24-
FDP_ERR_TOML = 6,
25-
FDP_ERR_OTHER = 7
38+
FDP_ERR_TOML = 7,
39+
FDP_ERR_OTHER = 8
2640
};
27-
typedef enum FDP_ERR_T FDP_ERR_T;
41+
typedef enum FdpError FdpError;
2842

2943

3044
/**
@@ -33,6 +47,10 @@ typedef enum FDP_ERR_T FDP_ERR_T;
3347
* Should be called once before any calls to fdp_link_read or fdp_link_write. If called
3448
* more than once, returns FDP_ERR_OTHER.
3549
*
50+
* @param data_pipeline Pointer-to-pointer of a FdpDataPipeline object. The user
51+
* should declare a pointer to a FdpDataPipeline, and pass its
52+
* address to this function. This function will then initialise
53+
* the pipeline.
3654
* @param config_file_path Path to the `config.yaml` file for this FDP run. Should
3755
* be at the location `${FDP_CONFIG_DIR}/config.yaml`.
3856
* @param script_file_path Path to the script which initiates this FDP run. Should
@@ -42,7 +60,8 @@ typedef enum FDP_ERR_T FDP_ERR_T;
4260
*
4361
* @return Error code.
4462
*/
45-
FDP_ERR_T fdp_init(
63+
FdpError fdp_init(
64+
FdpDataPipeline **data_pipeline,
4665
const char *config_file_path,
4766
const char *script_file_path,
4867
const char *token
@@ -56,10 +75,13 @@ FDP_ERR_T fdp_init(
5675
*
5776
* Record all data products and meta data to the registry. Update the code run with all
5877
* appropriate meta data.
78+
*
79+
* @param data_pipeline Pointer-to-pointer of a FdpDataPipeline object. This function
80+
* finalises the FdpDataPipeline, and sets its pointer to NULL.
5981
*
6082
* @return Error code.
6183
*/
62-
FDP_ERR_T fdp_finalise();
84+
FdpError fdp_finalise(FdpDataPipeline **data_pipeline);
6385

6486

6587
/**
@@ -68,12 +90,17 @@ FDP_ERR_T fdp_finalise();
6890
*
6991
* Must be called after fdp_init and before fdp_finalise.
7092
*
93+
* @param data_pipeline Pointer to a FdpDataPipeline object.
7194
* @param data_product Path to the input file.
7295
* @param data_store_path Path to the assigned data store location. The user should
7396
* allocate sufficient memory beforehand.
7497
* @return Error code
7598
*/
76-
FDP_ERR_T fdp_link_read(const char *data_product, char *data_store_path);
99+
FdpError fdp_link_read(
100+
FdpDataPipeline *data_pipeline,
101+
const char *data_product,
102+
char *data_store_path
103+
);
77104

78105

79106
/**
@@ -82,12 +109,17 @@ FDP_ERR_T fdp_link_read(const char *data_product, char *data_store_path);
82109
*
83110
* Must be called after fdp_init and before fdp_finalise.
84111
*
112+
* @param data_pipeline Pointer to a FdpDataPipeline object.
85113
* @param data_product Path to the output file.
86114
* @param data_store_path Path to the assigned data store location. The user should
87115
* allocate sufficient memory beforehand.
88116
* @return Error code
89117
*/
90-
FDP_ERR_T fdp_link_write(const char *data_product, char *data_store_path);
118+
FdpError fdp_link_write(
119+
FdpDataPipeline *data_pipeline,
120+
const char *data_product,
121+
char *data_store_path
122+
);
91123

92124

93125
/**
@@ -97,7 +129,7 @@ FDP_ERR_T fdp_link_write(const char *data_product, char *data_store_path);
97129
* level to `DEBUG` will include all log types except `TRACE`. These correspond to
98130
* the C++ logging levels `FairDataPipeline::logging::LOG_LEVEL`.
99131
*/
100-
enum FDP_LOG_LEVEL {
132+
enum FdpLogLevel {
101133
FDP_LOG_TRACE = 0,
102134
FDP_LOG_DEBUG = 1,
103135
FDP_LOG_INFO = 2,
@@ -106,23 +138,23 @@ enum FDP_LOG_LEVEL {
106138
FDP_LOG_CRITICAL = 5,
107139
FDP_LOG_OFF = 6
108140
};
109-
typedef enum FDP_LOG_LEVEL FDP_LOG_LEVEL;
141+
typedef enum FdpLogLevel FdpLogLevel;
110142

111143

112144
/**
113145
* @brief Set the log level. Must call `fdp_init` first.
114146
*
115147
* @param log_level
116148
*/
117-
void fdp_set_log_level(FDP_LOG_LEVEL log_level);
149+
void fdp_set_log_level(FdpLogLevel log_level);
118150

119151

120152
/**
121153
* @brief Get the current log level. Must call fdp_init first.
122154
*
123155
* @return Log level
124156
*/
125-
FDP_LOG_LEVEL fdp_get_log_level();
157+
FdpLogLevel fdp_get_log_level();
126158

127159

128160
/**
@@ -135,11 +167,33 @@ FDP_LOG_LEVEL fdp_get_log_level();
135167
*
136168
* @return Error code. 1 if logging unsuccessful, 0 otherwise.
137169
*/
138-
int fdp_log(FDP_LOG_LEVEL log_level, const char* msg);
170+
int fdp_log(FdpLogLevel log_level, const char *msg);
139171

140172

141173
#ifdef __cplusplus
174+
142175
} // close extern "C"
176+
177+
namespace FairDataPipeline {
178+
179+
/**
180+
* @brief Convert data pipeline from the C API to one in the C++ API.
181+
*/
182+
DataPipeline::sptr from_c_struct(FdpDataPipeline *data_pipeline);
183+
184+
/**
185+
* @brief Convert data pipeline from the C++ API to one in the C API.
186+
*
187+
* If the pipeline is set up using the C++ method DataPipeline::construct, this may be
188+
* used to generate a C-compatible struct. Note that this uses 'new' to allocate the
189+
* returned pointer, so the user should 'delete` the pointer after use to avoid memory
190+
* leaks. It is not recommended to mix usage of the C and C++ APIs for init and finalise
191+
* functions.
192+
*/
193+
FdpDataPipeline* to_c_struct(DataPipeline::sptr data_pipeline);
194+
195+
} // close namespace FairDataPipeline
196+
143197
#endif
144198

145199

0 commit comments

Comments
 (0)