2012-12-18 20:06:36 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-01-14 09:00:19 +00:00
|
|
|
#include <string>
|
|
|
|
#include <memory>
|
|
|
|
#include <unordered_map>
|
2015-09-29 19:19:54 +00:00
|
|
|
#include <common/singleton.h>
|
2012-12-18 20:06:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
class Context;
|
2017-01-14 09:00:19 +00:00
|
|
|
class IFunction;
|
|
|
|
using FunctionPtr = std::shared_ptr<IFunction>;
|
2012-12-18 20:06:36 +00:00
|
|
|
|
|
|
|
|
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.
|
2017-01-14 09:00:19 +00:00
|
|
|
std::unordered_map<std::string, Creator> functions;
|
2014-08-22 00:57:20 +00:00
|
|
|
|
2012-12-18 20:06:36 +00:00
|
|
|
public:
|
2014-08-22 00:57:20 +00:00
|
|
|
FunctionFactory();
|
|
|
|
|
2017-01-14 09:00:19 +00:00
|
|
|
FunctionPtr get(const std::string & name, const Context & context) const; /// Throws an exception if not found.
|
|
|
|
FunctionPtr tryGet(const std::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
|
|
|
};
|
|
|
|
|
|
|
|
}
|