ClickHouse/dbms/include/DB/Functions/FunctionFactory.h

40 lines
1.0 KiB
C++
Raw Normal View History

#pragma once
#include <DB/Functions/IFunction.h>
2015-09-29 19:19:54 +00:00
#include <common/singleton.h>
namespace DB
{
class Context;
2016-07-09 23:37:29 +00:00
/** Creates function by name.
* Function could use for initialization (take ownership of shared_ptr, for example)
* some dictionaries from Context.
*/
class FunctionFactory : public Singleton<FunctionFactory>
{
2015-04-24 15:49:30 +00:00
friend class StorageSystemFunctions;
private:
2016-07-09 23:37:29 +00:00
typedef FunctionPtr (*Creator)(const Context & context); /// Not std::function, for lower object size and less indirection.
std::unordered_map<String, Creator> functions;
public:
FunctionFactory();
2016-07-09 23:37:29 +00:00
FunctionPtr get(const String & name, const Context & context) const; /// Throws an exception if not found.
FunctionPtr tryGet(const String & name, const Context & context) const; /// Returns nullptr if not found.
/// No locking, you must register all functions before usage of get, tryGet.
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;
}
};
}