Let’s say my C++ code has some object that needs to be used throughout the program:
class API { API(); ~API(); void methodA() { std::cout << "called method A\n"; } void methodB() { std::cout << "called method B\n"; }}API api* = new API();extern "C" K methodA(K x) { api->methodA(); R 0; }extern "C" K methodB(K x) { api->methodB(); R 0; }
My Q code only knows about methodA
and methodB
loaded through the 2: function.
How do you go about managing to creating/destruction of API (and use of r0/r1)? I assume this is a common scenario when using C++ libraries in C.
Thanks
I would use a std::map< std::uintptr_t , boost::shared_ptr< API > > repository to manage the API objects.<o:p></o:p>
<o:p> </o:p>
Code snippet taken from https://github.com/kimtang/bml/blob/master/bml.cpp :<o:p></o:p>
|
kx::K uniform_01()<o:p></o:p>
|
| |
{<o:p></o:p>
|
| |
boost::random::uniform_01<>* dst = new boost::random::uniform_01<>();<o:p></o:p>
|
| |
std::uintptr_t ptr = reinterpret_cast<long>(dst);<o:p></o:p>
|
| |
bml::random::distribution_map[ptr] = dst;<o:p></o:p>
|
| |
return kx::kj(ptr);<o:p></o:p>
|
| |
}<o:p></o:p>
|
<o:p> </o:p>
Happy to hear others people opinion and solutions.<o:p></o:p>
<o:p> </o:p>
Kim<o:p></o:p>
<o:p> </o:p>
<o:p> </o:p>
Von: personal-kdbplus@googlegroups.com [mailto:personal-kdbplus@googlegroups.com] Im Auftrag von Morten Sorensen
Gesendet: Samstag, 9. April 2016 20:22
An: Kdb+ Personal Developers
Betreff: [personal kdb+] Object creation/destruction in C++<o:p></o:p>
<o:p> </o:p>
Let’s say my C++ code has some object that needs to be used throughout the program:<o:p></o:p>
<o:p> </o:p>
class API {
API();
~API();
void methodA() { std::cout << “called method A\n”; }
void methodB() { std::cout << “called method B\n”; }
}
API api* = new API();
extern “C” K methodA(K x) { api->methodA(); R 0; }
extern “C” K methodB(K x) { api->methodB(); R 0; }<o:p></o:p>
<o:p> </o:p>
My Q code only knows about methodA
and methodB
loaded through the 2: function.<o:p></o:p>
<o:p> </o:p>
How do you go about managing to creating/destruction of API (and use of r0/r1)? I assume this is a common scenario when using C++ libraries in C.<o:p></o:p>
<o:p> </o:p>
Thanks<o:p></o:p>
–
Submitted via Google Groups
you can use the pattern that linux and kdb+ use for managing lifetimes:
eg. in linux: : open/close and in kdb+: hopen/hclose
so in the library:
extern “C”{
K open(K x){api=new API();R 0;}
K close(K x){delete api;R 0;}
K methodA(K x) { api->methodA(); R 0; }
K methodB(K x) { api->methodB(); R 0; }
}
also on linux (and i think on windows) there is a mechanism for functions to be called on opening and closing the library:
$ man dlopen
..
libraries should export routines using the attribute ((constructor)) and attribute ((destructor)) function attributes. See the gcc info pages for information on these. Constructor routines are executed before dlopen() returns, and destructor routines are executed before dlclose() returns.
..
Thanks both! I found the __attribute__((constructor)) / __attribute__((destructor)) quite straightforward to use and works on both GCC and clang:
__attribute__ ((constructor))static void initialize_api() { api = new API();} __attribute__ ((destructor))static void destroy_api() { delete api;}
There’s an equivalent function DllMain() available for MSVC: http://stackoverflow.com/questions/20724628/attribute-destructor-equivalent-in-vc