dmlite 0.6
any.h
Go to the documentation of this file.
1/** @file include/dmlite/c/any.h
2 * @brief Opaque handler to pass different types of values to the API.
3 * @author Alejandro Álvarez Ayllon <aalvarez@cern.ch>
4 * @note Basically it wraps boost::any and dmlite::Extensible.
5 */
6#ifndef DMLITE_ANY_H
7#define DMLITE_ANY_H
8
9#include "dmlite/common/config.h"
10#include <stddef.h>
11#include <stdint.h>
12
13#ifdef __cplusplus
14extern "C" {
15#endif
16
17/**
18 * @brief Used to pass configuration values.
19 */
20typedef struct dmlite_any dmlite_any;
21
22/**
23 * @brief Handles key->value pairs.
24 */
26
27/**
28 * @brief Creates a new dmlite_any.
29 * @param str The string that will be wrapped. It is safe to free afterwards.
30 * @return A newly allocated dmlite_any.
31 */
33
34/**
35 * @brief Creates a new dmlite_any.
36 * @param l The long that will be wrapped.
37 * @return A newly allocated dmlite_any.
38 */
40
41/**
42 * @brief Creates a new dmlite_any from an int64_t type.
43 * @param i The int64_t value.
44 * @return A newly allocated dmlite_any.
45 */
47
48/**
49 * @brief Creates a new dmlite_any from an uint64_t type.
50 * @param i The uint64_t value.
51 * @return A newly allocated dmlite_any.
52 */
54
55/**
56 * @brief Creates a new dmlite_any.
57 * @param n The number of elements.
58 * @param strv The strings that will be wrapped. It is safe to free afterwards.
59 * @return A newly allocated dmlite_any.
60 * @deprecated Use dmlite_set_array instead.
61 */
62dmlite_any* dmlite_any_new_string_array(unsigned n, const char** strv);
63
64/**
65 * @brief Creates a new dmlite_any.
66 * @param n The number of elements.
67 * @param lv The longs that will be wrapped.
68 * @return A newly allocated dmlite_any.
69 * @deprecated Use dmlite_set_array instead.
70 */
72
73/**
74 * @brief Frees a dmlite_any.
75 * @param any The dmlite_any to destroy.
76 */
78
79/**
80 * @brief Gets the string interpretation of the dmlite_any.
81 * @details Defaults to "".
82 * @param any The dmlite_any to convert.
83 * @param buffer Where to put the string.
84 * @param bsize The size of the buffer.
85 */
86void dmlite_any_to_string(const dmlite_any* any, char* buffer, size_t bsize);
87
88/**
89 * @brief Returns the long interpretation of they dmlite_any.
90 * @details Defaults to 0.
91 * @param any The dmlite_any to convert.
92 */
94
95/**
96 * @brief Returns the int64_t interpretation of they dmlite_any.
97 * @details Defaults to 0.
98 * @param any The dmlite_any to convert.
99 */
100int64_t dmlite_any_to_s64(const dmlite_any* any);
101
102/**
103 * @brief Returns the uint64_t interpretation of they dmlite_any.
104 * @details Defaults to 0.
105 * @param any The dmlite_any to convert.
106 */
107uint64_t dmlite_any_to_u64(const dmlite_any* any);
108
109
110/**
111 * @brief Created a new generic dictionary.
112 * @return A newly allocated dmlite_any_dict.
113 */
115
116/**
117 * @brief Make a copy of the dictionary.
118 * @param dict The original
119 * @return A newly allocated copy of dict.
120 */
122
123/**
124 * @brief Frees a dmlite_any_dict
125 */
127
128/**
129 * @brief Clears the dictionary.
130 */
132
133/**
134 * @brief Insert a new dmlite_any value into the dictionary.
135 * @details Replaces if already present.
136 * @param d The dictionary.
137 * @param k The key.
138 * @param v The value.
139 */
140void dmlite_any_dict_insert(dmlite_any_dict* d, const char* k, const dmlite_any* v);
141
142/**
143 * @brief Returns how many elements there are in a specific dictionary.
144 */
146
147/**
148 * @brief Returns the value associated with the key k.
149 * @return NULL if not found.
150 */
152
153/**
154 * @brief Removes a key-value from the dictionary.
155 * @param d The dictionary.
156 * @param k The key to be removed.
157 */
159
160/**
161 * @brief Generates a JSON serialization of the dictionary.
162 * @return The same pointer as buffer.
163 */
164char* dmlite_any_dict_to_json(const dmlite_any_dict* d, char* buffer, size_t bsize);
165
166/**
167 * @brief Populates a dmlite_any_dict from a JSON string.
168 */
170
171/**
172 * @brief Puts in keys a pointer to an array of strings with all the available
173 * keys in d.
174 * @details Use dmlite_any_dict_keys_free to free.
175 * @param d The Dictionary.
176 * @param nkeys Will be set to the number of stored keys.
177 */
178void dmlite_any_dict_keys(const dmlite_any_dict* d, unsigned* nkeys, char*** keys);
179
180/**
181 * @brief Frees an array of strings allocated by dmlite_any_dict_keys.
182 * @param n The number of keys in **keys
183 * @param keys The array of keys.
184 */
185void dmlite_any_dict_keys_free(unsigned n, char** keys);
186
187#ifdef __cplusplus
188}
189#endif
190
191#endif /* DMLITE_ANY_H */
192
dmlite_any_dict * dmlite_any_dict_from_json(const char *json)
Populates a dmlite_any_dict from a JSON string.
void dmlite_any_to_string(const dmlite_any *any, char *buffer, size_t bsize)
Gets the string interpretation of the dmlite_any.
dmlite_any * dmlite_any_new_s64(int64_t i)
Creates a new dmlite_any from an int64_t type.
void dmlite_any_free(dmlite_any *any)
Frees a dmlite_any.
dmlite_any_dict * dmlite_any_dict_new()
Created a new generic dictionary.
dmlite_any * dmlite_any_new_string(const char *str)
Creates a new dmlite_any.
long dmlite_any_to_long(const dmlite_any *any)
Returns the long interpretation of they dmlite_any.
void dmlite_any_dict_insert(dmlite_any_dict *d, const char *k, const dmlite_any *v)
Insert a new dmlite_any value into the dictionary.
unsigned long dmlite_any_dict_count(const dmlite_any_dict *d)
Returns how many elements there are in a specific dictionary.
dmlite_any * dmlite_any_new_string_array(unsigned n, const char **strv)
Creates a new dmlite_any.
dmlite_any * dmlite_any_new_long_array(unsigned n, long *lv)
Creates a new dmlite_any.
dmlite_any * dmlite_any_dict_get(const dmlite_any_dict *d, const char *k)
Returns the value associated with the key k.
uint64_t dmlite_any_to_u64(const dmlite_any *any)
Returns the uint64_t interpretation of they dmlite_any.
void dmlite_any_dict_keys(const dmlite_any_dict *d, unsigned *nkeys, char ***keys)
Puts in keys a pointer to an array of strings with all the available keys in d.
void dmlite_any_dict_erase(dmlite_any_dict *d, const char *k)
Removes a key-value from the dictionary.
struct dmlite_any dmlite_any
Used to pass configuration values.
Definition: any.h:20
void dmlite_any_dict_keys_free(unsigned n, char **keys)
Frees an array of strings allocated by dmlite_any_dict_keys.
dmlite_any * dmlite_any_new_long(long l)
Creates a new dmlite_any.
void dmlite_any_dict_free(dmlite_any_dict *d)
Frees a dmlite_any_dict.
struct dmlite_any_dict dmlite_any_dict
Handles key->value pairs.
Definition: any.h:25
dmlite_any_dict * dmlite_any_dict_copy(const dmlite_any_dict *dict)
Make a copy of the dictionary.
int64_t dmlite_any_to_s64(const dmlite_any *any)
Returns the int64_t interpretation of they dmlite_any.
char * dmlite_any_dict_to_json(const dmlite_any_dict *d, char *buffer, size_t bsize)
Generates a JSON serialization of the dictionary.
void dmlite_any_dict_clear(dmlite_any_dict *d)
Clears the dictionary.
dmlite_any * dmlite_any_new_u64(uint64_t i)
Creates a new dmlite_any from an uint64_t type.