Is struct dot style initializers supported in C++ Builder 32bit? - c++builder

The following initializer is supported in Embarcadero C++ Builder 32bit?
struct test_s test = { .first = 1, .third = 3, .second = 2 };
It works on the 64bit target platform however I think it not works on 32bit platform.
I tried it in C++ Builder 10.1 .

The next code
struct test_s
{
int first;
int second;
int third;
};
int _tmain(int argc, _TCHAR* argv[])
{
test_s test = {.first = 1, .second = 2, .third = 3};
return 0;
}
compiles with C++ Builder 10.4.1 under Windows 64 bit platform (BCC64/Clang) as well as under Windows 32 bit platform if "Use classic Borland compiler" option is unchecked (BCC32C/Clang). Both compilers are based on LLVM ver. 5.0.2.

Related

Version Information in Embarcadero C++ Builder at compile time

Is it somehow possible to get the projects version info (major/minor/release/build) at compile time? Something like this?
#if MAJOR_VERSION=2
#include <fancyheader.h>
#else
#include <differentCoolStuff.h>
#endif
yours
Herwig
This can be done. Reading the verson number from the exe file ensures that the version number you read (and probably display or change s/w operation based on it) matches the value that a user sees if they check the exe file version information using the Windows
"properties" check, so it makes a lot of sense. This code works with VCL framework using C++ Builder 10.4 (Enterprise edition - but it should work with any edition).
String ExeFileName = ParamStr(0);
unsigned long Handle;
int VersionInfoSize = GetFileVersionInfoSize(ExeFileName.c_str(), &Handle);
int VersionInfo[4];
void *FileInfo;
unsigned int Length;
//if size is zero, then there is no version info in the exe
if (VersionInfoSize > 0) {
char *Buffer = new char[VersionInfoSize];
try {
GetFileVersionInfo(ExeFileName.c_str(), 0, VersionInfoSize, Buffer);
VerQueryValue(Buffer, L"\\", &FileInfo, &Length);
TVSFixedFileInfo *Info = static_cast<TVSFixedFileInfo *>(FileInfo);
VersionInfo[0] = ((Info->dwFileVersionMS) >> 16);
VersionInfo[1] = ((Info->dwFileVersionMS) & 0xffff);
VersionInfo[2] = ((Info->dwFileVersionLS) >> 16);
VersionInfo[3] = ((Info->dwFileVersionLS) & 0xffff);
} catch (...) {
VersionInfo[0] = 99;
VersionInfo[1] = 88;
VersionInfo[2] = 77;
VersionInfo[3] = 66;
}
delete[] Buffer;
// DO WHAT YOU WANT WITH VersionInfo HERE !!!
}

Is there a simple way to convert a lua table to a C++ array or vector?

I am starting to make my own package manager and am starting to develop a dependency system.
The builfiles are written in lua, they look something like this:
package = {
name = "pfetch",
version = "0.6.0",
source = "https://github.com/dylanaraps/pfetch/archive/0.6.0.tar.gz",
git = false
}
dependencies = {
"some_dep",
"some_dep2"
}
function install()
quantum_install("pfetch", false)
end
Only problem,I have no idea how to convert
dependencies = {
"some_dep",
"some_dep2"
}
To a global c++ array: ["some_dep", "some_dep2"]
Anything in the list that's not valid as a string should be ignored.
Any good way to do this?
Thanks in advance
Note: I am using the C api to interface with lua in C++. I don't know whether Lua's errors use longjmp or C++ exceptions.
Based on the clarification in your comment, something like this will work for you:
#include <iostream>
#include <string>
#include <vector>
#include <lua5.3/lua.hpp>
std::vector<std::string> dependencies;
static int q64795651_set_dependencies(lua_State *L) {
dependencies.clear();
lua_settop(L, 1);
for(lua_Integer i = 1; lua_geti(L, 1, i) != LUA_TNIL; ++i) {
size_t len;
const char *str = lua_tolstring(L, 2, &len);
if(str) {
dependencies.push_back(std::string{str, len});
}
lua_settop(L, 1);
}
return 0;
}
static int q64795651_print_dependencies(lua_State *) {
for(const auto &dep : dependencies) {
std::cout << dep << std::endl;
}
return 0;
}
static const luaL_Reg q64795651lib[] = {
{"set_dependencies", q64795651_set_dependencies},
{"print_dependencies", q64795651_print_dependencies},
{nullptr, nullptr}
};
extern "C"
int luaopen_q64795651(lua_State *L) {
luaL_newlib(L, q64795651lib);
return 1;
}
Demo:
$ g++ -fPIC -shared q64795651.cpp -o q64795651.so
$ lua5.3
Lua 5.3.3 Copyright (C) 1994-2016 Lua.org, PUC-Rio
> q64795651 = require('q64795651')
> dependencies = {
>> "some_dep",
>> "some_dep2"
>> }
> q64795651.set_dependencies(dependencies)
> q64795651.print_dependencies()
some_dep
some_dep2
>
One important pitfall: since you're not sure if Lua is compiled to use longjmp or exceptions for its errors, you need to make sure that you don't have any automatic variables with destructors anywhere that a Lua error could happen. (This is already the case in the code in my answer; just make sure you don't accidentally add any such places when incorporating this into your program.)

WebAssembly.instantiate fails when wasm file was compiled with `clang++`, but works with `clang`

When C/C++ .wasm code is compiled with clang (C) - it loads in Chrome and works well, but when with clang++ (C++) - wasm load fails with error (in JS console):
Uncaught (in promise) LinkError: WebAssembly.instantiate(): Import #1 module="wasi_snapshot_preview1" function="fd_close" error: function import requires a callable
Why?
WASM compile arguments:
"clang", <=== I only changed this to "clang++" - and it fails
"-O0",
// "-std=c++14",
"--target=wasm32-unknown-wasi",
"--sysroot C:\\OpenGL\\wasi-sdk-11.0-mingw.tar\\wasi-sdk-11.0\\share\\wasi-sysroot",
"-fno-exceptions",
"-nostartfiles",
"-Wl,--import-memory",
"-Wl,--no-entry",
"-Wl,--export-all",
"-o templates/my-app/public/hello_wasm.wasm",
"wasm/hello_wasm.cpp"
JS wasm load code:
const response = await fetch("./hello_wasm.wasm");
const bytes = await response.arrayBuffer();
const { instance } = await WebAssembly.instantiate(bytes, {
env: { memory: this.memory },
},
});
this.instance = instance;
console.log("c" + instance);
})();
hello_wasm.cpp (compilation was without an error):
#include <math.h>
// extern "C"
// {
int int_sqrt(int x)
{
return sqrt(x);
};
float *run_sin(float x[], int n)
{
// float *a = new float[n];
float *a = (float *)malloc(sizeof(float) * n);
for (int i = 0; i < n; i++)
{
a[i] = x[i] * 2;
}
return a;
}
LLVM v10
I use wasi sysroot from https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-11/wasi-sdk-11.0-mingw.tar.gz
Also discussing this issue here https://github.com/WebAssembly/wasi-sdk/issues/145
In order to run a WASI binary on the web you need to provide an implementation of the WASI APIs. The web platform does not natively support WASI. There are some polyfills out there that try to emulate some/all of the WASI APIs which might work for your case.

opencv 2.4.8 microsoft surface pro 2 camera

I built opencv 2.4.8 x86 using visual studio express 2012, with media foundation support, and then wrote a simple program to test camera, all things done on Microsoft surface pro 2.
Problem 1, image shown in window is upside down
problem 2, setting image width and height not work
code is pasted here, and thank you for help!
#include <iostream>
#include <opencv2\opencv.hpp>
bool quitNow=false;
void mouseCallBackFunc(int event, int x, int y, int flags, void* userdata){
if (event==CV_EVENT_LBUTTONDBLCLK) quitNow=true;
}
int main()
{
int key;
std::string winName="show cam";
cv::namedWindow(winName,1);
cv::setMouseCallback(winName,mouseCallBackFunc);
cv::Mat img;
cv::VideoCapture vc(1);
if (vc.isOpened()){
std::cout<<"cam open good\n";
}else{
std::cout<<"cam open bad\n";
vc.release();
return 2;
}
if (!vc.set(CV_CAP_PROP_FRAME_HEIGHT,720)) return 3;
if (!vc.set(CV_CAP_PROP_FRAME_HEIGHT,1280)) return 4;
vc>>img;
std::cout<<std::endl<<"size is "<<img.size()<<std::endl;
do
{
vc>>img;
if(img.empty()) return 9;
cv::imshow(winName,img);
key=cv::waitKey(10) & 0xff;
} while (!quitNow);
return 0;
}

Open CV Image editing library for windows 8 and windows phone 8

Is there any support of OpenCV graphics library is available for Windows Phone 8 and Windows 8. I made a search on Google but didn't find any resource related with OpenCV to connect with Windows Phone 8 / Windows 8. If any of you know more about this please help me, and provide some link to reach the library.
This is the latest information what I get from OpenCV team.
OpenCV development team is working on port for Windows RT. Here is current development branch for WinRT(https://github.com/asmorkalov/opencv/tree/winrt). You can build it for ARM using Visual Studio Express for Windows 8 and Platform SDK.
Open Visual Studio development console.
Setup environment for cross compilation by command "C:\Program Files(x86)\Microsoft
Visual Studio 11.0\VC\bin\x86_arm\vcvarsx86_arm.bat"
cd <opencv_source_dir>/platforms/winrt/
run scripts/cmake_winrt.cmd
run ninja
Alternatively you can use nmake instead ninja. You need to edit cmake_winrt.cmd and change project generator fro -GNinja to -G "NMake Makefiles". Only algorithmic part of the library is supported now, no tbb, no UI, no video IO.
Please check the below given URL from more details.
http://answers.opencv.org/question/9847/opencv-for-windows-8-tablet/?answer=9851#post-id-9851
By windows-8, I guess you mean winRT ? AFAIK, there is no official port to winRT. You need to compile it by yourself as a Win8 Store DLL for instance, so that you can reference it from a Win8 Store Application.
Just start by opencv-core, then add the lib you need, one by one, because all the components will not be able to compile (for instance, opencv-highgui is highly dependant on Windows API which is not fully compatible with Win8 Store Apps).
You'll also need to code by yourself some Win32 methods used by OpenCV and not accessible from Win8 App like GetSystemInfo(), GetTempPathA(), GetTempFileNameA() and all methods related to thread local storage (TLS).
I've been able to use a small subset of OpenCV in WinRT by compiling opencv_core, opencv_imgproc and zlib, as 3 seperate static libs. I've added one another, called opencv_winrt, that contains only the two following files:
opencv_winrt.h
#pragma once
#include "combaseapi.h"
void WINAPI GetSystemInfo(
_Out_ LPSYSTEM_INFO lpSystemInfo
);
DWORD WINAPI GetTempPathA(
_In_ DWORD nBufferLength,
_Out_ char* lpBuffer
);
UINT WINAPI GetTempFileNameA(
_In_ const char* lpPathName,
_In_ const char* lpPrefixString,
_In_ UINT uUnique,
_Out_ char* lpTempFileName
);
DWORD WINAPI TlsAlloc();
BOOL WINAPI TlsFree(
_In_ DWORD dwTlsIndex
);
LPVOID WINAPI TlsGetValue(
_In_ DWORD dwTlsIndex
);
BOOL WINAPI TlsSetValue(
_In_ DWORD dwTlsIndex,
_In_opt_ LPVOID lpTlsValue
);
void WINAPI TlsShutdown();
# define TLS_OUT_OF_INDEXES ((DWORD)0xFFFFFFFF)
opencv_winrt.cpp
#include "opencv_winrt.h"
#include <vector>
#include <set>
#include <mutex>
#include "assert.h"
void WINAPI GetSystemInfo(LPSYSTEM_INFO lpSystemInfo)
{
GetNativeSystemInfo(lpSystemInfo);
}
DWORD WINAPI GetTempPathA(DWORD nBufferLength, char* lpBuffer)
{
return 0;
}
UINT WINAPI GetTempFileNameA(const char* lpPathName, const char* lpPrefixString, UINT uUnique, char* lpTempFileName)
{
return 0;
}
// Thread local storage.
typedef std::vector<void*> ThreadLocalData;
static __declspec(thread) ThreadLocalData* currentThreadData = nullptr;
static std::set<ThreadLocalData*> allThreadData;
static DWORD nextTlsIndex = 0;
static std::vector<DWORD> freeTlsIndices;
static std::mutex tlsAllocationLock;
DWORD WINAPI TlsAlloc()
{
std::lock_guard<std::mutex> lock(tlsAllocationLock);
// Can we reuse a previously freed TLS slot?
if (!freeTlsIndices.empty())
{
DWORD result = freeTlsIndices.back();
freeTlsIndices.pop_back();
return result;
}
// Allocate a new TLS slot.
return nextTlsIndex++;
}
_Use_decl_annotations_ BOOL WINAPI TlsFree(DWORD dwTlsIndex)
{
std::lock_guard<std::mutex> lock(tlsAllocationLock);
assert(dwTlsIndex < nextTlsIndex);
assert(find(freeTlsIndices.begin(), freeTlsIndices.end(), dwTlsIndex) == freeTlsIndices.end());
// Store this slot for reuse by TlsAlloc.
try
{
freeTlsIndices.push_back(dwTlsIndex);
}
catch (...)
{
return false;
}
// Zero the value for all threads that might be using this now freed slot.
for each (auto threadData in allThreadData)
{
if (threadData->size() > dwTlsIndex)
{
threadData->at(dwTlsIndex) = nullptr;
}
}
return true;
}
_Use_decl_annotations_ LPVOID WINAPI TlsGetValue(DWORD dwTlsIndex)
{
ThreadLocalData* threadData = currentThreadData;
if (threadData && threadData->size() > dwTlsIndex)
{
// Return the value of an allocated TLS slot.
return threadData->at(dwTlsIndex);
}
else
{
// Default value for unallocated slots.
return nullptr;
}
}
_Use_decl_annotations_ BOOL WINAPI TlsSetValue(DWORD dwTlsIndex, LPVOID lpTlsValue)
{
ThreadLocalData* threadData = currentThreadData;
if (!threadData)
{
// First time allocation of TLS data for this thread.
try
{
threadData = new ThreadLocalData(dwTlsIndex + 1, nullptr);
std::lock_guard<std::mutex> lock(tlsAllocationLock);
allThreadData.insert(threadData);
currentThreadData = threadData;
}
catch (...)
{
if (threadData)
delete threadData;
return false;
}
}
else if (threadData->size() <= dwTlsIndex)
{
// This thread already has a TLS data block, but it must be expanded to fit the specified slot.
try
{
std::lock_guard<std::mutex> lock(tlsAllocationLock);
threadData->resize(dwTlsIndex + 1, nullptr);
}
catch (...)
{
return false;
}
}
// Store the new value for this slot.
threadData->at(dwTlsIndex) = lpTlsValue;
return true;
}
// Called at thread exit to clean up TLS allocations.
void WINAPI TlsShutdown()
{
ThreadLocalData* threadData = currentThreadData;
if (threadData)
{
{
std::lock_guard<std::mutex> lock(tlsAllocationLock);
allThreadData.erase(threadData);
}
currentThreadData = nullptr;
delete threadData;
}
}
And I modify the file cvconfig.h: I've commented out every #define, except PACKAGE* and VERSION, and I added #include "opencv_winrt.h" at the end.
Just a hint - there is a C# wrapper for OpenCV called EmguCV (http://www.emgu.com/wiki/index.php/Main_Page), by looking at the forum posts I see that there is some activity towards using it on Windows 8 but it's hard to tell if it's now working since the posts claiming issues are quite old. I'd suggest you just give it a try and see if this C# wrapper is able to run on Windows Phone 8, I think it should definitely run on Windows 8.

Resources