cancel
Showing results for 
Search instead for 
Did you mean: 

Object creation/destruction in C++

samsogade
New Contributor
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
3 REPLIES 3

kuentang
New Contributor

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.

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