ClickHouse/dbms/src/Functions/FunctionFactory.h
Amos Bird a1310ffbe6 Split libdbms.so using object library
Now the linking time of incremental builds are around 1-2 seconds
2019-09-11 01:56:01 +08:00

77 lines
2.2 KiB
C++

#pragma once
#include <Functions/IFunction.h>
#include <Common/IFactoryWithAliases.h>
#include <functional>
#include <memory>
#include <string>
#include <unordered_map>
namespace DB
{
class Context;
/** Creates function by name.
* Function could use for initialization (take ownership of shared_ptr, for example)
* some dictionaries from Context.
*/
class FunctionFactory : private boost::noncopyable, public IFactoryWithAliases<std::function<FunctionBuilderPtr(const Context &)>>
{
public:
static FunctionFactory & instance();
template <typename Function>
void registerFunction(CaseSensitiveness case_sensitiveness = CaseSensitive)
{
registerFunction<Function>(Function::name, case_sensitiveness);
}
template <typename Function>
void registerFunction(const std::string & name, CaseSensitiveness case_sensitiveness = CaseSensitive)
{
if constexpr (std::is_base_of<IFunction, Function>::value)
registerFunction(name, &createDefaultFunction<Function>, case_sensitiveness);
else
registerFunction(name, &Function::create, case_sensitiveness);
}
/// Throws an exception if not found.
FunctionBuilderPtr get(const std::string & name, const Context & context) const;
/// Returns nullptr if not found.
FunctionBuilderPtr tryGet(const std::string & name, const Context & context) const;
private:
using Functions = std::unordered_map<std::string, Creator>;
Functions functions;
Functions case_insensitive_functions;
template <typename Function>
static FunctionBuilderPtr createDefaultFunction(const Context & context)
{
return std::make_shared<DefaultFunctionBuilder>(Function::create(context));
}
const Functions & getCreatorMap() const override { return functions; }
const Functions & getCaseInsensitiveCreatorMap() const override { return case_insensitive_functions; }
String getFactoryName() const override { return "FunctionFactory"; }
/// Register a function by its name.
/// No locking, you must register all functions before usage of get.
void registerFunction(
const std::string & name,
Creator creator,
CaseSensitiveness case_sensitiveness = CaseSensitive);
};
}