https://github.com/boolangery/d-autointf
Auto interface implementation generator for D.
https://github.com/boolangery/d-autointf
dlang interface library metaprogramming
Last synced: 3 months ago
JSON representation
Auto interface implementation generator for D.
- Host: GitHub
- URL: https://github.com/boolangery/d-autointf
- Owner: boolangery
- License: mit
- Created: 2018-09-04T16:17:02.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2024-06-25T08:25:58.000Z (about 2 years ago)
- Last Synced: 2025-12-28T18:47:37.819Z (6 months ago)
- Topics: dlang, interface, library, metaprogramming
- Language: D
- Size: 15.6 KB
- Stars: 5
- Watchers: 2
- Forks: 1
- Open Issues: 3
-
Metadata Files:
- Readme: README.rst
- License: LICENSE.txt
Awesome Lists containing this project
README
autointf
===============================================================================
.. image:: https://img.shields.io/dub/v/autointf.svg
:target: https://code.dlang.org/packages/autointf
An helper-library to auto-generate interface implementation from a
template function.
Installation:
------------------------------------------------------------------------------
Using dub:
.. code-block:: json
"dependencies": {
"autointf": "*"
}
Quickstart
==============================================================================
See example app:
.. code-block:: d
import std.stdio;
import autointf;
class AutoJsonRpc(I) : I
{
private int id;
private ReturnType!Func executeMethod(alias Func, ARGS...)(ARGS args)
{
import std.traits;
import std.array : join;
import std.conv : to;
// retrieve some compile time informations
alias RT = ReturnType!Func;
alias PTT = ParameterTypeTuple!Func;
enum Name = __traits(identifier, Func);
string[] params;
foreach (i, PT; PTT)
params ~= to!string(args[i]);
return `{"jsonrpc": "2.0", "method": "` ~ Name ~ `", "params": [`
~ params.join(",") ~ `], "id": ` ~ (id++).to!string() ~ "}";
}
mixin(autoImplementMethods!(I, executeMethod)());
}
interface IAPI
{
string helloWorld(int number, string str);
@noAutoImplement()
final string foo() { return "foo"; }
}
void main()
{
auto api = new AutoJsonRpc!IAPI();
writeln(api.helloWorld(42, "foo"));
// > {"jsonrpc": "2.0", "method": "helloWorld", "params": [42,foo], "id": 0}
writeln(api.foo());
// > foo
}