StreamBase C++ API  7.6.2dev
PluginAggregate.hpp
1 //
2 // Copyright (c) 2004-2016 Cloud Software Group, Inc. All rights reserved.
3 //
4 
5 #ifndef STREAMBASE_PLUGIN_AGGREGATE_H
6 #define STREAMBASE_PLUGIN_AGGREGATE_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 PluginAggregateRep;
19 
20 /// A plugin aggregate function.
21 class SB_EXPORT PluginAggregate : public Plugin {
22  public:
23  /// Virtual destructor.
24  virtual ~PluginAggregate() {}
25 
26  /// Check to make sure the argument types are valid for this
27  /// aggregate function.
28  /// @param arg_types The types of the arguments to this function.
29  /// @return the type of the return value.
30  /// @throw TypecheckException if the argument types are not
31  /// appropriate.
32  virtual void typecheck(const Schema &arg_types) {}
33 
34  /// Initialize any window state. Clear the window values, if any.
35  /// Instances of PluginAggregate may be reused for multiple
36  /// windows.
37  virtual void initialize() = 0;
38 
39  /// Add the values passed as parameters to the window state.
40  /// initialize() will be called first.
41  virtual void increment(const Tuple &args) = 0;
42 
43  /// Calculate the value of the aggregate over the values in the window.
44  /// initialize() will be called first. No guarantee that increment()
45  /// will ever be called.
46  virtual void calculate(Tuple &retval) = 0;
47 
48  ///
49  /// Clear internal state. This method is called after this use of the aggregate
50  /// is finished.
51  ///
52  virtual void release() { }
53 
54  /// Return a binary string containing the checkpoint state of this
55  /// object. This string will be passed to the load() method if a
56  /// checkpoint is loaded.
57  virtual std::string save() { return std::string(); }
58 
59  /// Recover the state of this object from the argument, a binary
60  /// string that was returned by a previous invocation of save()
61  virtual void load(std::string data) {}
62 };
63 
64 typedef PluginRegistryInfo<PluginAggregate> PluginAggregateRegistryInfo;
65 
66 void SB_EXPORT plugin_aggregate_callback(const PluginAggregateRegistryInfo &info);
67 
68 #ifndef DOXYGEN_SKIP
69 template<>
70 struct PluginRegistryCallback<PluginAggregateRegistryInfo> {
71  void operator()(const PluginAggregateRegistryInfo &info)
72  {
73  plugin_aggregate_callback(info);
74  }
75 };
76 
77 typedef PluginRegistryCallback<PluginAggregateRegistryInfo> PluginAggregateRegistryCallback;
78 
79 typedef PluginRegistry<PluginAggregate,
80  PluginAggregateRegistryInfo,
81  PluginAggregateRegistryCallback> PluginAggregateRegistry;
82 
83 #ifdef WIN32
84 
85 #ifndef STREAMBASE_PLUGIN_VERSION_DEFINED
86 #define STREAMBASE_PLUGIN_VERSION_DEFINED
87 // this function is used to determine what streambase version the plugin was compiled against
88 extern "C" __declspec( dllexport ) inline const char*streambase_plugin_version() {return ((char *)&StreamBaseVersion::INFO_LINE);}
89 #endif
90 
91 // There is a limitation where most stl containers can not be
92 // exported across a dll boundary. To get plugs - which really need to be dlls - to work, the
93 // registration mechanism can not require anything with stl containers to be exported.
94 // The current mechanism exposes PluginRegistry (and maybe others) which uses map.
95 // So on windows, the registration mechanism is an exported class called PluginRegistryAggregate
96 // (and one for each type of registred object) that does not directly reference the user
97 // defined plugin class name.
98 class SB_EXPORT PluginRegistryAggregate {
99  public:
100  static bool Add(PluginRegistry<PluginAggregate, PluginRegistryInfo<PluginAggregate>, PluginRegistryCallback< PluginRegistryInfo<PluginAggregate> > >::Info* streambase_plugin_info) {
101  return (PluginRegistry<PluginAggregate>::get().add(*streambase_plugin_info), true);
102  }
103 };
104 
105 #define STREAMBASE_DEFINE_PLUGIN_AGGREGATE(ClassName, FuncName) \
106  const SimplePluginRegistryInfo<PluginAggregate, ClassName> ClassName::_streambase_plugin_info(FuncName); \
107  bool ClassName::_streambase_plugin_registered = \
108  PluginRegistryAggregate::Add((PluginRegistryInfo<PluginAggregate>*)&(ClassName::_streambase_plugin_info));
109 
110 #define STREAMBASE_DECLARE_PLUGIN_AGGREGATE(ClassName) \
111  public: \
112  static const SimplePluginRegistryInfo<PluginAggregate, ClassName> _streambase_plugin_info; \
113  static bool _streambase_plugin_registered;
114 
115 #else
116 #define STREAMBASE_DECLARE_PLUGIN_AGGREGATE(ClassName) \
117  STREAMBASE_DECLARE_PLUGIN_REG_CLASS(PluginAggregate, ClassName)
118 
119 #define STREAMBASE_DEFINE_PLUGIN_AGGREGATE(ClassName, FuncName) \
120  STREAMBASE_DEFINE_PLUGIN_REG_CLASS_WITH_KEY(PluginAggregate, ClassName, FuncName)
121 #endif
122 
123 #endif // !defined(DOXYGEN_SKIP)
124 
125 SB_NAMESPACE_END;
126 
127 #endif //PLUGIN_AGGREGATE_H