I would like to be able to switch my feed between an image, a video and a webcam.
Atm i try this:
#define F_WEBCAM
#define F_VIDEO
#define F_IMAGE
#define FEED(F_WEBCAM)
Somewhere else:
#if defined(FEED) && FEED == F_WEBCAM
ofVideoGrabber vidGrabber;
#elif defined(FEED) && FEED == F_VIDEO
ofVideoPlayer vidPlayer;
#elif defined(FEED) && FEED == F_IMAGE
// code for image
#endif
But i get the following error:
Expected value in expression
Is this possible the way i want?
To compare, you need to define your macro constants with values. This will solve your issue:
#define F_WEBCAM 1
#define F_VIDEO 2
#define F_IMAGE 3
#define FEED F_WEBCAM
Related
In C++, I have some #define and also a count like this:
#define USE_1
#undef USE_2
#define USE_3
const size_t NUM_USE = (
0
#ifdef USE_1
+ 1
#endif
#ifdef USE_2
+ 1
#endif
#ifdef USE_3
+ 1
#endif
);
Now, I want to use it like this, which does not work as const variable cannot be used in a #if preprocessor statement:
#if NUM_USE > 0
// define here some specific code
#endif
One way to solve it would be to make some extra defines like this:
#ifdef USE_1
#define HAVE_ANY_USE
#else
#ifdef USE_2
#define HAVE_ANY_USE
#else
#ifdef USE_3
#define HAVE_ANY_USE
#endif
#endif
#endif
#ifdef HAVE_ANY_USE
// define here some specific code
#endif
Is there a more elegant solution, maybe by using NUM_USE from above?
You can define the USEs to be either 1 or 0 and then the sum can be a simple macro:
#define USE_1 1
#define USE_2 0
#define USE_3 1
#define NUM_USE (USE_1 + USE_2 + USE_3)
I'm trying to run the expression _setmode only if I'm using Windows, and setlocale only if I'm using Linux, but I can't manage to make them work with a simple if-else inside a function due to Linux having errors with the Windows libraries, and vice versa.
#if defined(_WIN32) || defined(_WIN64) || (defined(__CYGWIN__) && !defined(_WIN32))
#define PLATFORM_NAME 0
#include <io.h>
#include <fcntl.h>
#elif defined(__linux__)
#define PLATFORM_NAME 1
#include <locale>
#elif defined(__APPLE__) && defined(__MACH__)
#include <TargetConditionals.h>
#if TARGET_OS_MAC == 1
#define PLATFORM_NAME 2
#endif
#else
#define PLATFORM_NAME NULL
#endif
#if PLATFORM_NAME == 0
_setmode(_fileno(stdout), _O_U8TEXT);
#endif
#if PLATFORM_NAME == 1
setlocale(LC_CTYPE, "");
#endif
If you write OS-dependent* code (as in this case), you can manage it at compile-time**. To do this, we need two parts:
Define OS-dependent constants (optional, if condition simple, this part can be omitted):
#if defined(_WIN32)
#define PLATFORM_NAME 0
#include <fcntl.h>
#include <io.h>
#elif defined(__linux__)
#define PLATFORM_NAME 1
#include <locale>
#endif
In needed place call OS-dependent code with preprocessor conditions:
#if PLATFORM_NAME == 0
//Windows code here
#endif
You can write more complex conditions:
#if PLATFORM_NAME == 0
//Windows code here
#elif PLATFORM_NAME != 0
//Non-Windows code here
#if PLATFORM_NAME == 1 || PLATFORM_NAME == 2
//Linux or unknown OS code here
#endif
#endif
See restrictions of conditions here
Tip: If your code have entrypoint (main function as example), you can call most of OS-dependent code at main if this help to reduce code. In library you can place OS-dependent code to dedicated source-file functions like there. Usage preprocessor-time code is good method for writing zero-cost runtime code because of preprocessor just remove all sources,if they're not met conditions.
* - or whatever-dependent 😃
** - more precisely, preprocessor-time
Source: GNU, Microsoft docs.
I was checking some import statement, and found some import statement is in the pattern like this:
#ifdef A
#ifdef B
//SOME SETTINGS 1 (some include,define,ifdef...)
#else
//SOME SETTINGS 2 (some include,define,ifdef...)
#endif
#else
#ifndef B
//SOME SETTINGS 1 (some include,define,ifdef...)
#else
//SOME SETTINGS 2 (some include,define,ifdef...)
#endif
#endif
which the macro of A is just inverse the macro of B,but "//SOME SETTINGS 1" and "//SOME SETTINGS 2" need to appear twice, so I try to rewrite that:
#ifdef A
#ifdef B
#else
#ifndef B
#endif
//SOME SETTINGS 1 (some include,define,ifdef...)
#else
//SOME SETTINGS 2 (some include,define,ifdef...)
#endif
but it failed to compile, is there any syntax to simulate this case that "//SOME SETTINGS 1" and "//SOME SETTINGS 2" only need to appear once?
Your problem is this:
#ifdef A
#ifdef B
#else
#ifndef B
#endif
//SOME SETTINGS 1 (some include,define,ifdef...)
#else
//SOME SETTINGS 2 (some include,define,ifdef...)
#endif
You have a mismathed number of #ifdefs, elses and endifs.
To do what you asked for:
#if (defined(A) && defined(B)) || (!defined(A) && !defined(B))
// settings 1
#else
// settings 2
#endif
This is how you generally approach these kind of problems. You set up a Karnaugh truth table:
A B => 1
A !B => 2
!A B => 2
!A !B => 1
Reading this table alound means you need to use settings 1 for when (A and B) are defined OR when (A is not defined and B is not defined)... hence in C:
(A && B) || (!A && !B)
Convert that to ifdef syntax and you're good to go.
Yes, you can use the defined operator (evaluates to 1 if the identifier is defined and 0 otherwise). You basically seem to want SOME SETTING 1 if A and B are defined or both undefined and SOME SETTINGS 2 otherwise:
#if defined(A) == defined(B)
//SOME SETTINGS 1 (some include,define,ifdef...)
#else
//SOME SETTINGS 2 (some include,define,ifdef...)
#endif
The direct comparision is because either you want defined(A) and defined(B) to be both one or both zero - and one and zero is the only alternatives for them.
Note that if you want to extend this to more than two alternatives you have to be careful to use #elif because SOME SETTINGS 1 can alter subsequent tests if you repeat #if-#endif groups - that's why we need to use #else for the SOME SETTINGS 2 part. For example:
#if COND1(A,B):
// SOME SETTINGS 1
#elif COND2(A,B):
// SOME SETTINGS 2
// etc
#else
// DEFAULT SETTINGS
#endif
You want settings 1 when A & B are defined, or niether A or B are defined. This should do the trick:
#if (defined (A) && defined(B)) || (!defined (A) && !defined(B))
//SOME SETTINGS 1 (some include,define,ifdef...)
#else
//SOME SETTINGS 2 (some include,define,ifdef...)
#endif
What about this:
#if (defined A && defined B) || (!defined A && !defined B)
//SOME SETTINGS 1 (some include,define,ifdef...)
#else
//SOME SETTINGS 2 (some include,define,ifdef...)
#endif
You can combine the checks for definition like this.
#if (defined(A) && defined(B)) || (!defined(A) && !defined(B))
// SOME SETTINGS 1
#else
// SOME SETTINGS 2
#endif
i have a problem with resources in my audio plugin
here is my resource file
// Unique IDs for each image resource.
#define BCKG_ID 101
#define CANT_ID 102
#define COM10_ID 103
#define COM20_ID 104
#define COM40_ID 105
#define COM80_ID 106
#define SAVE_ID 107
#define WAIT_ID 108
#define ONOFFBYPASS_ID 109
#define ONOFFPRESSED_ID 110
// Image resource locations for this plug.
#define BCKG_FN "resources/img/background.png"
#define CANT_FN "resources/img/cant.png"
#define COM10_FN "resources/img/combo10.png"
#define COM20_FN "resources/img/combo20.png"
#define COM40_FN "resources/img/combo40.png"
#define COM80_FN "resources/img/combo80.png"
#define SAVE_FN "resources/img/savwav.png"
#define WAIT_FN "resources/img/waiting.png"
#define ONOFFBYPASS_FN "resources/img/onoff-bypass.png"
#define ONOFFPRESSED_FN "resources/img/onoff-pressed.png"
i have an assert issue : "file not found" when using this code :
IBitmap onoff1 = pGraphics->LoadIBitmap(ONOFFBYPASS_ID, ONOFFBYPASS_FN, 1);
if i use WAIT_ID instead of ONOFFBYPASS_ID, everything works
in debug assert this code raises a flag:
IBitmap IGraphics::LoadIBitmap(int ID, const char* name, int nStates, bool framesAreHoriztonal)
{
LICE_IBitmap* lb = s_bitmapCache.Find(ID);
if (!lb)
{
lb = OSLoadBitmap(ID, name);
#ifndef NDEBUG
bool imgResourceFound = lb;
#endif
assert(imgResourceFound); **//imgResourceFound = false**
s_bitmapCache.Add(lb, ID);
}
return IBitmap(lb, lb->getWidth(), lb->getHeight(), nStates, framesAreHoriztonal);
}
i tried to :
switch ID values (109 <-> 108)
change the names
check 10 times the paths
but nothing works
it doesn't makes sense, especially because i have 2 other audio plugins with the same part of code that are working ok...
sorry can't provide sample code as it would mean installing VST SDK, WDL-OK...so a bit too much i guess.
please help anyway
Jeff
oh i made a noob error: i forget to add
ONOFFBYPASS_ID PNG ONOFFBYPASS_FN
ONOFFPRESSED_ID PNG ONOFFPRESSED_FN
to MyProg.rc
now everything works
Jeff
Is there a way to escape macro names (identifiers) in a c pre processor (cpp) ?
I want to conditionalize some web code (html, css...) with readable macro names.
Example for a conditional css file:
/*root*/
some rootcode
#if height==480
/* height 480 */
.page {
line-height:23px;
}
#elif height<480
/* height < 480 */
.page {
line-height:46px;
}
#endif
An invocation of
cpp -P -D height=480 -oout.css css.ccss
results in (after deleting newlines)
some rootcode
.page {
line-480:23px;
}
but "line-480" is wrong.
Is there a way to escape "height" in the code without changing the macro name or stringify it?
You can either:
1) undefine the macro:
#undef height
2) rename the macro using standard-like caps:
#define HEIGHT
3) Use guards before processing the file:
#if height==480
#define HEIGHT_480
#undef height
#endif
#if height>480
#define HEIGHT_OVER_480
#undef height
#endif
/*root*/
some rootcode
#if HEIGHT_480
/* height 480 */
.page {
line-height:23px;
}
#elif HEIGHT_OVER_480
/* height < 480 */
.page {
line-height:46px;
}
#endif
The first one loses information after the undefine. The second one is impractical if there's extensive use of the macro.
The third one is the best option IMO. I've seen it used in production code where stuff like this was necessary.
I played with the idea of Luchian Grigore to undefine used filenames and I found a (nearly) general solution for this problem:
including a "define.file" at the begin of the conditionals and a "undefine.file" after the given condition.
So the problem is reduced to two macro names, that have to stay: DEFINEFILE und UNDEFINEFILE. But this two macros could be encrypted with their hashcode or with a randomname to avoid a use of this names in the conditional text.
The "define.file":
#define height 480
#define os 1
The "undefine.file"
#undef height
#undef os
the "conditionalcss.ccss"
/*root*/
some rootcode
#include __DEFINEFILENAMEPATH__
#if height==480
#include __UNDEFINEFILENAMEPATH__
/* height 480 */
.page {
line-height:23px;
}
#include __DEFINEFILENAMEPATH__
#elif height<480
#include __UNDEFINEFILENAMEPATH__
/* height > 480 */
.page {
line-height:46px;
}
#include __DEFINEFILENAMEPATH__
#endif
#if os==1
#include __UNDEFINEFILENAMEPATH__
os is windows (if 1 refers to windows)
and height also undefined i hope
#endif
Finally the cppcall with the parametric define and undefine files:
cpp -P -D __DEFINEFILENAMEPATH__="\"define.file\"" -D __UNDEFINEFILENAMEPATH__="\"undefine.file\"" -oout.css css.ccss
With this idea, the restulting "out.css" looks like:
some rootcode
.page {
line-height:23px;
}
os is windows (if 1 refers to windows)
and height also undefined i hope
This solution has only the disadvantages of two macros and a possible poor performace because of the multiple imports.
I hope it helps other people to solve their problem.
Greetz
Adreamus