MADARA  3.1.8
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/MADARA_export.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  bool invalid_callable = false;
113 
114  // Check to make sure its a callable object
115  if (0 == PyObject_HasAttrString (func.ptr (), "__call__"))
116  {
117  // If not, lets throw an exception to warn the user
118  PyErr_SetString (
119  PyExc_TypeError,
120  "Handler must be a callable object");
121 
122  boost::python::throw_error_already_set();
123  }
124  }
125 
126  bool is_python_callable (void) const
127  {
128  return type == PYTHON_CALLABLE && !python_function.is_none ();
129  }
130 
131  boost::python::object python_function;
132 #endif
133 
134  inline bool is_extern_unnamed (void) const
135  {
136  return type == EXTERN_UNNAMED && unnamed_filter;
137  }
138 
139  inline bool is_extern_named (void) const
140  {
141  return type == EXTERN_NAMED && named_filter;
142  }
143 
144  inline bool is_functor (void) const
145  {
146  return type == FUNCTOR;
147  }
148 
149  bool is_uninitialized (void) const
150  {
151  return type == UNINITIALIZED;
152  }
153 
155  void (*unnamed_filter) (
157  Variables &);
158 
160  void (*named_filter) (
161  const char *, KnowledgeMap &,
162  const transport::TransportContext &,
163  Variables &);
164 
167 
168  // type of function definition
169  int type;
170  };
171 
175  typedef std::list <AggregateFilter> AggregateFilters;
176  }
177 }
178 
179 
180 
181 
182 #endif // _MADARA_AGGREGATE_FILTERS_H_
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
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.
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.