2016.04.09 11:22 AM
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; }
2016.04.09 01:51 PM
I would use a std::map< std::uintptr_t , boost::shared_ptr< API > > repository to manage the API objects.
Code snippet taken from https://github.com/kimtang/bml/blob/master/bml.cpp :
kx::K uniform_01() | |
{ | |
boost::random::uniform_01<>* dst = new boost::random::uniform_01<>(); | |
std::uintptr_t ptr = reinterpret_cast<long>(dst); | |
bml::random::distribution_map[ptr] = dst; | |
return kx::kj(ptr); | |
} |
Happy to hear other’s people opinion and solutions.
Kim
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++
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
--
You received this message because you are subscribed to the Google Groups "Kdb+ Personal Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to personal-kdbplus+unsubscribe@googlegroups.com.
To post to this group, send email to personal-kdbplus@googlegroups.com.
Visit this group at https://groups.google.com/group/personal-kdbplus.
For more options, visit https://groups.google.com/d/optout.
2016.04.09 04:38 PM
2016.04.09 11:49 PM
__attribute__((constructor))
static void initialize_api() {
api = new API();
}
__attribute__((destructor))
static void destroy_api() {
delete api;
}
EMEA
Tel: +44 (0)28 3025 2242
AMERICAS
Tel: +1 (212) 447 6700
APAC
Tel: +61 (0)2 9236 5700
KX. All Rights Reserved.
KX and kdb+ are registered trademarks of KX Systems, Inc., a subsidiary of FD Technologies plc.