All Projects → TheLartians → Glue

TheLartians / Glue

Licence: mit
⛓️ Bindings that stick. A simple and generic API for C++ to other language bindings supporting bidirectional communication, inheritance and automatic declarations.

Programming Languages

typescript
32286 projects
cpp
1120 projects

Projects that are alternatives of or similar to Glue

Autophrase
AutoPhrase: Automated Phrase Mining from Massive Text Corpora
Stars: ✭ 835 (+1797.73%)
Mutual labels:  automatic, multi-language
Codegen
A model-view based code generator written in Java
Stars: ✭ 36 (-18.18%)
Mutual labels:  automatic
Embeddinator 4000
Tools to turn .NET libraries into native libraries that can be consumed on Android, iOS, Mac, Linux and other platforms.
Stars: ✭ 735 (+1570.45%)
Mutual labels:  bindings
Auto Cpufreq
Automatic CPU speed & power optimizer for Linux
Stars: ✭ 843 (+1815.91%)
Mutual labels:  automatic
Ecommerce Codeigniter Bootstrap
Responsive, Multi-Vendor, MultiLanguage Online Store Platform (shopping cart solution)
Stars: ✭ 788 (+1690.91%)
Mutual labels:  multi-language
Soloud Rs
Rust bindings for the soloud audio engine library
Stars: ✭ 21 (-52.27%)
Mutual labels:  bindings
Opensource Socialnetwork
Open Source Social Network (OSSN) is a social networking software written in PHP. It allows you to make a social networking website and helps your members build social relationships, with people who share similar professional or personal interests. It is available in 16 international languages.
Stars: ✭ 710 (+1513.64%)
Mutual labels:  multi-language
Vst2
Bindings for vst2 sdk
Stars: ✭ 39 (-11.36%)
Mutual labels:  bindings
Itri Speech Recognition Dataset Generation
Automatic Speech Recognition Dataset Generation
Stars: ✭ 32 (-27.27%)
Mutual labels:  automatic
Tablebinding
Swift NSTableView bound to an NSArrayController
Stars: ✭ 8 (-81.82%)
Mutual labels:  bindings
Gopherjs Electron
Gopherjs bindings for Electron with an API translator.
Stars: ✭ 26 (-40.91%)
Mutual labels:  bindings
Automatic Ripping Machine
Automatic Ripping Machine (ARM) Scripts
Stars: ✭ 827 (+1779.55%)
Mutual labels:  automatic
Rdrpostagger
R package for Ripple Down Rules-based Part-Of-Speech Tagging (RDRPOS). On more than 45 languages.
Stars: ✭ 31 (-29.55%)
Mutual labels:  multi-language
Nvk
Vulkan API for JavaScript/TypeScript
Stars: ✭ 774 (+1659.09%)
Mutual labels:  bindings
Auto Frontend
Automatically generate frontends for go programs
Stars: ✭ 37 (-15.91%)
Mutual labels:  automatic
Wrap
The easy to use Swift JSON encoder
Stars: ✭ 725 (+1547.73%)
Mutual labels:  automatic
Organize
The file management automation tool.
Stars: ✭ 883 (+1906.82%)
Mutual labels:  automatic
Rusqlite
Ergonomic bindings to SQLite for Rust
Stars: ✭ 1,008 (+2190.91%)
Mutual labels:  bindings
Ad Edwarthogenhancedscript
An Advanced & Highly Customisable Elite Dangerous Thrustmaster Warthog Script + ED Bindings Pack that utilises Modifiers, allowing for all commands to be easily accessible on the HOTAS. Includes many Quality of Life features to get the most enjoyment out of ED!
Stars: ✭ 39 (-11.36%)
Mutual labels:  bindings
Cuda
Experiments with CUDA and Rust
Stars: ✭ 31 (-29.55%)
Mutual labels:  bindings

Actions Status Actions Status Actions Status Actions Status Actions Status codecov

Glue

A common interface for C++ to other language bindings.

Motivation

C++ is a great language for writing algorithms and high-performance code. However, once it comes to building applications, servers or websites, simple things become complex and we settle for a scripting language to glue C++ components together. The bindings usually need to be hand-crafted for every exposed type and language. This project aims to create a generic language binding interface in pure standard C++, which allows two-way interactions, automatic declaration creation and relatively short compile times.

API

Maps and values

Glue is based on the Revisited framework and provides convenient wrapper methods. The most important types for creating the API and are glue::Value and glue::MapValue.

#include <glue/value.h>
#include <iostream>

void valueExample() {
  // `glue::Value`s hold an `revisited::Any` type that can store any type of value
  glue::Value value = "Hello glue!";

  // access the any type through the `->` or `*` operator
  // `as<T>()` returns an `std::optional<T>` that is defined if the cast is possible
  std::cout << *value->as<std::string>() << std::endl;

  // `glue::MapValue` is a wrapper for a container that maps strings to values
  // `glue::createAnyMap()` creates a map based on `std::unordered_set`
  // Values also accept lambda functions
  glue::MapValue map = glue::createAnyMap();
  map["myNumber"] = 42;
  map["myString"] = value;
  map["myCallback"] = [](bool a, float b){ return a ? b : -b; };
  map["myMap"] = glue::createAnyMap();

  // use helper functions to cast to maps or callbacks.
  // the result will evaluate to `false` if no cast is possible.
  if (auto f = map["myCallback"].asFunction()) {
    // callbacks are stored as a `revisited::AnyFunction` and can accept both values or `Any` arguments
    // (here `map["myNumber"]` is casted to an `Any` through the `*` operator)
    // `get<T>` returns casts the value to `T` or throws an exception if not possible
    std::cout << f(false, *map["myNumber"]).get<int>() << std::endl;
  }

  // inner maps are also `glue::MapValue`s.
  map["myMap"].asMap()["inner"] = "inner value";
}

Classes

Glue also has built-in support for maps representing classes and inheritance.

#include <glue/class.h>
#include <glue/context.h>
#include <iostream>

struct A {
  std::string member;
};

struct B: public A {
  B(std::string value) : A{value} {}
  int method(int v) { return int(member.size()) + v; }
};

void classExample() {
  auto map = glue::createAnyMap();

  // `glue::createClass<T>` is a convience function for declaring maps for class APIs
  // `addConstructor<Args...>()` adds a function that constructs `A` given the argument types `Args...`
  // `.addMember("name", &T::member)` adds a setter (`member`) and getter (`setMember`) function
  map["A"] = glue::createClass<A>()
    .addConstructor<>()
    .addMember("member", &A::member)
  ;

  // classes can be made inheritance aware
  // `setExtends(map)` uses the argument map or callback to retrieve undefined keys
  // `glue::WithBases<A>()` adds implicit conversions to the listed base classes
  // `addMethod("name", &T::method)` adds a method that calls a member function or lambda
  map["B"] = glue::createClass<B>(glue::WithBases<A>())
    .setExtends(map["A"])
    .addConstructor<std::string>()
    .addMethod("method", &B::method)
    .addMethod("lambda", [](const B &b, int x){ return b.member.size() + x; })
  ;

  // contexts collect map class data and can be used to test instance creation
  glue::Context context;
  context.addRootMap(map);

  // `glue::Instance` captures a value and behaves as a class instance
  auto b = context.createInstance(map["B"][glue::keys::constructorKey]("arg"));

  // calls will be treated as member functions
  std::cout << b["member"]().get<std::string>() << std::endl;
  b["setMember"]("new value");
  std::cout << b["lambda"](10).get<int>() << std::endl;
}

Declarations

Glue can automatically generate declarations for type-safe scripting using TypeScript or TypeScriptToLua.

glue::DeclarationPrinter printer;
printer.init();
printer.print(std::cout, map, &context);

In the example above, this would result in the following declarations.

/** @customConstructor A.__new */
declare class A {
  constructor()
  member(): string
  setMember(arg1: string): void
}
/** @customConstructor B.__new */
declare class B extends A {
  constructor(arg0: string)
  lambda(arg1: number): number
  method(arg1: number): number
}

Supported bindings

Here you can find current and planned bindings for Glue.

  • [x] Lua: LuaGlue
  • [x] JavaScript (Wasm): EmGlue
  • [ ] JavaScript (Duktape)
  • [ ] Python
  • [ ] Java
  • [ ] Swift
  • [ ] Rust

As you can see, many bindings are still missing from the list. If you've created Glue bindings for any language, feel free to open a PR to add them here.

Limitations

  • No support for pointer or optional arguments in callbacks (yet)
  • Class bindings aren't as sophisticated as other libraries bindings such as sol2
  • There is a small overhead through the additional abstraction layer compared to native bindings

Usage

Glue can be easily added to your project using CPM.cmake.

CPMAddPackage(
  NAME Glue
  VERSION 1.0
  GIT_REPOSITORY https://github.com/TheLartians/Glue.git
)

target_link_libraries(myLibrary Glue)

See here for an example project using Glue to create TypeScript bindings for C++.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].