MADARA  3.4.1
Functions.h
Go to the documentation of this file.
1 
2 #ifndef _MADARA_EXTERNAL_FUNCTIONS_H_
3 #define _MADARA_EXTERNAL_FUNCTIONS_H_
4 
5 #include <string>
6 #include "madara/MadaraExport.h"
14 
15 #ifdef _MADARA_JAVA_
16 #include <jni.h>
17 #include "madara_jni.h"
18 #endif
19 
20 #ifdef _MADARA_PYTHON_CALLBACKS_
21 #include <boost/python.hpp>
22 #endif
23 
32 namespace madara
33 {
34 namespace knowledge
35 {
36 class Variables;
37 class ThreadSafeContext;
38 
40 
45 class MADARA_EXPORT Function
46 {
47 public:
51  enum Types
52  {
53  UNINITIALIZED = 0,
54  EXTERN_UNNAMED = 1,
55  EXTERN_NAMED = 2,
56  KARL_EXPRESSION = 3,
57  PYTHON_CALLABLE = 4,
58  JAVA_CALLABLE = 5,
59  FUNCTOR = 6
60  };
61 
66  : extern_named(0),
67  extern_unnamed(0),
68 
69 #ifndef _MADARA_NO_KARL_
70  function_contents(*logger::global_logger.get()),
71 #endif // _MADARA_NO_KARL_
72 
73  functor(0),
74  type(UNINITIALIZED)
75  {
76  }
77 
82  : extern_named(0),
83  extern_unnamed(func),
84 
85 #ifndef _MADARA_NO_KARL_
86  function_contents(*logger::global_logger.get()),
87 #endif // _MADARA_NO_KARL_
88 
89  functor(0),
90  type(EXTERN_UNNAMED)
91  {
92  }
93 
98  : extern_named(func),
99  extern_unnamed(0),
100 
101 #ifndef _MADARA_NO_KARL_
102  function_contents(*logger::global_logger.get()),
103 #endif // _MADARA_NO_KARL_
104 
105  functor(0),
106  type(EXTERN_NAMED)
107  {
108  }
109 
110 #ifndef _MADARA_NO_KARL_
115  : extern_named(0),
116  extern_unnamed(0),
117  function_contents(func),
118  functor(0),
119  type(KARL_EXPRESSION)
120  {
121  }
122 
127  : extern_named(0),
128  extern_unnamed(0),
129  function_contents(exp.expression),
130  functor(0),
131  type(KARL_EXPRESSION)
132  {
133  }
134 
135 #endif // _MADARA_NO_KARL_
136 
141  : extern_named(0),
142  extern_unnamed(0),
143 
144 #ifndef _MADARA_NO_KARL_
145  function_contents(*logger::global_logger.get()),
146 #endif // _MADARA_NO_KARL_
147 
148  functor(filter),
149  type(FUNCTOR)
150  {
151  }
152 
153  inline bool is_extern_unnamed(void) const
154  {
155  return type == EXTERN_UNNAMED && extern_unnamed;
156  }
157 
158  inline bool is_extern_named(void) const
159  {
160  return type == EXTERN_NAMED && extern_named;
161  }
162 
163  inline bool is_karl_expression(void) const
164  {
165  return type == KARL_EXPRESSION;
166  }
167 
168  inline bool is_functor(void) const
169  {
170  return type == FUNCTOR;
171  }
172 
173  inline bool is_uninitialized(void) const
174  {
175  return type == UNINITIALIZED;
176  }
177 
178  // internal function pointer
179  knowledge::KnowledgeRecord (*extern_named)(
180  const char*, FunctionArguments&, Variables&);
181 
182  // internal function pointer
184 
185 #ifndef _MADARA_NO_KARL_
186  // expression tree
188 #endif // _MADARA_NO_KARL_
189 
191 
192  // type of function definition
193  int type;
194 
195 #ifdef _MADARA_JAVA_
196  jobject java_object;
197 
201  Function(jobject& object)
202  : function_contents(*logger::global_logger.get()), type(JAVA_CALLABLE)
203  {
204  // We have to create a globla ref to the object or we cant call it
205  JNIEnv* env = madara_jni_get_env();
206  java_object = (jobject)env->NewGlobalRef(object);
207  }
208 
209  inline bool is_java_callable(void) const
210  {
211  return type == JAVA_CALLABLE;
212  }
213 
214 #endif
215 
216 #ifdef _MADARA_PYTHON_CALLBACKS_
220  Function(boost::python::object& func)
221  : function_contents(*logger::global_logger.get()),
222  type(PYTHON_CALLABLE),
223  python_function(func)
224  {
225  // Check to make sure its a callable object
226  if (0 == PyObject_HasAttrString(func.ptr(), "__call__"))
227  {
228  // If not, lets throw an exception to warn the user
229  PyErr_SetString(PyExc_TypeError, "Handler must be a callable object");
230 
231  boost::python::throw_error_already_set();
232  }
233  }
234 
235  bool is_python_callable(void) const
236  {
237  return type == PYTHON_CALLABLE && !python_function.is_none();
238  }
239 
240  boost::python::object python_function;
241 #endif
242 };
243 }
244 }
245 
246 #endif // _MADARA_EXTERNAL_FUNCTIONS_H_
madara::knowledge::KnowledgeRecord KnowledgeRecord
Encapsulates a MADARA KaRL expression into an evaluatable tree.
Abstract base class for implementing individual record filters via a functor interface.
Definition: RecordFilter.h:34
Compiled, optimized KaRL logic.
This class stores a function definition.
Definition: Functions.h:46
boost::python::object python_function
Definition: Functions.h:240
bool is_extern_named(void) const
Definition: Functions.h:158
Function(KnowledgeRecord(*func)(const char *, FunctionArguments &, Variables &))
Constructor for function pointer.
Definition: Functions.h:97
Function(KnowledgeRecord(*func)(FunctionArguments &, Variables &))
Constructor for function pointer.
Definition: Functions.h:81
Types
Types of functions supported.
Definition: Functions.h:52
bool is_java_callable(void) const
Definition: Functions.h:209
Function(jobject &object)
Constructor for java.
Definition: Functions.h:201
bool is_uninitialized(void) const
Definition: Functions.h:173
bool is_extern_unnamed(void) const
Definition: Functions.h:153
Function()
Default constructor.
Definition: Functions.h:65
Function(const madara::expression::ExpressionTree &func)
Constructor for KaRL expression.
Definition: Functions.h:114
Function(filters::RecordFilter *filter)
Constructor for KaRL expression.
Definition: Functions.h:140
filters::RecordFilter * functor
Definition: Functions.h:190
bool is_python_callable(void) const
Definition: Functions.h:235
madara::expression::ExpressionTree function_contents
Definition: Functions.h:187
bool is_karl_expression(void) const
Definition: Functions.h:163
Function(const madara::knowledge::CompiledExpression &exp)
Constructor for KaRL expression.
Definition: Functions.h:126
Function(boost::python::object &func)
Constructor for function pointer.
Definition: Functions.h:220
bool is_functor(void) const
Definition: Functions.h:168
This class encapsulates an entry in a KnowledgeBase.
Provides an interface for external functions into the MADARA KaRL variable settings.
Definition: Variables.h:53
Provides functions and classes for the distributed knowledge base.
T get(const KnowledgeRecord &kr)
Get the value of a KnowlegeRecord.
Definition: GetRecord.h:121
madara::knowledge::KnowledgeRecord VALUE_TYPE
Definition: Functions.h:37
std::vector< KnowledgeRecord > FunctionArguments
Provides knowledge logging services to files and terminals.
Definition: GlobalLogger.h:12
MADARA_EXPORT utility::Refcounter< logger::Logger > global_logger
Copyright(c) 2020 Galois.