From 79cb3221c2babe6f560f2e3e463e899631a32b33 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Wed, 23 Nov 2016 14:58:30 +0200 Subject: Implement few builtin functions that can operate on any value type() null() empty () identity() --- build2/buildfile | 1 + build2/function | 12 +++++++++++- build2/function.cxx | 2 ++ build2/functions-builtin.cxx | 24 ++++++++++++++++++++++++ 4 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 build2/functions-builtin.cxx (limited to 'build2') diff --git a/build2/buildfile b/build2/buildfile index fa6faef..98494d8 100644 --- a/build2/buildfile +++ b/build2/buildfile @@ -15,6 +15,7 @@ exe{b}: \ {hxx ixx cxx}{ file } \ {hxx txx cxx}{ filesystem } \ {hxx cxx}{ function } \ + { cxx}{ functions-builtin } \ { cxx}{ functions-path } \ { cxx}{ functions-process-path } \ {hxx cxx}{ lexer } \ diff --git a/build2/function b/build2/function index 68edcde..192c4a3 100644 --- a/build2/function +++ b/build2/function @@ -37,7 +37,8 @@ namespace build2 // T - statically-typed (value_traits must be defined) // names - untyped // value - any type - // T* - NULL-able argument (here T can be names, value). + // T* - NULL-able argument (here T can be names) + // value* - NULL-able any type (never NULL itself, use value::null) // optional - optional argument (here T can be T*, names, value) // // Optional arguments must be last. In case of a failure the function is @@ -281,6 +282,15 @@ namespace build2 } }; + template <> + struct function_arg: function_arg + { + static const bool null = true; + + static value* + cast (value* v) {return v;} // NULL indicator in value::null. + }; + template struct function_arg>: function_arg { diff --git a/build2/function.cxx b/build2/function.cxx index 46dc403..e76f0c2 100644 --- a/build2/function.cxx +++ b/build2/function.cxx @@ -295,6 +295,7 @@ namespace build2 // function_map functions; + void builtin_functions (); // functions-builtin.cxx void path_functions (); // functions-path.cxx void process_path_functions (); // functions-process-path.cxx @@ -302,6 +303,7 @@ namespace build2 { functions_init () { + builtin_functions (); path_functions (); process_path_functions (); } diff --git a/build2/functions-builtin.cxx b/build2/functions-builtin.cxx new file mode 100644 index 0000000..448bb53 --- /dev/null +++ b/build2/functions-builtin.cxx @@ -0,0 +1,24 @@ +// file : build2/functions-builtin.cxx -*- C++ -*- +// copyright : Copyright (c) 2014-2016 Code Synthesis Ltd +// license : MIT; see accompanying LICENSE file + +#include +#include + +using namespace std; + +namespace build2 +{ + void + builtin_functions () + { + function_family f ("builtin"); + + f["type"] = [](value* v) {return v->type != nullptr ? v->type->name : "";}; + + f["null"] = [](value* v) {return v->null;}; + f["empty"] = [](value v) {return v.empty ();}; + + f["identity"] = [](value* v) {return move (*v);}; + } +} -- cgit v1.1