MADARA  3.1.8
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/MADARA_export.h"
13 
14 #ifdef _MADARA_JAVA_
15 #include <jni.h>
16 #include "madara_jni.h"
17 #endif
18 
19 #ifdef _MADARA_PYTHON_CALLBACKS_
20  #include <boost/python.hpp>
21 #endif
22 
31 namespace madara
32 {
33  namespace knowledge
34  {
35  class Variables;
36  class ThreadSafeContext;
37 
39 
44  class MADARA_Export Function
45  {
46  public:
47 
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), extern_unnamed (0),
67 
68 #ifndef _MADARA_NO_KARL_
69  function_contents (*logger::global_logger.get ()),
70 #endif // _MADARA_NO_KARL_
71 
72  functor (0), type (UNINITIALIZED)
73  {
74  }
75 
80  : extern_named (0), extern_unnamed (func),
81 
82 #ifndef _MADARA_NO_KARL_
83  function_contents (*logger::global_logger.get ()),
84 #endif // _MADARA_NO_KARL_
85 
86  functor (0), type (EXTERN_UNNAMED)
87  {
88  }
89 
93  Function (KnowledgeRecord (*func) (const char *,
95  : extern_named (func), extern_unnamed (0),
96 
97 #ifndef _MADARA_NO_KARL_
98  function_contents (*logger::global_logger.get ()),
99 #endif // _MADARA_NO_KARL_
100 
101  functor (0), type (EXTERN_NAMED)
102  {
103  }
104 
105 #ifndef _MADARA_NO_KARL_
106 
110  : extern_named (0), extern_unnamed (0), function_contents (func),
111  functor (0), type (KARL_EXPRESSION)
112  {
113  }
114 
115 #endif // _MADARA_NO_KARL_
116 
121  : extern_named (0), extern_unnamed (0),
122 
123 #ifndef _MADARA_NO_KARL_
124  function_contents (*logger::global_logger.get ()),
125 #endif // _MADARA_NO_KARL_
126 
127  functor (filter), type (FUNCTOR)
128  {
129  }
130 
131  inline bool is_extern_unnamed (void) const
132  {
133  return type == EXTERN_UNNAMED && extern_unnamed;
134  }
135 
136  inline bool is_extern_named (void) const
137  {
138  return type == EXTERN_NAMED && extern_named;
139  }
140 
141  inline bool is_karl_expression (void) const
142  {
143  return type == KARL_EXPRESSION;
144  }
145 
146  inline bool is_functor (void) const
147  {
148  return type == FUNCTOR;
149  }
150 
151  inline bool is_uninitialized (void) const
152  {
153  return type == UNINITIALIZED;
154  }
155 
156  // internal function pointer
157  knowledge::KnowledgeRecord (*extern_named) (
158  const char *, FunctionArguments &, Variables &);
159 
160  // internal function pointer
161  knowledge::KnowledgeRecord (*extern_unnamed) (FunctionArguments &, Variables &);
162 
163 #ifndef _MADARA_NO_KARL_
164  // expression tree
166 #endif // _MADARA_NO_KARL_
167 
169 
170  // type of function definition
171  int type;
172 
173 
174 #ifdef _MADARA_JAVA_
175  jobject java_object;
176 
180  Function (jobject& object)
181  : function_contents (*logger::global_logger.get ()),
182  type (JAVA_CALLABLE)
183  {
184  //We have to create a globla ref to the object or we cant call it
185  JNIEnv* env = madara_jni_get_env();
186  java_object = (jobject) env->NewGlobalRef(object);
187  }
188 
189  inline bool is_java_callable (void) const
190  {
191  return type == JAVA_CALLABLE;
192  }
193 
194 #endif
195 
196 #ifdef _MADARA_PYTHON_CALLBACKS_
197 
200  Function (boost::python::object & func)
201  : function_contents (*logger::global_logger.get ()),
202  python_function (func), type (PYTHON_CALLABLE)
203  {
204  bool invalid_callable = false;
205 
206  // Check to make sure its a callable object
207  if (0 == PyObject_HasAttrString (func.ptr (), "__call__"))
208  {
209  // If not, lets throw an exception to warn the user
210  PyErr_SetString (
211  PyExc_TypeError,
212  "Handler must be a callable object");
213 
214  boost::python::throw_error_already_set();
215  }
216  }
217 
218  bool is_python_callable (void) const
219  {
220  return type == PYTHON_CALLABLE && !python_function.is_none ();
221  }
222 
223  boost::python::object python_function;
224 #endif
225 
226 
227  };
228 
229  }
230 }
231 
232 #endif // _MADARA_EXTERNAL_FUNCTIONS_H_
This class encapsulates an entry in a KnowledgeBase.
Function(KnowledgeRecord(*func)(const char *, FunctionArguments &, Variables &))
Constructor for function pointer.
Definition: Functions.h:93
This class stores a function definition.
Definition: Functions.h:44
bool is_functor(void) const
Definition: Functions.h:146
Encapsulates a MADARA KaRL expression into an evaluatable tree.
madara::knowledge::KnowledgeRecord KnowledgeRecord
Function(filters::RecordFilter *filter)
Constructor for KaRL expression.
Definition: Functions.h:120
bool is_karl_expression(void) const
Definition: Functions.h:141
Function(const madara::expression::ExpressionTree &func)
Constructor for KaRL expression.
Definition: Functions.h:109
MADARA_Export utility::Refcounter< logger::Logger > global_logger
Function()
Default constructor.
Definition: Functions.h:65
Abstract base class for implementing individual record filters via a functor interface.
Definition: RecordFilter.h:33
Provides knowledge logging services to files and terminals.
Definition: GlobalLogger.h:11
std::vector< KnowledgeRecord > FunctionArguments
bool is_uninitialized(void) const
Definition: Functions.h:151
bool is_extern_named(void) const
Definition: Functions.h:136
Types
Types of functions supported.
Definition: Functions.h:51
madara::knowledge::KnowledgeRecord VALUE_TYPE
Provides functions and classes for the distributed knowledge base.
filters::RecordFilter * functor
Definition: Functions.h:168
Copyright (c) 2015 Carnegie Mellon University.
Provides an interface for external functions into the MADARA KaRL variable settings.
bool is_extern_unnamed(void) const
Definition: Functions.h:131
Function(KnowledgeRecord(*func)(FunctionArguments &, Variables &))
Constructor for function pointer.
Definition: Functions.h:79
madara::expression::ExpressionTree function_contents
Definition: Functions.h:165