Coder Social home page Coder Social logo

jcelerier / dynalizer Goto Github PK

View Code? Open in Web Editor NEW
18.0 3.0 0.0 20 KB

C++ Dynamic loader generator for C APIs

License: GNU Affero General Public License v3.0

Python 81.16% C++ 18.84%
cpp python libclang dynamic-loading dynamic-library shared-object

dynalizer's Introduction

Dynalizer

A python script which generates a C++ dynamic loader for shared objects, given a C header file. Useful when you don't want to link at compile-time, and if you want to have a beginning of C++ API for your C code.

Dependencies

pip install clang inflection

Example

$ echo 'void mylib_foo(int x, const char* bar);\nvoid mylib_bar(float** z);' > mylib.h
$ python dynalizer.py header.h

will produce a class akin to the following :

class mylib
{
public:
  // Pass the path to your .so / .dll / .dylib here
  explicit mylib(const char* filepath)
    : m_lib{filepath}
  {
    m_sym_mylib_foo = m_lib.symbol<decltype(::mylib_foo)*>("mylib_foo");
    if(!m_sym_mylib_foo)
      throw std::runtime_error("Invalid symbol lookup: mylib_foo");

    m_sym_mylib_bar = m_lib.symbol<decltype(::mylib_bar)*>("mylib_bar");
    if(!m_sym_mylib_bar)
      throw std::runtime_error("Invalid symbol lookup: mylib_bar");
  }

  static auto& instance() {
    static mylib c;
    return c;
  }
  
  template<typename... Args>
  auto mylib_foo(Args&&... args) -> decltype(auto)
  {
    return m_sym_mylib_foo(std::forward<Args>(args)...);
  }

  template<typename... Args>
  auto mylib_bar(Args&&... args) -> decltype(auto)
  {
    return m_sym_mylib_bar(std::forward<Args>(args)...);
  }

private:
  decltype(::mylib_foo)* m_sym_mylib_foo{};
  decltype(::mylib_bar)* m_sym_mylib_bar{};

  dynalizer::library m_lib;
};

Options

Multiple codegen aspects can be changed:

  • Lazy (--lazy) and eager (--eager) loading: in the eager case, all the symbols are loaded upon instantiation of the library. In the lazy case, symbols are not loaded until they are actually used.

  • Error reporting : --throw and --abort. These options define what happens when a symbol is missing: an exception, or an assertion.

  • Prettification : --pretty. If possible, will remove common suffixes of the API functions, and will port to standard C++ snake_case.

    That is, the earlier example would become instead:

template<typename... Args>
auto foo(Args&&... args) -> decltype(auto)
{
  return m_sym_mylib_foo(std::forward<Args>(args)...);
}

template<typename... Args>
auto bar(Args&&... args) -> decltype(auto)
{
  return m_sym_mylib_bar(std::forward<Args>(args)...);
}

so that usage patterns can be akin to :

int main() {
  auto& pa = portaudio::instance();
  pa.open_stream(...);
  pa.close_stream(...);
}

instead of the much heavier

int main() {
  auto& pa = portaudio::instance();
  pa.Pa_OpenStream(...);
  pa.Pa_CloseStream(...);
}

dynalizer's People

Contributors

jcelerier avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

dynalizer's Issues

Support APIs with multiple headers

e.g. JACK, alsa, SDL...

Possibilities :

  • Multiple objects, put the dll instance in a shared_ptr
  • Nested objects with references to the parent
  • Everything in a big class

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.