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>
|
2017-07-10 04:34:14 +00:00
|
|
|
#include <ext/singleton.h>
|
2017-06-10 09:04:31 +00:00
|
|
|
#include <Common/Exception.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
|
|
|
|
2017-06-10 09:04:31 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2017-07-10 04:34:14 +00:00
|
|
|
class FunctionFactory : public ext::singleton<FunctionFactory>
|
2012-12-18 20:06:36 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
friend class StorageSystemFunctions;
|
2015-04-24 15:49:30 +00:00
|
|
|
|
2014-08-22 00:57:20 +00:00
|
|
|
private:
|
2017-06-10 09:04:31 +00:00
|
|
|
using Creator = FunctionPtr(*)(const Context & context); /// Not std::function, for lower object size and less indirection.
|
2017-04-01 07:20:54 +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:
|
2017-04-01 07:20:54 +00:00
|
|
|
FunctionFactory();
|
2014-08-22 00:57:20 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
FunctionPtr get(const std::string & name, const Context & context) const; /// Throws an exception if not found.
|
2017-06-13 02:06:53 +00:00
|
|
|
FunctionPtr tryGet(const std::string & name, const Context & context) const; /// Returns nullptr if not found.
|
2014-08-22 00:57:20 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// No locking, you must register all functions before usage of get, tryGet.
|
2017-06-10 09:04:31 +00:00
|
|
|
template <typename Function> void registerFunction()
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-06-10 09:04:31 +00:00
|
|
|
static_assert(std::is_same<decltype(&Function::create), Creator>::value, "Function::create has incorrect type");
|
|
|
|
|
2017-06-12 00:26:22 +00:00
|
|
|
if (!functions.emplace(std::string(Function::name), &Function::create).second)
|
2017-06-13 02:06:53 +00:00
|
|
|
throw Exception("FunctionFactory: the function name '" + std::string(Function::name) + "' is not unique",
|
2017-06-10 09:04:31 +00:00
|
|
|
ErrorCodes::LOGICAL_ERROR);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2012-12-18 20:06:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|