dizzyd avatar dizzyd committed 83e073e

Adding basic NIF template

Comments (0)

Files changed (3)

priv/templates/basicnif.c

+
+#include "erl_nif.h"
+
+static ErlNifResourceType* {{module}}_RESOURCE;
+
+typedef struct
+{
+} {{module}}_handle;
+
+// Prototypes
+ERL_NIF_TERM {{module}}_new(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]);
+ERL_NIF_TERM {{module}}_myfunction(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]);
+
+static ErlNifFunc nif_funcs[] =
+{
+    {"new", 0, {{module}}_new},
+    {"myfunction", 1, {{module}}_myfunction}
+};
+
+ERL_NIF_TERM {{module}}_new(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
+{
+    {{module}}_handle* handle = enif_alloc_resource(env,
+                                                    {{module}}_RESOURCE,
+                                                    sizeof({{module}}_handle));
+    ERL_NIF_TERM result = enif_make_resource(env, handle);
+    enif_release_resource(env, handle);
+    return enif_make_tuple2(env, enif_make_atom(env, "ok"), result);
+}
+
+
+ERL_NIF_TERM {{module}}_myfunction(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
+{
+    return enif_make_atom(env, "ok");
+}
+
+static void {{module}}_resource_cleanup(ErlNifEnv* env, void* arg)
+{
+    // Delete any dynamically allocated memory stored in {{module}}_handle
+    // {{module}}_handle* handle = ({{module}}_handle*)arg;
+}
+
+static int on_load(ErlNifEnv* env, void** priv_data, ERL_NIF_TERM load_info)
+{
+    {{module}}_RESOURCE = enif_open_resource_type(env, "{{module}}_resource",
+                                                  &{{module}}_resource_cleanup,
+                                                  ERL_NIF_RT_CREATE | ERL_NIF_RT_TAKEOVER,
+                                                  0);
+    return 0;
+}
+
+ERL_NIF_INIT({{module}}, nif_funcs, &on_load, NULL, NULL, NULL);

priv/templates/basicnif.erl

+-module({{module}}).
+
+-export([new/0,
+         myfunction/1]).
+
+-on_load(init/0).
+
+-ifdef(TEST).
+-include_lib("eunit/include/eunit.hrl").
+-endif.
+
+init() ->
+    case code:priv_dir({{module}}) of
+        {error, bad_name} ->
+            SoName = filename:join("../priv", {{module}});
+        Dir ->
+            SoName = filename:join(Dir, {{module}})
+    end,
+    erlang:load_nif(SoName, 0).
+
+new() ->
+    "NIF library not loaded".
+
+myfunction(Ref) ->
+    "NIF library not loaded".
+
+%% ===================================================================
+%% EUnit tests
+%% ===================================================================
+-ifdef(TEST).
+
+basic_test() ->
+    {ok, Ref} = new(),
+    ok = myfunction(Ref).
+
+-endif.

priv/templates/basicnif.template

+{variables, [{module, "mymodule"}]}.
+{template, "basicnif.erl", "src/{{module}}.erl"}.
+{template, "basicnif.c", "c_src/{{module}}.c"}.
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.