InternalFunction.h
Go to the documentation of this file.
00001 /*
00002  * InternalFunction.h
00003  *
00004  *  Created on: Oct 27, 2013
00005  *      Author: blackpc
00006  */
00007 
00008 #ifndef INTERNALFUNCTION_H_
00009 #define INTERNALFUNCTION_H_
00010 
00011 #include <iostream>
00012 #include <map>
00013 #include <yaml-cpp/yaml.h>
00014 #include <boost/shared_ptr.hpp>
00015 #include <boost/lexical_cast.hpp>
00016 #include <boost/foreach.hpp>
00017 
00018 #include <scriptable_monitor/Parameters.h>
00019 
00020 using namespace std;
00021 
00022 #define DEFINE_INTERNAL_FUNCTION(FunctionClassName) \
00023 class FunctionClassName:public InternalFunction {\
00024 public:\
00025 FunctionClassName() { }\
00026         virtual ~FunctionClassName() { }\
00027         virtual string functionName(){ return #FunctionClassName; };\
00028 \
00029 protected:\
00030         virtual void process(Parameters& input, Parameters& output);\
00031 };\
00032 REGISTER_INTERNAL_FUNCTION(FunctionClassName)\
00033 void NAME::process(Parameters& input, Parameters& output)
00034 
00035 #define REGISTER_INTERNAL_FUNCTION(FunctionClassName) \
00036 static InternalFunctionRegistrar FunctionClassName##registrar(new FunctionClassName());
00037 
00038 
00042 class InternalFunction {
00043 public:
00044 
00045         InternalFunction() { }
00046         virtual ~InternalFunction() { }
00047 
00048         Parameters invoke(Parameters& input) {
00049                 Parameters output;
00050                 process(input, output);
00051                 return output;
00052         }
00053 
00054         virtual string functionName() = 0;
00055 
00056 protected:
00057         virtual void process(Parameters& input, Parameters& output) = 0;
00058 };
00059 
00060 
00064 class InternalFunctionsManager {
00065 public:
00066         static void registerFunction(InternalFunction* function) {
00067                 _functions[function->functionName()] = function;
00068         }
00069 
00070         static InternalFunction* resolve(string functionName) {
00071                 if (_functions.count(functionName) == 0)
00072                         return NULL;
00073 
00074                 return _functions[functionName];
00075         }
00076 
00077         static vector<string> getFunctionNames() {
00078 
00079                 pair<string, InternalFunction*> func;
00080                 vector<string> names;
00081 
00082                 BOOST_FOREACH(func, _functions) {
00083                         names.push_back(func.first);
00084                 }
00085 
00086                 return names;
00087         }
00088 private:
00089         static map<string, InternalFunction*> _functions;
00090 };
00091 
00092 
00093 class InternalFunctionRegistrar {
00094 public:
00095         InternalFunctionRegistrar(InternalFunction* function) {
00096                 InternalFunctionsManager::registerFunction(function);
00097         }
00098 };
00099 
00100 
00101 
00102 #endif /* INTERNALFUNCTION_H_ */


scriptable_monitor
Author(s):
autogenerated on Wed Aug 26 2015 16:21:30