Comments (2)
This is a great idea!
I'm not familiar enough with GTK to see how they get away with this. Just calling a function/macro with more arguments than it has declared will surely fire error messages from most C compilers.
Or am I misunderstanding you? I would appreciate a more detailed sketch of how this would look.
from cgreen.
Ok, I will try to explain this whole C function pointers magic above:
The most important thing is to handle two
function signatures with a one macro. This can be done
with little bit of casting. Take a look at this trivial program:
#include <stdio.h>
typedef void (*Func2)(int, int);
typedef void (*Func1)(int);
void func(void) {
printf("LaLa\n");
}
int main()
{
Func2 func2 = (Func2)&func;
Func1 func1 = (Func1)&func;
func2(4,5);
func1(5);
return 0;
}
It will compile and run on any platform. However, this doesn't mean this
is a C-compilant program, because standard doesn't define
such calls at any point.
For more information please visit:
https://stackoverflow.com/questions/559581/casting-a-function-pointer-to-another-type
As one reply suggest, this is technically undefined behavior., but the second
one suggests that whole GNOME framework depends upon it, because of ABI
compatibility.
After some reconsideration I figured out we might use some other,
more standard C tricks to handle two types with a single macro.
In C11 there is a _Generic macro, which can be useful:
#include <stdio.h>
typedef void (*FuncA)(void);
typedef void (*FuncB)(int);
void funcA(void) {
printf("LaLa\n");
}
void funcB(int unused) {
(void)unused;
printf("TaTa\n");
}
void callA(FuncA a) {
a();
}
void callB(FuncB b) {
b(0);
}
#define CALL(x) _Generic(x, FuncA: callA, default: callB)(x)
int main()
{
CALL(funcA);
CALL(funcB);
return 0;
}
I personally like the second approach more sue to standard compliance
and cleaner error messages. If that approach is to be chosen sth like
with_parametrized_side_effects needs to be provided for for
older C versions.
Hope all is clear by now ;)
from cgreen.
Related Issues (20)
- Ability to return captured values by mocks. HOT 2
- CMake warning: Could NOT find LibBfd HOT 4
- Never seem to get a cgreen-runner executable HOT 5
- The libbfd dependence issue in Debian HOT 14
- Test helper scripts should use sh instead of bash for maximum portability
- Revert dependency on libbfd HOT 1
- Make learning mocks print immediately HOT 4
- Add documentation on compile time configuration
- create_xml_reporter() crashes on MSYS platform HOT 2
- CUTE reporter final report statistics zero
- CUTE reporter per test time invalid HOT 2
- Option for unbuffered stdout for cgreen-runner HOT 1
- cannot pass unit test in i386 HOT 8
- Test signal HOT 1
- [1.6.2] Version marked as 1.6.1 in the Cmake file HOT 1
- "cgreen-runner --quiet" results in "unrecognised option" HOT 3
- "cgreen-runner --verbose" is not HOT 2
- Default text reporter does not show names of failed tests HOT 2
- cannot pass unit test in s390x HOT 31
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cgreen.