Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
hyperthunk committed Jan 16, 2012
0 parents commit 166f158
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 0 deletions.
13 changes: 13 additions & 0 deletions examples/simple/rebar.config
@@ -0,0 +1,13 @@

{deps, [
{parse_trans, ".*",
{git, "https://github.com/esl/parse_trans.git"}},
{annotations, ".*",
{git, "https://github.com/hyperthunk/annotations.git"}},
{delegate, ".*", {git, "../../.."}}
]}.
{plugins, [rebar_annotations_plugin]}.

{annotations, [
{registered, [delegate]}
]}.
9 changes: 9 additions & 0 deletions examples/simple/src/simple.app.src
@@ -0,0 +1,9 @@
{application, simple,
[
{description, "Simple Example of delegate in use"},
{vsn, "1"},
{applications, [
kernel,
stdlib
]}
]}.
34 changes: 34 additions & 0 deletions examples/simple/src/simple_log.erl
@@ -0,0 +1,34 @@
%% -----------------------------------------------------------------------------
%% Copyright (c) 2002-2012 Tim Watson (watson.timothy@gmail.com)
%%
%% Permission is hereby granted, free of charge, to any person obtaining a copy
%% of this software and associated documentation files (the "Software"), to deal
%% in the Software without restriction, including without limitation the rights
%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
%% copies of the Software, and to permit persons to whom the Software is
%% furnished to do so, subject to the following conditions:
%%
%% The above copyright notice and this permission notice shall be included in
%% all copies or substantial portions of the Software.
%%
%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
%% THE SOFTWARE.
%% -----------------------------------------------------------------------------
-module(simple_log).
-export([log/3]).
-include_lib("annotations/include/annotations.hrl").

-delegate([{prefix, function},
{delegate, [error, warn, info, debug]}]).
log(Level, Message, Args) ->
case get({?MODULE, loglevel}) of
Level ->
io:format(Message, Args);
_ ->
ok
end.
14 changes: 14 additions & 0 deletions rebar.config
@@ -0,0 +1,14 @@

{lib_dirs, ["deps/annotations"]}.
{deps, [
{parse_trans, ".*",
{git, "https://github.com/esl/parse_trans.git"}},
{annotations, ".*",
{git, "https://github.com/hyperthunk/annotations.git"}}
]}.

{plugins, [rebar_annotations_plugin]}.

{annotations, [
{registered, [delegate]}
]}.
9 changes: 9 additions & 0 deletions src/delegate.app.src
@@ -0,0 +1,9 @@
{application, delegate,
[
{description, ""},
{vsn, "0.0.1"},
{applications, [
kernel,
stdlib
]}
]}.
52 changes: 52 additions & 0 deletions src/delegate.erl
@@ -0,0 +1,52 @@
%% -----------------------------------------------------------------------------
%% Copyright (c) 2002-2012 Tim Watson (watson.timothy@gmail.com)
%%
%% Permission is hereby granted, free of charge, to any person obtaining a copy
%% of this software and associated documentation files (the "Software"), to deal
%% in the Software without restriction, including without limitation the rights
%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
%% copies of the Software, and to permit persons to whom the Software is
%% furnished to do so, subject to the following conditions:
%%
%% The above copyright notice and this permission notice shall be included in
%% all copies or substantial portions of the Software.
%%
%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
%% THE SOFTWARE.
%% -----------------------------------------------------------------------------
-module(delegate).
-include_lib("annotations/include/types.hrl").
-export([codegen/3, codegen_advised/4]).

codegen(A=#annotation{name=N, data=Data}, _Mod, AST) ->
case lists:keyfind(N, 1, Data) of
Delegates when is_list(Delegates) ->
%% NB: this *can* return either {gen_function, Name}, in which case
%% we generate a function that calls ?MODULE:codegen_advised/4 - or -
%% you return {add_function, Name, codegen:gen_function/2/3} in which case
%% you need -include_lib("parse_trans/include/codegen.hrl"), and this
%% will be added and exported as-is

%% NB: you need a *little* understanding of erl_syntax to use these
%% kinds of callbacks
{FN, _} = erl_syntax:analyze_function(AST),
[ {D, A#annotation{data=[{function, FN}|Data]}} || D <- Delegates ];
_ ->
%% TODO: clearer error handling API
{error, "no delegates defined"}
end.

codegen_advised(#annotation{data=AnnotationData}, M, F, Inputs) ->
Argv = case lists:keyfind(prefix, 1, AnnotationData) of
true ->
[F|Inputs];
false ->
Inputs
end,
Target = lists:keyfind(target, 1, AnnotationData),
erlang:apply(M, Target, [Argv]).

0 comments on commit 166f158

Please sign in to comment.