2016-06-07 08:23:15 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <DB/Functions/IFunction.h>
|
|
|
|
|
#include <common/singleton.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
class Context;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Позволяет получить функцию по имени.
|
2016-06-08 13:08:20 +00:00
|
|
|
|
* Функция при создании также может использовать для инициализации (например, захватить shared_ptr)
|
2016-06-07 08:23:15 +00:00
|
|
|
|
* какие-нибудь справочники, находящиеся в Context-е.
|
|
|
|
|
*/
|
|
|
|
|
class FunctionFactory : public Singleton<FunctionFactory>
|
|
|
|
|
{
|
|
|
|
|
friend class StorageSystemFunctions;
|
|
|
|
|
|
|
|
|
|
private:
|
2016-06-08 13:08:20 +00:00
|
|
|
|
typedef FunctionPtr (*Creator)(const Context & context); /// Не std::function, так как меньше indirection и размер объекта.
|
2016-06-07 08:23:15 +00:00
|
|
|
|
std::unordered_map<String, Creator> functions;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
FunctionFactory();
|
|
|
|
|
|
|
|
|
|
FunctionPtr get(const String & name, const Context & context) const; /// Кидает исключение, если не нашлось.
|
|
|
|
|
FunctionPtr tryGet(const String & name, const Context & context) const; /// Возвращает nullptr, если не нашлось.
|
|
|
|
|
|
|
|
|
|
template <typename F> void registerFunction()
|
|
|
|
|
{
|
|
|
|
|
static_assert(std::is_same<decltype(&F::create), Creator>::value, "F::create has incorrect type");
|
|
|
|
|
functions[F::name] = &F::create;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|