STRINGZ_TO_NPVARIANT in xcode problem - xcode

have folowing code:
NPVariant type;
STRINGZ_TO_NPVARIANT("click", type);
and the xcode returns
"error: expected expression before 'uint32_t'"
anyone can give me a hand with this?

I ran into this as well. Based on this bug i found in the npapi project, i solved the problem by not using the macro, and using what it expands to instead, then applying what the patch shows.
http://code.google.com/p/npapi-headers/issues/detail?id=3
- NPString str = { _val, uint32_t(strlen(_val)) };
+ NPString str = { _val, (uint32_t)(strlen(_val)) };
Basically, wrap uint32_t in parenthesis, then gcc will compile it.
So the full replacement for the macro is
NPVariant type;
type.type = NPVariantType_String;
NPString str = { "click", (uint32_t)(strlen("click")) };
type.value.stringValue = str;

If you check what this macro resolves into you get:
NPVariant type;
type.type = NPVariantType_String;
NPString str = { "click", uint32_t(strlen("click")) };
type.value.stringValue = str;
At least this way one can understand what the error message refers to. I'm still not sure what the reason for the error is however - maybe your Xcode or gcc version is outdated. According to the documentation you need at least Xcode 2.5 and gcc 4.2.

in your npruntime.h find these two MACRO
#define STRINGZ_TO_NPVARIANT(_val, _v) \
NP_BEGIN_MACRO \
(_v).type = NPVariantType_String; \
NPString str = { _val, **(uint32_t)** strlen(_val) }; \
(_v).value.stringValue = str; \
NP_END_MACRO
#define STRINGN_TO_NPVARIANT(_val, _len, _v) \
NP_BEGIN_MACRO \
(_v).type = NPVariantType_String; \
NPString str = { _val, **(uint32_t)**_len }; \
(_v).value.stringValue = str; \
NP_END_MACRO
    
add (uint32_t) cast before strlen(_val) and _len

Related

OpenCL 2.1 C++ HelloWorld: invalid context?

Similar to this question, I'm trying to implement the HelloWorld example from this video by Wesley Shillingford, except this time with OpenCL 2.1. I can get it to run if I use the default context, but not if I create my own (as the video does).
When I use my own context, it produces a cl::Error (-34 = CL_INVALID_CONTEXT):
From here:
CL_INVALID_CONTEXT = the given context is invalid OpenCL context, or the context associated with certain parameters are not the same.
I'm not sure how I could tell that the context is invalid. I've tried comparing the defaultContext to myContext, and they match of everything except CL_CONTEXT_REFERENCE_COUNT. Which doesn't seem to matter (but maybe it does).
I could be mixing contexts. However, I assign the context I want to use to chosenContext and use that everywhere I need a context.
It seems that something is somehow using the default context instead of my supplied context, but I haven't been able to spot where. Any insights would be appreciated.
The code:
#define CL_HPP_ENABLE_EXCEPTIONS
#define CL_HPP_TARGET_OPENCL_VERSION 200
#include <CL/cl2.hpp>
#include <fstream>
#include <iostream>
int main()
{
// Get Platform and Device
std::vector<cl::Platform> platforms;
cl::Platform::get(&platforms);
auto platform = platforms.front();
std::vector<cl::Device> devices;
platform.getDevices(CL_DEVICE_TYPE_GPU, &devices);
auto device = devices.front();
//This context doesn't work. Causes CL_INVALID_CONTEXT (-34)
cl_context_properties properties[] = {CL_CONTEXT_PLATFORM, (cl_context_properties)platform(), 0};
cl::Context myContext(device, properties);
//If I stick with the default context, things work.
cl::Context defaultContext = cl::Context::getDefault();
//The choice of context here determines whether it works or not:
// myContext -> Fails with CL_INVALID_CONTEXT (-34)
// defaultContext -> works
auto chosenContext = myContext;
std::ifstream helloWorldFile("hello_world.cl");
std::string src(std::istreambuf_iterator<char>(helloWorldFile), (std::istreambuf_iterator<char>()));
cl::Program program(chosenContext, src);
program.build("-cl-std=CL2.1");
//Debugging code: Check to make sure that the contexts are similar
auto myContextDevices = myContext.getInfo<CL_CONTEXT_DEVICES>();
auto defaultContextDevices = defaultContext.getInfo<CL_CONTEXT_DEVICES>();
auto devicesMatch = myContextDevices == defaultContextDevices; //true
auto myContextProperties = myContext.getInfo<CL_CONTEXT_PROPERTIES>();
auto defaultContextProperties = defaultContext.getInfo<CL_CONTEXT_PROPERTIES>();
auto propertiesMatch = myContextProperties == defaultContextProperties; //true
auto myContextNumDevices = myContext.getInfo<CL_CONTEXT_NUM_DEVICES>();
auto defaultContextNumDevices = defaultContext.getInfo<CL_CONTEXT_NUM_DEVICES>();
auto numDevicesMatch = myContextNumDevices == defaultContextNumDevices; //true
auto myContextRefCount = myContext.getInfo<CL_CONTEXT_REFERENCE_COUNT>(); // 1 if defaultContext, 3 if myContext
auto defaultContextRefCount = defaultContext.getInfo<CL_CONTEXT_REFERENCE_COUNT>(); // 4 if defaultContext, 2 if myContext
auto refCountsMatch = myContextRefCount == defaultContextRefCount; // false
auto contextsMatch = myContext == defaultContext; //false
//End of debugging code
//Continuing with computation
char buf[16];
cl::Buffer outputBuffer = cl::Buffer(CL_MEM_WRITE_ONLY | CL_MEM_HOST_READ_ONLY, sizeof(buf));
cl::Kernel kernel(program, "HelloWorld");
kernel.setArg(0, outputBuffer);
cl::CommandQueue commandQueue(chosenContext, device);
auto result = commandQueue.enqueueNDRangeKernel(kernel, 0, 1, 1); //CL_SUCCESS
commandQueue.enqueueReadBuffer(outputBuffer, CL_TRUE, 0, sizeof(buf), buf); // Execution fails here, raises cl::Error (-34)
std::cout << buf;
return EXIT_SUCCESS;
}
Build Command:
g++ -g hello_world_21.cpp -IOpenCL-Headers/opencl21 -std=c++11 -lOpenCL
hello_world.cl:
__kernel void HelloWorld(__global char* output) {
output[0] = 'H';
output[1] = 'e';
output[2] = 'l';
output[3] = 'l';
output[4] = 'o';
output[5] = ' ';
output[6] = 'W';
output[7] = 'o';
output[8] = 'r';
output[9] = 'l';
output[10] = 'd';
output[11] = '!';
output[12] = '\n';
}
You are still using the default context for the global memory buffer instead of using your own context:
cl::Buffer outputBuffer = cl::Buffer(CL_MEM_WRITE_ONLY | CL_MEM_HOST_READ_ONLY, sizeof(buf));
Just change this line to the following and it should work:
cl::Buffer outputBuffer = cl::Buffer(myContext, CL_MEM_WRITE_ONLY | CL_MEM_HOST_READ_ONLY, sizeof(buf));

What is the point of window_magic in SDL?

While browsing the SDL source code that interfaces with the system window manager, I encountered struct SDL_VideoDevice with a mystical Uint8 window_magic field. There doesn't seem to be any documentation for this field. What is the purpose of this field? It plays a role in the following functions/macros (all defined in SDL_video.c).
The macro CHECK_WINDOW_MAGIC:
#define CHECK_WINDOW_MAGIC(window, retval) \
if (!_this) { \
SDL_UninitializedVideo(); \
return retval; \
} \
if (!window || window->magic != &_this->window_magic) { \
SDL_SetError("Invalid window"); \
return retval; \
}
The function SDL_GetWindowWMInfo:
SDL_GetWindowWMInfo(SDL_Window * window, struct SDL_SysWMinfo *info)
{
CHECK_WINDOW_MAGIC(window, SDL_FALSE);
if (!info) {
SDL_InvalidParamError("info");
return SDL_FALSE;
}
info->subsystem = SDL_SYSWM_UNKNOWN;
if (!_this->GetWindowWMInfo) {
SDL_Unsupported();
return SDL_FALSE;
}
return (_this->GetWindowWMInfo(_this, window, info));
}
The function SDL_CreateWindow:
SDL_Window *
SDL_CreateWindow(const char *title, int x, int y, int w, int h, Uint32 flags)
{
...
window->magic = &_this->window_magic;
...
}
_this is a pointer to an SDL_VideoDevice, which is initialized when the user calls SDL_Init. When the user calls SDL_CreateWindow, SDL assignes window->magic to the address of _this->window_magic. As far as I can tell, _this->window_magic is never initialized anywhere in SDL. What could possibly be the role of this value?
It uses address of _this->window_magic, not its value. Variable don't have to be initialised to have an address, especially if it is a structure field.
Its purpose is to quickly check against uninitialised window, or not a window at all. No guarantees but hit rate is very high - it is very unlikely that anything will accidentially match the address of SDL's internal structure.

warning: format not a string literal and no format arguments just in some GCC

gcc -v:
gcc version 4.9.2 (Ubuntu 4.9.2-10ubuntu13)
#define d_write_log_evolved(old_fmt, args...) \
do \
{ char new_fmt[2048] = {0}; \
time_t timep; \
struct tm *p; \
timep = time(&timep); \
p = localtime(&timep); \
sprintf(new_fmt, "%d-%d-%d %d:%d:%d [process %d][thread %lu]: ", \
(int)(1900+p->tm_year), \
(int)(1+p->tm_mon), \
(int)(p->tm_mday), \
(int)(p->tm_hour), \
(int)(p->tm_min), \
(int)(p->tm_sec), \
getpid(), \
pthread_self()); \
strcat(new_fmt, old_fmt); \
FILE *logfp = fopen(CCDAEMON_LOG, "a"); \
fprintf(logfp, new_fmt, ##args); \
fclose(logfp); \
} \
while (0)
d_write_log_evolved("[err] compile %s failed, thread will exit.\n", file);//here no warning.
d_write_log_evolved("socket() error, thread exit.\n"); //here have the warning.
When I pass some args to d_write_log_evolved(), then there's no warning reported, but without args, there always warning:warning: format not a string literal and no format arguments [-Wformat-security]
This really trouble me although program can be running properly.
BTW, there's no such warning under Scientific Linux.
Could you tell me how to eliminate this warning? Thanks!
PS. below is preprocessing result.
without agrs:
do { char new_fmt[2048] = {0}; time_t timep; struct tm *p; timep = time(&timep); p = localtime(&timep); sprintf(new_fmt, "%d-%d-%d %d:%d:%d [process %d][thread %lu]: ", (int)(1900+p->tm_year), (int)(1+p->tm_mon), (int)(p->tm_mday), (int)(p->tm_hour), (int)(p->tm_min), (int)(p->tm_sec), getpid(), pthread_self()); strcat(new_fmt, "socket() error, thread exit.\n"); FILE *logfp = fopen("/tmp/ccdcc.log", "a"); fprintf(logfp, new_fmt); fclose(logfp); } while (0);
with args:
do { char new_fmt[2048] = {0}; time_t timep; struct tm *p; timep = time(&timep); p = localtime(&timep); sprintf(new_fmt, "%d-%d-%d %d:%d:%d [process %d][thread %lu]: ", (int)(1900+p->tm_year), (int)(1+p->tm_mon), (int)(p->tm_mday), (int)(p->tm_hour), (int)(p->tm_min), (int)(p->tm_sec), getpid(), pthread_self()); strcat(new_fmt, "[ERR] send msg to client %s failed.\n"); FILE *logfp = fopen("/tmp/ccdcc.log", "a"); fprintf(logfp, new_fmt, inet_ntoa(host.sin_addr)); fclose(logfp); } while (0);

Command Line Parameters Through WinMain and CommandLineToArgvW

I'm encountering a problem when trying to pass a parameter through my program via the command line (eg. -w 1280 -h 1024) while attempting to utilize WinMain. I've looked through every topic I could find, and have created code that builds and runs, but the parameters are ignored completely!
My Code:
LPWSTR *szArgList;
int argCount;
szArgList = CommandLineToArgvW(GetCommandLineW(), &argCount);
for(int i = 1;i < argCount;i++)
{
if(i + 1 != argCount)
{
if(szArgList[i] == L"-w")
{
width = _wtoi(szArgList[i+1]);
}
else if(szArgList[i] == L"-h")
{
height = _wtoi(szArgList[i+1]);
}
}
}
MSG msg;
BOOL done=FALSE;
if(MessageBox(NULL,"Fullscreen?", "my window", MB_YESNO|MB_ICONQUESTION)==IDNO)
{
fullscreen=FALSE;
}
if(!CreateGLWindow("Window",width,height,16,fullscreen))
{
return 0;
}
I'm attempting to pass it as "window.exe -w 800 -h 600" (without quotes, of course)
Anything i'm missing within my sleep-depraved code?
szArgList[i] == L"-w"
szArgList[i] == L"-h"
C and C++ will compare by pointer instead of character. use strcmp.

Multilined define directives in CLI

Im trying to use quite long define in my CLI program but i'm getting dosens of errors because of that.
This is my code.
#define OPERATION_MACRO(character) \
array<Byte>^ opRes = gcnew array<Byte>(args[0]->Length); \
for(int i=0;i<args[0]->Length;i++) \
{ \
double result=0; \
for(int k=0;k<args->Length;k++) \
{ \
result character##= args[k][i]; \
} \
graphicUtils::round(result); \
opRes[i] = result; \
} \
return opRes;
array<Byte>^ myInterpreter::mathOp(array< array<Byte>^ >^ args, Char opr)
{
switch(opr)
{
case '*':
OPERATION_MACRO(*)
case '/':
OPERATION_MACRO(/)
case '+':
OPERATION_MACRO(+)
case '-':
OPERATION_MACRO(-)
case '%':
OPERATION_MACRO(%)
case '^':
OPERATION_MACRO(^)
}
}
Is it because of different new line character in CLI compiler?

Resources