2012-12-18 20:06:36 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <DB/Functions/IFunction.h>
|
2015-09-29 19:19:54 +00:00
|
|
|
#include <common/singleton.h>
|
2012-12-18 20:06:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
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.
|
2012-12-18 20:06:36 +00:00
|
|
|
*/
|
2014-12-17 11:53:17 +00:00
|
|
|
class FunctionFactory : public Singleton<FunctionFactory>
|
2012-12-18 20:06:36 +00:00
|
|
|
{
|
2015-04-24 15:49:30 +00:00
|
|
|
friend class StorageSystemFunctions;
|
|
|
|
|
2014-08-22 00:57:20 +00:00
|
|
|
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.
|
2014-08-22 00:57:20 +00:00
|
|
|
std::unordered_map<String, Creator> functions;
|
|
|
|
|
2012-12-18 20:06:36 +00:00
|
|
|
public:
|
2014-08-22 00:57:20 +00:00
|
|
|
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.
|
2014-08-22 00:57:20 +00:00
|
|
|
|
2016-07-09 23:40:15 +00:00
|
|
|
/// No locking, you must register all functions before usage of get, tryGet.
|
2014-11-12 17:23:26 +00:00
|
|
|
template <typename F> void registerFunction()
|
2014-08-22 00:57:20 +00:00
|
|
|
{
|
2014-11-12 17:23:26 +00:00
|
|
|
static_assert(std::is_same<decltype(&F::create), Creator>::value, "F::create has incorrect type");
|
|
|
|
functions[F::name] = &F::create;
|
2014-08-22 00:57:20 +00:00
|
|
|
}
|
2012-12-18 20:06:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|