-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
244 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* M*LIB - Generic module | ||
* | ||
* Copyright (c) 2017-2023, Patrick Pelissier | ||
* All rights reserved. | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* + Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* + Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY | ||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY | ||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
#ifndef MSTARLIB_GENERIC_H | ||
#define MSTARLIB_GENERIC_H | ||
|
||
#include "m-core.h" | ||
|
||
/* 3 levels of indirection are neeeded: | ||
* One level is the organisation. | ||
* One level is the component of the organisation. | ||
* One level is the oplist within the component of the organisation | ||
Return the list of unevaled oplist that are registered accorss all organisations, all components. | ||
MLIB Organisation is the default organisation and is always registered | ||
Usage Domain: Nb organizations * Nb components * Nb oplists < M_MAX_NB_ARGUMENT | ||
*/ | ||
#define M_G3N_REGISTERED_ITEMS() \ | ||
M_CROSS_MAP( M_G3N_IS_PRESENT3, ( comp M_CROSS_MAP(M_G3N_IS_PRESENT2, (MLIB M_MAP(M_G3N_IS_PRESENT, M_SEQ(1, 50))), ( M_SEQ(1, 50) ) ) ), ( M_SEQ(1, 50) ) ) | ||
|
||
#define M_G3N_IS_PRESENT(num) \ | ||
M_IF(M_PARENTHESIS_P(M_C(M_GENERIC_ORG_, num)() ))(M_DEFERRED_COMMA M_ID M_C(M_GENERIC_ORG_, num)(), ) | ||
|
||
#define M_G3N_IS_PRESENT2(el1, num) \ | ||
M_IF(M_PARENTHESIS_P(M_C4(M_GENERIC_ORG_, el1, _COMP_, num)()))(M_DEFERRED_COMMA M_APPLY(M_C3, el1, _COMP_, M_ID M_C4(M_GENERIC_ORG_, el1, _COMP_, num)()), ) | ||
|
||
#define M_G3N_IS_PRESENT3(el1, num) \ | ||
M_IF(M_OPLIST_P(M_C4(M_GENERIC_ORG_, el1, _OPLIST_, num)()))(M_DEFERRED_COMMA M_C4(M_GENERIC_ORG_, el1, _OPLIST_, num), ) | ||
|
||
// Dummy unused structure. Just to fill in default case of the _Generic | ||
struct m_g3neric_dummys; | ||
|
||
// Call the OPERATOR call of the oplist registered to the variable 'x' | ||
// which takes one argument. | ||
#define M_G3N_CALL_1(call, x) \ | ||
_Generic( ((void)0, (x)), \ | ||
M_MAP2(M_G3N_CALL_1_func, (call, x) M_G3N_REGISTERED_ITEMS() ) \ | ||
struct m_g3neric_dummys *: /* cannot happen */ (void)0) | ||
#define M_G3N_CALL_1_func(x, oplist) \ | ||
M_G3N_CALL_1_func_expand(M_GET_GENTYPE oplist(), M_PAIR_1 x, M_PAIR_2 x, oplist) | ||
#define M_G3N_CALL_1_func_expand(gentype, call, x, oplist) \ | ||
gentype: call(oplist(), M_AS_TYPE(gentype, x)), | ||
|
||
// Call the OPERATOR call of the oplist registered to the variable 'x' | ||
// which takes two argument (type, type) | ||
#define M_G3N_CALL_2(call, x, y) \ | ||
_Generic( ((void)0, (x)), \ | ||
M_MAP2(M_G3N_CALL_2_func, (call, x, y) M_G3N_REGISTERED_ITEMS() ) \ | ||
struct m_g3neric_dummys *: /* cannot happen */ (void) 0) | ||
#define M_G3N_CALL_2_func(x, oplist) \ | ||
M_G3N_CALL_2_func_expand(M_GET_GENTYPE oplist(), M_TRIPLE_1 x, M_TRIPLE_2 x, M_TRIPLE_3 x, oplist) | ||
#define M_G3N_CALL_2_func_expand(gentype, call, x, y, oplist) \ | ||
gentype: call(oplist(), M_AS_TYPE(gentype, x), M_AS_TYPE(gentype, y)), | ||
|
||
// Call the OPERATOR call of the oplist registered to the variable 'x' | ||
// which takes two argument (type, subtype) | ||
#define M_G3N_CALL_2t(call, x, y) \ | ||
_Generic( ((void)0, (x)), \ | ||
M_MAP2(M_G3N_CALL_2t_func, (call, x, y) M_G3N_REGISTERED_ITEMS() ) \ | ||
struct m_g3neric_dummys *: /* cannot happen */ (void) 0) | ||
#define M_G3N_CALL_2t_func(x, oplist) \ | ||
M_G3N_CALL_2t_func_expand(M_GET_GENTYPE oplist(), \ | ||
M_IF_METHOD(GENTYPE, M_GET_OPLIST oplist())(M_GET_GENTYPE M_GET_OPLIST oplist(), M_GET_SUBTYPE oplist()), \ | ||
M_TRIPLE_1 x, M_TRIPLE_2 x, M_TRIPLE_3 x, oplist) | ||
#define M_G3N_CALL_2t_func_expand(gentype, basictype, call, x, y, oplist) \ | ||
gentype: call(oplist(), M_AS_TYPE(gentype, x), M_AS_TYPE(basictype, y)), | ||
|
||
|
||
// Define generic functions | ||
// TODO: m_ prefix? | ||
#define init(x) M_G3N_CALL_1(M_CALL_INIT, x) | ||
#define init_set(x, y) M_G3N_CALL_2(M_CALL_INIT_SET, x, y) | ||
#define set(x, y) M_G3N_CALL_2(M_CALL_SET, x, y) | ||
#define clear(x) M_G3N_CALL_1(M_CALL_CLEAR, x) | ||
#define init_move(x, y) M_G3N_CALL_2(M_CALL_INIT_MOVE, x, y) | ||
#define push(x, y) M_G3N_CALL_2t(M_CALL_PUSH, x, y) | ||
|
||
|
||
/* User code has to register oplists : | ||
#define M_GENERIC_ORG_2() (USER) | ||
#define M_GENERIC_ORG_USER_COMP_1() (CORE) | ||
#define M_GENERIC_ORG_USER_COMP_CORE_OPLIST_6() FLT1 | ||
#define M_GENERIC_ORG_USER_COMP_CORE_OPLIST_7() STR1 | ||
*/ | ||
|
||
// Don't need to register M*LIB organisation as it is the default one | ||
//#define M_GENERIC_ORG_1() (MLIB) | ||
|
||
// TODO: Register CORE component | ||
// TODO: Register oplist to CORE component (m-string ? serial ?) | ||
// TODO: Add GENTYPE to all types | ||
//#define M_GENERIC_ORG_MLIB_COMP_1() (CORE) | ||
//#define M_GENERIC_ORG_MLIB_COMP_CORE_OPLIST_1() STR1 | ||
//#define M_GENERIC_ORG_MLIB_COMP_CORE_OPLIST_5() FLT1 | ||
|
||
|
||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
* Copyright (c) 2017-2023, Patrick Pelissier | ||
* All rights reserved. | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* + Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* + Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY | ||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY | ||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
#include <stdbool.h> | ||
#include <assert.h> | ||
#include "coverage.h" | ||
|
||
#include "m-string.h" | ||
#include "m-generic.h" | ||
|
||
// Serial json is not supported for standard types if not C11 | ||
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L | ||
|
||
static void h(string_t x) | ||
{ | ||
string_set_str(x, "TEST STRING"); | ||
} | ||
|
||
const string_t gx; | ||
|
||
|
||
/* | ||
init(x) | ||
init_set(x,y) | ||
set(x,y) | ||
clear(x) | ||
init_move(x,y) | ||
init_with(x, ...) | ||
swap(x,y) | ||
reset(x) | ||
bool empty_p(x) | ||
size_t get_size(x) | ||
size_t hash(x) | ||
bool equal(x, y) | ||
int cmp(x,y) | ||
add(x, y) | ||
sub(x, y) | ||
mul(x, y) | ||
div(x, y) | ||
T* get(x, key) | ||
set(x, key, value) | ||
T *safe_get(x, key) | ||
erase(x, key) | ||
push(x, y) | ||
pop(x, y) | ||
... | ||
pas de pb | ||
pb for iterator through "each": | ||
FIXME: uses of C23 typeof + extensions ? | ||
*/ | ||
|
||
#define STR1 M_OPEXTEND(STRING_OPLIST, GENTYPE(struct m_string_s *)) | ||
#define FLT1 (GENTYPE(float), TYPE(float), INIT(M_INIT_DEFAULT)) | ||
|
||
//#define M_GENERIC_ORG_MLIB_COMP_1() (CORE) | ||
//#define M_GENERIC_ORG_MLIB_COMP_CORE_OPLIST_1() STR1 | ||
//#define M_GENERIC_ORG_MLIB_COMP_CORE_OPLIST_5() FLT1 | ||
|
||
#define M_GENERIC_ORG_2() (USER) | ||
#define M_GENERIC_ORG_USER_COMP_1() (CORE) | ||
#define M_GENERIC_ORG_USER_COMP_CORE_OPLIST_6() FLT1 | ||
#define M_GENERIC_ORG_USER_COMP_CORE_OPLIST_7() STR1 | ||
|
||
// typeof(_Generic(...) ) ? | ||
// GCC/CLANG/TCC: __extension__ __typeof__ | ||
// MSVC++: decltype | ||
// C23; typeof | ||
|
||
static void test_string(string_t p) | ||
{ | ||
string_t s , d; | ||
// ‘_Generic’ selector of type ‘const struct m_string_s *’ is not compatible with any association | ||
//init(gx); | ||
|
||
init(s); | ||
h(s); | ||
init_set(d, s); | ||
h(d); | ||
init_set(p, gx); | ||
clear(s); | ||
clear(d); | ||
|
||
float f; | ||
init(f); | ||
clear(f); | ||
clear(p); | ||
// clear(gx); | ||
} | ||
|
||
int main(void) | ||
{ | ||
string_t p; | ||
test_string(p); | ||
|
||
exit(0); | ||
} | ||
|
||
#else | ||
int main(void) | ||
{ | ||
exit(0); | ||
} | ||
#endif |