MADARA  3.4.1
AggregateFilter.h
Go to the documentation of this file.
1 
2 #ifndef _MADARA_AGGREGATE_FILTERS_H_
3 #define _MADARA_AGGREGATE_FILTERS_H_
4 
5 #include <list>
6 #include <string>
7 #include "madara/MadaraExport.h"
12 
13 #ifdef _MADARA_JAVA_
14 #include <jni.h>
15 #include "madara_jni.h"
16 #endif
17 
18 #ifdef _MADARA_PYTHON_CALLBACKS_
19 #include <boost/python.hpp>
20 #endif
21 
30 namespace madara
31 {
32 namespace knowledge
33 {
38 class MADARA_EXPORT AggregateFilter
39 {
40 public:
44  enum Types
45  {
46  UNINITIALIZED = 0,
47  EXTERN_UNNAMED = 1,
48  EXTERN_NAMED = 2,
49  PYTHON_CALLABLE = 3,
50  JAVA_CALLABLE = 4,
51  FUNCTOR = 5
52  };
53 
57  AggregateFilter() : unnamed_filter(0), functor(0), type(UNINITIALIZED) {}
58 
62  AggregateFilter(void (*extern_func)(
64  : unnamed_filter(extern_func), functor(0), type(EXTERN_UNNAMED)
65  {
66  }
67 
72  : unnamed_filter(0), functor(filter), type(FUNCTOR)
73  {
74  }
75 
76 #ifdef _MADARA_JAVA_
77  jobject java_object;
78 
82  AggregateFilter(jobject& object) : type(JAVA_CALLABLE)
83  {
84  // We have to create a globla ref to the object or we cant call it
85  JNIEnv* env = madara_jni_get_env();
86  java_object = (jobject)env->NewGlobalRef(object);
87  }
88 
89  bool is_java_callable(void) const
90  {
91  return type == JAVA_CALLABLE;
92  }
93 
94  // KnowledgeRecord call_java(KnowledgeMap & recordsMap, const
95  // transport::TransportContext & context, Variables & vars) const
96  //{
97  // return knowledge::KnowledgeRecord ();
98  //}
99 #endif
100 
101 #ifdef _MADARA_PYTHON_CALLBACKS_
105  AggregateFilter(boost::python::object& func)
106  : python_function(func), type(PYTHON_CALLABLE)
107  {
108  // Check to make sure its a callable object
109  if (0 == PyObject_HasAttrString(func.ptr(), "__call__"))
110  {
111  // If not, lets throw an exception to warn the user
112  PyErr_SetString(PyExc_TypeError, "Handler must be a callable object");
113 
114  boost::python::throw_error_already_set();
115  }
116  }
117 
118  bool is_python_callable(void) const
119  {
120  return type == PYTHON_CALLABLE && !python_function.is_none();
121  }
122 
123  boost::python::object python_function;
124 #endif
125 
126  inline bool is_extern_unnamed(void) const
127  {
128  return type == EXTERN_UNNAMED && unnamed_filter;
129  }
130 
131  inline bool is_extern_named(void) const
132  {
133  return type == EXTERN_NAMED && named_filter;
134  }
135 
136  inline bool is_functor(void) const
137  {
138  return type == FUNCTOR;
139  }
140 
141  bool is_uninitialized(void) const
142  {
143  return type == UNINITIALIZED;
144  }
145 
147  void (*unnamed_filter)(
149 
151  void (*named_filter)(const char*, KnowledgeMap&,
153 
156 
157  // type of function definition
158  int type;
159 };
160 
164 typedef std::list<AggregateFilter> AggregateFilters;
165 }
166 }
167 
168 #endif // _MADARA_AGGREGATE_FILTERS_H_
Abstract base class for implementing aggregate record filters via a functor interface.
This class stores a function definition.
AggregateFilter(filters::AggregateFilter *filter)
Constructor for functor.
AggregateFilter(boost::python::object &func)
Constructor for function pointer.
AggregateFilter(void(*extern_func)(KnowledgeMap &, const transport::TransportContext &, Variables &))
Constructor for function pointer.
AggregateFilter()
Default constructor.
boost::python::object python_function
Types
Types of functions supported.
AggregateFilter(jobject &object)
Constructor for java.
filters::AggregateFilter * functor
mapped functor for aggregate filtering
Provides an interface for external functions into the MADARA KaRL variable settings.
Definition: Variables.h:53
Provides context about the transport.
Provides functions and classes for the distributed knowledge base.
std::list< AggregateFilter > AggregateFilters
Typedef for a list of aggregate filters.
::std::map< std::string, KnowledgeRecord > KnowledgeMap
Copyright(c) 2020 Galois.