Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
ccan
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
mirror
ccan
Commits
11e4a54f
Commit
11e4a54f
authored
Jan 11, 2009
by
Rusty Russell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
make const and volatile-qualified callbacks actually work.
parent
e5ee09b2
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
120 additions
and
30 deletions
+120
-30
ccan/typesafe_cb/test/compile_ok-typesafe_cb-const.c
ccan/typesafe_cb/test/compile_ok-typesafe_cb-const.c
+45
-0
ccan/typesafe_cb/test/compile_ok-typesafe_cb-volatile.c
ccan/typesafe_cb/test/compile_ok-typesafe_cb-volatile.c
+45
-0
ccan/typesafe_cb/typesafe_cb.h
ccan/typesafe_cb/typesafe_cb.h
+30
-30
No files found.
ccan/typesafe_cb/test/compile_ok-typesafe_cb-const.c
0 → 100644
View file @
11e4a54f
#include "typesafe_cb/typesafe_cb.h"
#include <stdlib.h>
/* const args in callbacks should be OK. */
static
void
_register_callback
(
void
(
*
cb
)(
void
*
arg
),
void
*
arg
)
{
}
#define register_callback(cb, arg) \
_register_callback(typesafe_cb(void, (cb), (arg)), (arg))
static
void
_register_callback_pre
(
void
(
*
cb
)(
int
x
,
void
*
arg
),
void
*
arg
)
{
}
#define register_callback_pre(cb, arg) \
_register_callback_pre(typesafe_cb_preargs(void, (cb), (arg), int), (arg))
static
void
_register_callback_post
(
void
(
*
cb
)(
void
*
arg
,
int
x
),
void
*
arg
)
{
}
#define register_callback_post(cb, arg) \
_register_callback_post(typesafe_cb_postargs(void, (cb), (arg), int), (arg))
static
void
my_callback
(
const
char
*
p
)
{
}
static
void
my_callback_pre
(
int
x
,
const
char
*
p
)
{
}
static
void
my_callback_post
(
const
char
*
p
,
int
x
)
{
}
int
main
(
int
argc
,
char
*
argv
[])
{
register_callback
(
my_callback
,
"hello world"
);
register_callback_pre
(
my_callback_pre
,
"hello world"
);
register_callback_post
(
my_callback_post
,
"hello world"
);
return
0
;
}
ccan/typesafe_cb/test/compile_ok-typesafe_cb-volatile.c
0 → 100644
View file @
11e4a54f
#include "typesafe_cb/typesafe_cb.h"
#include <stdlib.h>
/* volatile args in callbacks should be OK. */
static
void
_register_callback
(
void
(
*
cb
)(
void
*
arg
),
void
*
arg
)
{
}
#define register_callback(cb, arg) \
_register_callback(typesafe_cb(void, (cb), (arg)), (arg))
static
void
_register_callback_pre
(
void
(
*
cb
)(
int
x
,
void
*
arg
),
void
*
arg
)
{
}
#define register_callback_pre(cb, arg) \
_register_callback_pre(typesafe_cb_preargs(void, (cb), (arg), int), (arg))
static
void
_register_callback_post
(
void
(
*
cb
)(
void
*
arg
,
int
x
),
void
*
arg
)
{
}
#define register_callback_post(cb, arg) \
_register_callback_post(typesafe_cb_postargs(void, (cb), (arg), int), (arg))
static
void
my_callback
(
volatile
char
*
p
)
{
}
static
void
my_callback_pre
(
int
x
,
volatile
char
*
p
)
{
}
static
void
my_callback_post
(
volatile
char
*
p
,
int
x
)
{
}
int
main
(
int
argc
,
char
*
argv
[])
{
register_callback
(
my_callback
,
"hello world"
);
register_callback_pre
(
my_callback_pre
,
"hello world"
);
register_callback_post
(
my_callback_post
,
"hello world"
);
return
0
;
}
ccan/typesafe_cb/typesafe_cb.h
View file @
11e4a54f
...
...
@@ -38,7 +38,7 @@ __builtin_choose_expr(__builtin_types_compatible_p(typeof(1?(expr):0), oktype),
/**
* typesafe_cb - cast a callback function if it matches the arg
* @r
et
type: the return type of the callback function
* @rtype: the return type of the callback function
* @fn: the callback function to cast
* @arg: the (pointer) argument to hand to the callback function.
*
...
...
@@ -54,18 +54,18 @@ __builtin_choose_expr(__builtin_types_compatible_p(typeof(1?(expr):0), oktype),
* #define register_callback(fn, arg) \
* _register_callback(typesafe_cb(void, (fn), (arg)), (arg))
*/
#define typesafe_cb(r
et
type, fn, arg) \
#define typesafe_cb(rtype, fn, arg) \
cast_if_type(cast_if_type(cast_if_type((fn), \
r
ettype (*)(const typeof(arg)
), \
r
et
type (*)(void *)), \
r
ettype (*)(volatile typeof(arg)
), \
r
et
type (*)(void *)), \
r
et
type (*)(typeof(arg)), \
r
et
type (*)(void *))
r
type (*)(const typeof(*arg)*
), \
rtype (*)(void *)), \
r
type (*)(volatile typeof(*arg) *
), \
rtype (*)(void *)), \
rtype (*)(typeof(arg)), \
rtype (*)(void *))
/**
* typesafe_cb_preargs - cast a callback function if it matches the arg
* @r
et
type: the return type of the callback function
* @rtype: the return type of the callback function
* @fn: the callback function to cast
* @arg: the (pointer) argument to hand to the callback function.
*
...
...
@@ -78,21 +78,21 @@ __builtin_choose_expr(__builtin_types_compatible_p(typeof(1?(expr):0), oktype),
* _register_callback(typesafe_cb_preargs(void, (fn), (arg), int),\
* (arg))
*/
#define typesafe_cb_preargs(r
et
type, fn, arg, ...) \
#define typesafe_cb_preargs(rtype, fn, arg, ...) \
cast_if_type(cast_if_type(cast_if_type((fn), \
r
et
type (*)(__VA_ARGS__, \
const typeof(arg)),
\
r
et
type (*)(__VA_ARGS__, \
void *)), \
r
et
type (*)(__VA_ARGS__, \
volatile typeof(arg)
), \
r
et
type (*)(__VA_ARGS__, void *)), \
r
et
type (*)(__VA_ARGS__, typeof(arg)), \
r
et
type (*)(__VA_ARGS__, void *))
rtype (*)(__VA_ARGS__, \
const typeof(*arg) *),
\
rtype (*)(__VA_ARGS__, \
void *)), \
rtype (*)(__VA_ARGS__, \
volatile typeof(*arg) *
), \
rtype (*)(__VA_ARGS__, void *)), \
rtype (*)(__VA_ARGS__, typeof(arg)), \
rtype (*)(__VA_ARGS__, void *))
/**
* typesafe_cb_postargs - cast a callback function if it matches the arg
* @r
et
type: the return type of the callback function
* @rtype: the return type of the callback function
* @fn: the callback function to cast
* @arg: the (pointer) argument to hand to the callback function.
*
...
...
@@ -105,16 +105,16 @@ __builtin_choose_expr(__builtin_types_compatible_p(typeof(1?(expr):0), oktype),
* _register_callback(typesafe_cb_preargs(void, (fn), (arg), int),\
* (arg))
*/
#define typesafe_cb_postargs(r
et
type, fn, arg, ...) \
#define typesafe_cb_postargs(rtype, fn, arg, ...) \
cast_if_type(cast_if_type(cast_if_type((fn), \
r
ettype (*)(const typeof(arg)
, \
__VA_ARGS__),
\
r
et
type (*)(void *, \
__VA_ARGS__)), \
r
ettype (*)(volatile typeof(arg)
, \
__VA_ARGS__), \
r
et
type (*)(void *, __VA_ARGS__)), \
r
et
type (*)(typeof(arg), __VA_ARGS__), \
r
et
type (*)(void *, __VA_ARGS__))
r
type (*)(const typeof(*arg) *
, \
__VA_ARGS__),
\
rtype (*)(void *, \
__VA_ARGS__)), \
r
type (*)(volatile typeof(*arg) *
, \
__VA_ARGS__), \
rtype (*)(void *, __VA_ARGS__)), \
rtype (*)(typeof(arg), __VA_ARGS__), \
rtype (*)(void *, __VA_ARGS__))
#endif
/* CCAN_CAST_IF_TYPE_H */
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment