StreamBase C++ API  7.5.4dev
PluginFunction.hpp
1 //
2 // Copyright (c) 2004-2015 Cloud Software Group, Inc. All rights reserved.
3 //
4 
5 #ifndef STREAMBASE_PLUGIN_FUNCTION_H
6 #define STREAMBASE_PLUGIN_FUNCTION_H
7 
8 #include "StreamBase.hpp"
9 #include "StreamBaseVersion.hpp"
10 
11 #include "Plugin.hpp"
12 #include "DataType.hpp"
13 #include "PluginException.hpp"
14 #include "PluginRegistry.hpp"
15 
16 SB_NAMESPACE_BEGIN;
17 
18 class PluginFunctionRep;
19 
20 /// A plugin function.
21 class SB_EXPORT PluginFunction : public Plugin {
22  private:
23  PluginFunctionRep *_rep;
24 
25  public:
26  virtual ~PluginFunction() {}
27 
28  /// Check to make sure the argument types are valid for this
29  /// function.
30  /// @param arg_types The types of the arguments to this function.
31  /// @throw TypecheckException if the argument types are not
32  /// appropriate.
33  virtual void typecheck(const Schema &arg_types) {}
34 
35  /// Evaluate this function over the arguments and place the return
36  /// value in retval.
37  /// @param retval Value where the output of this function should
38  /// be placed. Its type will match the type returned from the
39  /// typecheck method.
40  /// @param args Values corresponding to the arguments. Their types
41  /// will match the types passed to typecheck.
42  /// @throw PluginEvalException if anything goes wrong.
43  virtual void eval(Tuple &retval, Tuple &args) = 0;
44 };
45 
46 #ifndef DOXYGEN_SKIP
47 typedef PluginRegistryInfo<PluginFunction> PluginFunctionRegistryInfo;
48 
49 void SB_EXPORT plugin_function_callback(const PluginFunctionRegistryInfo &info);
50 
51 template<>
52 struct PluginRegistryCallback<PluginFunctionRegistryInfo> {
53  void operator()(const PluginFunctionRegistryInfo &info)
54  {
55  plugin_function_callback(info);
56  }
57 };
58 
59 typedef PluginRegistryCallback<PluginFunctionRegistryInfo> PluginFunctionRegistryCallback;
60 
61 typedef PluginRegistry<PluginFunction,
62  PluginFunctionRegistryInfo,
63  PluginFunctionRegistryCallback> PluginFunctionRegistry;
64 
65 #ifdef WIN32
66 // See PluginAggregate.hpp for a description of why this is here...
67 
68 #ifndef STREAMBASE_PLUGIN_VERSION_DEFINED
69 #define STREAMBASE_PLUGIN_VERSION_DEFINED
70 // this function is used to determine what streambase version the plugin was compiled against
71 extern "C" __declspec( dllexport ) inline const char*streambase_plugin_version() {return ((char *)&StreamBaseVersion::INFO_LINE);}
72 #endif
73 
74 class SB_EXPORT PluginRegistryFunction {
75  public:
76  static bool Add(PluginRegistry<PluginFunction, PluginRegistryInfo<PluginFunction>, PluginRegistryCallback< PluginRegistryInfo<PluginFunction> > >::Info* streambase_plugin_info) {
77  return (PluginRegistry<PluginFunction>::get().add(*streambase_plugin_info), true);
78  }
79 };
80 
81 #define STREAMBASE_DEFINE_PLUGIN_FUNCTION(ClassName, FuncName) \
82  const SimplePluginRegistryInfo<PluginFunction, ClassName> ClassName::_streambase_plugin_info(FuncName); \
83  bool ClassName::_streambase_plugin_registered= \
84  PluginRegistryFunction::Add((PluginRegistryInfo<PluginFunction>*)&(ClassName::_streambase_plugin_info));
85 
86 #define STREAMBASE_DECLARE_PLUGIN_FUNCTION(ClassName) \
87  public: \
88  static const SimplePluginRegistryInfo<PluginFunction, ClassName> _streambase_plugin_info; \
89  static bool _streambase_plugin_registered;
90 
91 #else
92 #define STREAMBASE_DECLARE_PLUGIN_FUNCTION(ClassName) \
93  STREAMBASE_DECLARE_PLUGIN_REG_CLASS(PluginFunction, ClassName)
94 
95 #define STREAMBASE_DEFINE_PLUGIN_FUNCTION(ClassName, FuncName) \
96  STREAMBASE_DEFINE_PLUGIN_REG_CLASS_WITH_KEY(PluginFunction, ClassName, FuncName)
97 #endif
98 
99 #endif // !defined(DOXYGEN_SKIP)
100 
101 SB_NAMESPACE_END;
102 #endif //PLUGIN_FUNCTION_H
103