Skip to content

Integrating Cxx code into Cando

Christian Schafmeister edited this page Dec 31, 2019 · 1 revision

Add the new .cc and .h source files.

For example - I added two C++ source files to Cando: cando/src/chem/maxcliqueseq.cc and cando/include/cando/chem/maxcliqueseq.h.

In the maxcliqueseq case a new class chem::Dimacs_O is exposed to Common Lisp.

I tell the garbage collector how to deal with the new class using this:

template <>
struct gctools::GCInfo<chem::Dimacs_O> {
  static bool constexpr CanAllocateWithNoArguments = true;
  static bool constexpr NeedsInitialization = true;
  static bool constexpr NeedsFinalization = true;
  static GCInfo_policy constexpr Policy = normal;
};

The NeedsFinalization = true tells the garbage collector that when it collects the object that it should invoke the destructor for the C++ class. This is necessary because the Dimacs_O class includes fields that are C++ std objects like 'vector' and they need to be cleaned up or they will leak memory.

If you don't use C++ standard classes - then nothing needs to be cleaned up. Clasp's classes are largely designed so that they don't need finalization or cleanup - they can be simply abandoned by the garbage collector and this is more efficient.

Add the name of the .cc file without the extension to cando/src/chem/wscript.

This ensures that the file is compiled and installed with the rest of the source code.

Add the header file maxcliqueseq.h to the cando/include/cando/main/project_headers.h file.

This ensures that the classes defined in the header file are known to the garbage collector.

Add any functions that you want to expose by adding things like this to the .cc file.

CL_DEFUN
core::T_sp chem__find_maximum_clique_search(Dimacs_sp dimacsGraph, int numThreads, int numJobs) {
   ...
}