Skip to content
Snippets Groups Projects
Commit 0f3a09f3 authored by Christoph Lehmann's avatar Christoph Lehmann
Browse files

[NL] moved max fct args; renamed argument info -> ~names

parent bb81230e
No related branches found
No related tags found
No related merge requests found
......@@ -64,9 +64,9 @@ static const CallerFunction callers[] = {
generateCaller<21>(), generateCaller<22>(), generateCaller<23>(),
generateCaller<24>(), generateCaller<25>(), generateCaller<26>(),
generateCaller<27>(), generateCaller<28>(), generateCaller<29>(),
generateCaller<30>(), generateCaller<31>()};
generateCaller<30>(), generateCaller<31>(), generateCaller<32>()};
static_assert(sizeof(callers) / sizeof(CallerFunction) ==
NumLib::MAX_FUNCTION_ARGS,
NumLib::NamedFunction::MAX_FUNCTION_ARGS + 1,
"You did not instantiate the right number of callers.");
/*! Deletes the given \c function.
......@@ -113,9 +113,9 @@ static const DeleterFunction deleters[] = {
generateDeleter<21>(), generateDeleter<22>(), generateDeleter<23>(),
generateDeleter<24>(), generateDeleter<25>(), generateDeleter<26>(),
generateDeleter<27>(), generateDeleter<28>(), generateDeleter<29>(),
generateDeleter<30>(), generateDeleter<31>()};
generateDeleter<30>(), generateDeleter<31>(), generateDeleter<32>()};
static_assert(sizeof(deleters) / sizeof(DeleterFunction) ==
NumLib::MAX_FUNCTION_ARGS,
NumLib::NamedFunction::MAX_FUNCTION_ARGS + 1,
"You did not instantiate the right number of deleters.");
/*! Copies the given \c function.
......@@ -162,16 +162,16 @@ static const CopierFunction copiers[] = {
generateCopier<21>(), generateCopier<22>(), generateCopier<23>(),
generateCopier<24>(), generateCopier<25>(), generateCopier<26>(),
generateCopier<27>(), generateCopier<28>(), generateCopier<29>(),
generateCopier<30>(), generateCopier<31>()};
generateCopier<30>(), generateCopier<31>(), generateCopier<32>()};
static_assert(sizeof(copiers) / sizeof(CopierFunction) ==
NumLib::MAX_FUNCTION_ARGS,
NumLib::NamedFunction::MAX_FUNCTION_ARGS + 1,
"You did not instantiate the right number of deleters.");
namespace NumLib
{
NamedFunction::NamedFunction(NamedFunction&& other)
: _name(std::move(other._name)),
_argument_info(std::move(other._argument_info)),
_argument_names(std::move(other._argument_names)),
_function(other._function)
{
other._function = nullptr;
......@@ -179,20 +179,20 @@ NamedFunction::NamedFunction(NamedFunction&& other)
NamedFunction::NamedFunction(NamedFunction const& other)
: _name(other._name),
_argument_info(other._argument_info),
_function(copiers[_argument_info.size()](other._function))
_argument_names(other._argument_names),
_function(copiers[_argument_names.size()](other._function))
{
}
NamedFunction::~NamedFunction()
{
deleters[_argument_info.size()](_function);
deleters[_argument_names.size()](_function);
}
double NamedFunction::call(const std::vector<double>& arguments) const
{
assert(arguments.size() == _argument_info.size());
return callers[_argument_info.size()](_function, arguments);
assert(arguments.size() == _argument_names.size());
return callers[_argument_names.size()](_function, arguments);
}
} // namespace NumLib
......@@ -10,12 +10,12 @@
#ifndef NUMLIB_NAMED_FUNCTION
#define NUMLIB_NAMED_FUNCTION
#include <string>
#include <vector>
#include <cassert>
#include <functional>
#include <memory>
#include <cassert>
#include <string>
#include <type_traits>
#include <vector>
namespace detail
{
......@@ -44,31 +44,23 @@ const bool AllTypesSameAs<TRef>::value;
} // namespace detail
namespace NumLib
{
//! Maximum number of function arguments supported by NamedFunction.
const int MAX_FUNCTION_ARGS = 32;
//! Stores a function object along with a name for it and information about its
//! arguments.
class NamedFunction final
{
public:
using ArgumentInfo = std::string;
/*! Constructs a new named function.
*
* \param name the function's name
* \param arguments names of arguments of the function
* \param argument_names names of arguments of the function
* \param function the actual function object
*/
template <typename ReturnType, typename... Arguments>
NamedFunction(
std::string const& name,
std::vector<ArgumentInfo>&& arguments,
std::function<ReturnType(Arguments...)>&& function);
NamedFunction(std::string const& name,
std::vector<std::string>&& argument_names,
std::function<ReturnType(Arguments...)>&& function);
NamedFunction(NamedFunction&& other);
NamedFunction(NamedFunction const&);
......@@ -77,22 +69,24 @@ public:
//! Returns the function's name.
std::string const& getName() const { return _name; }
//! Returns information about the function's arguments.
std::vector<ArgumentInfo> const& getArgumentInfo() const
//! Returns the names of the function's arguments.
std::vector<std::string> const& getArgumentNames() const
{
return _argument_info;
return _argument_names;
}
//! Call the function with the supplied arguments.
double call(std::vector<double> const& arguments) const;
//! Maximum number of function arguments supported by NamedFunction.
static const int MAX_FUNCTION_ARGS = 32;
private:
//! The function's name.
std::string _name;
//! Information about the function's arguments.
std::vector<ArgumentInfo> _argument_info;
std::vector<std::string> _argument_names;
//! The function handle.
void* _function;
......@@ -100,10 +94,10 @@ private:
template <typename ReturnType, typename... Arguments>
NamedFunction::NamedFunction(std::string const& name,
std::vector<ArgumentInfo>&& argument_info,
std::vector<std::string>&& argument_names,
std::function<ReturnType(Arguments...)>&& function)
: _name(name),
_argument_info(std::move(argument_info)),
_argument_names(std::move(argument_names)),
_function(
new std::function<ReturnType(Arguments...)>(std::move(function)))
{
......@@ -114,10 +108,9 @@ NamedFunction::NamedFunction(std::string const& name,
static_assert(sizeof...(Arguments) <= MAX_FUNCTION_ARGS,
"The function you passed has too many arguments.");
assert(sizeof...(Arguments) == _argument_info.size());
assert(sizeof...(Arguments) == _argument_names.size());
}
} // namespace NumLib
} // namespace NumLib
#endif // NUMLIB_NAMED_FUNCTION
......@@ -120,7 +120,8 @@ void NamedFunctionCaller::addNamedFunction(NamedFunction&& fct)
_map_name_idx, fct.getName(), _named_functions.size(),
"The name of the function is not unique.");
_map_sink_source.emplace_back(fct.getArgumentInfo().size(), _uninitialized);
_map_sink_source.emplace_back(fct.getArgumentNames().size(),
_uninitialized);
_named_functions.push_back(std::move(fct));
}
......@@ -157,7 +158,7 @@ void NamedFunctionCaller::applyPlugs()
auto const sink_fct_idx = sink_it->second;
auto const& sink_args =
_named_functions[sink_it->second].getArgumentInfo();
_named_functions[sink_it->second].getArgumentNames();
auto const sink_arg_it =
std::find(sink_args.begin(), sink_args.end(), sink_arg);
if (sink_arg_it == sink_args.end())
......@@ -216,7 +217,7 @@ double NamedFunctionCaller::call(
_named_functions[function_idx].getName().c_str());
auto const& sis_sos = _map_sink_source[function_idx];
assert(sis_sos.size() ==
_named_functions[function_idx].getArgumentInfo().size());
_named_functions[function_idx].getArgumentNames().size());
std::vector<double> fct_args(sis_sos.size());
for (std::size_t sink=0; sink<sis_sos.size(); ++sink)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment