MADARA  3.2.3
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 
58  : unnamed_filter (0), functor (0), type (UNINITIALIZED)
59  {
60  }
61 
65  AggregateFilter (void (*extern_func) (
67  Variables &))
68  : unnamed_filter (extern_func), functor (0), type (EXTERN_UNNAMED)
69  {
70  }
71 
76  : unnamed_filter (0), functor (filter), type (FUNCTOR)
77  {
78  }
79 
80 #ifdef _MADARA_JAVA_
81  jobject java_object;
82 
86  AggregateFilter (jobject& object)
87  : type (JAVA_CALLABLE)
88  {
89  //We have to create a globla ref to the object or we cant call it
90  JNIEnv* env = madara_jni_get_env();
91  java_object = (jobject) env->NewGlobalRef(object);
92  }
93 
94  bool is_java_callable (void) const
95  {
96  return type == JAVA_CALLABLE;
97  }
98 
99  //KnowledgeRecord call_java(KnowledgeMap & recordsMap, const transport::TransportContext & context, Variables & vars) const
100  //{
101  // return knowledge::KnowledgeRecord ();
102  //}
103 #endif
104 
105 #ifdef _MADARA_PYTHON_CALLBACKS_
106 
109  AggregateFilter (boost::python::object & func)
110  : python_function (func), type (PYTHON_CALLABLE)
111  {
112  // Check to make sure its a callable object
113  if (0 == PyObject_HasAttrString (func.ptr (), "__call__"))
114  {
115  // If not, lets throw an exception to warn the user
116  PyErr_SetString (
117  PyExc_TypeError,
118  "Handler must be a callable object");
119 
120  boost::python::throw_error_already_set();
121  }
122  }
123 
124  bool is_python_callable (void) const
125  {
126  return type == PYTHON_CALLABLE && !python_function.is_none ();
127  }
128 
129  boost::python::object python_function;
130 #endif
131 
132  inline bool is_extern_unnamed (void) const
133  {
134  return type == EXTERN_UNNAMED && unnamed_filter;
135  }
136 
137  inline bool is_extern_named (void) const
138  {
139  return type == EXTERN_NAMED && named_filter;
140  }
141 
142  inline bool is_functor (void) const
143  {
144  return type == FUNCTOR;
145  }
146 
147  bool is_uninitialized (void) const
148  {
149  return type == UNINITIALIZED;
150  }
151 
153  void (*unnamed_filter) (
155  Variables &);
156 
158  void (*named_filter) (
159  const char *, KnowledgeMap &,
160  const transport::TransportContext &,
161  Variables &);
162 
165 
166  // type of function definition
167  int type;
168  };
169 
173  typedef std::list <AggregateFilter> AggregateFilters;
174  }
175 }
176 
177 
178 
179 
180 #endif // _MADARA_AGGREGATE_FILTERS_H_
helper type for specifying template type parameters using a function argument instead of inside expli...
Definition: KnowledgeCast.h:72
AggregateFilter(filters::AggregateFilter *filter)
Constructor for functor.
Abstract base class for implementing aggregate record filters via a functor interface.
filters::AggregateFilter * functor
mapped functor for aggregate filtering
AggregateFilter(jobject &object)
Constructor for java.
Provides context about the transport.
This class stores a function definition.
::std::map< std::string, KnowledgeRecord > KnowledgeMap
std::list< AggregateFilter > AggregateFilters
Typedef for a list of aggregate filters.
AggregateFilter(void(*extern_func)(KnowledgeMap &, const transport::TransportContext &, Variables &))
Constructor for function pointer.
boost::python::object python_function
Provides functions and classes for the distributed knowledge base.
Types
Types of functions supported.
Copyright (c) 2015 Carnegie Mellon University.
AggregateFilter()
Default constructor.
Provides an interface for external functions into the MADARA KaRL variable settings.
AggregateFilter(boost::python::object &func)
Constructor for function pointer.