YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
Dispatcher.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#ifndef YARP_OS_IMPL_DISPATCHER_H
8#define YARP_OS_IMPL_DISPATCHER_H
9
11
12#include <cstdio>
13#include <map>
14#include <string>
15#include <vector>
16
18
19namespace yarp::os::impl {
20
24template <class T, class RET>
26{
27private:
28 class Entry
29 {
30 public:
31 std::string name;
32 RET (T::*fn)(int argc, char* argv[]);
33
34 Entry(const char* name, RET (T::*fn)(int argc, char* argv[])) :
35 name(name),
36 fn(fn)
37 {
38 }
39
40 Entry() :
41 fn(nullptr)
42 {
43 }
44 };
45
46 std::map<std::string, Entry> action;
47 std::vector<std::string> names;
48
49public:
50 void add(const char* name, RET (T::*fn)(int argc, char* argv[]))
51 {
52 Entry e(name, fn);
53 action[std::string(name)] = e;
54 // maintain a record of order of keys
55 names.push_back(std::string(name));
56 }
57
58 RET dispatch(T* owner, const char* name, int argc, char* argv[])
59 {
60 std::string sname(name);
61 typename std::map<std::string, Entry>::const_iterator it = action.find(sname);
62 if (it != action.end()) {
63 return (owner->*(it->second.fn))(argc, argv);
64 } else {
65 yCError(DISPATCHER, "Could not find command \"%s\"", name);
66 }
67 return RET();
68 }
69
70 std::vector<std::string> getNames()
71 {
72 return names;
73 }
74};
75
76} // namespace yarp::os::impl
77
78
79#endif // YARP_OS_IMPL_DISPATCHER_H
const yarp::os::LogComponent & DISPATCHER()
Definition Dispatcher.cpp:9
const yarp::os::LogComponent & DISPATCHER()
Definition Dispatcher.cpp:9
A mini-server for performing network communication in the background.
Dispatch to named methods based on string input.
Definition Dispatcher.h:26
std::vector< std::string > getNames()
Definition Dispatcher.h:70
void add(const char *name, RET(T::*fn)(int argc, char *argv[]))
Definition Dispatcher.h:50
RET dispatch(T *owner, const char *name, int argc, char *argv[])
Definition Dispatcher.h:58
#define yCError(component,...)
#define YARP_DECLARE_LOG_COMPONENT(name)
The components from which ports and connections are built.