-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make C++ compatible #13
Changes from all commits
cc5aae0
80e4736
7817302
1ab4233
eee0fbc
845a54b
31c6ce0
8381645
0b67235
ce2aa0c
fd90b78
bc67214
8a3b233
79a51a3
077c389
6523f25
d5c59f9
04b825e
fb0e5b3
7be3805
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
!.cmake/* | ||
|
||
!*.c | ||
!*.cpp | ||
!*.h | ||
!*.S | ||
!*.asm | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,16 @@ | ||
#ifndef MMK_ALLOC_H_ | ||
# define MMK_ALLOC_H_ | ||
|
||
# ifdef __cplusplus | ||
extern "C" { | ||
# endif | ||
|
||
void *mmk_malloc(size_t size); | ||
void *mmk_realloc(void *ptr, size_t size); | ||
void mmk_free(void *ptr); | ||
|
||
# ifdef __cplusplus | ||
} | ||
# endif | ||
|
||
#endif /* !MMK_ALLOC_H_ */ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright © 2016 Franklin "Snaipe" Mathieu <http://snai.pe/> | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
#ifndef MMK_LITERAL_H_ | ||
# define MMK_LITERAL_H_ | ||
|
||
# undef mmk_struct_literal | ||
|
||
# ifdef __cplusplus | ||
|
||
# include <cstdarg> | ||
# include <utility> | ||
|
||
# include "preprocess.h" | ||
|
||
template<typename T, int ID> | ||
struct mmk_literal { | ||
static constexpr int id = ID; | ||
static T storage; | ||
}; | ||
|
||
template<typename T, int ID> | ||
T mmk_literal<T, ID>::storage; | ||
|
||
template <typename T> | ||
T & mmk_assign(T & dst, T src) { | ||
return dst = std::move(dst); | ||
} | ||
|
||
template <typename T, size_t N> | ||
T (& mmk_assign(T (&dst)[N], T * src))[N] { | ||
if (src) { | ||
mmk_memcpy(dst, src, sizeof(T) * N); | ||
} else { | ||
mmk_memset(dst, 0, sizeof(T) * N); | ||
} | ||
return dst; | ||
} | ||
|
||
va_list & mmk_assign(va_list & dst, va_list src) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That should probably be inline There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point. |
||
// no-op | ||
return dst; | ||
} | ||
|
||
# define MMK_COMMA_APPEND(_, prefix, content) (void) (prefix content), | ||
|
||
# define mmk_struct_initialize(variable, ...) \ | ||
(MMK_EXPAND(MMK_APPLY_N_(MMK_COMMA_APPEND, MMK_VA_NARGS(__VA_ARGS__), variable, __VA_ARGS__)) \ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hah, what a clever hack -- I love it! |
||
variable) | ||
|
||
# define mmk_struct_literal(type, ...) \ | ||
mmk_struct_initialize((mmk_literal<type, __COUNTER__>::storage), __VA_ARGS__) | ||
|
||
# define mmk_literal(type, value) \ | ||
(mmk_assign(mmk_literal<type, __COUNTER__>::storage, value)) | ||
|
||
# else /* !defined __cplusplus */ | ||
|
||
# define mmk_assign(dst, src) (dst) = (src) | ||
|
||
# define mmk_literal(type, value) ((type) value) | ||
|
||
# define mmk_struct_literal(type, ...) ((type) { __VA_ARGS__ }) | ||
|
||
# endif | ||
|
||
#endif /* !MMK_LITERAL_H_ */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This breaks the API as far as I'm concerned. I'd rather have mmk_stub be an alias for
struct mmk_stub *
. I would be fine ifstruct mmk_stub
was renamed tostruct mmk_stub_
however.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does break API, but I could only find one place where the
mmk_stub
alias was being used instead ofstruct mmk_stub *
. I figured I'd normalize to thestruct <name>
syntax instead.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is mostly about API usage -- mmk_stub is an opaque type, so having the user type
struct mmk_stub
in C is just misleading because you'd expect to be able to change or read some fields in there.In other words, I am fine with keeping struct and union keywords when it's possible to access the underlying type, and this is not one of these situations.