This is Unofficial EPICS BASE Doxygen Site
iocshelper.h
Go to the documentation of this file.
1 #ifndef IOCSHELPER_H
2 #define IOCSHELPER_H
3 
25 #include <string>
26 
27 #include <iocsh.h>
28 
29 namespace epics {
30 namespace detail {
31 
32 template<typename T>
33 struct getarg {};
34 template<> struct getarg<int> {
35  static int op(const iocshArgBuf& a) { return a.ival; }
36  enum { argtype = iocshArgInt };
37 };
38 template<> struct getarg<double> {
39  static double op(const iocshArgBuf& a) { return a.dval; }
40  enum { argtype = iocshArgDouble };
41 };
42 template<> struct getarg<char*> {
43  static char* op(const iocshArgBuf& a) { return a.sval; }
44  enum { argtype = iocshArgString };
45 };
46 template<> struct getarg<const char*> {
47  static const char* op(const iocshArgBuf& a) { return a.sval; }
48  enum { argtype = iocshArgString };
49 };
50 
51 
52 template<int N>
55  std::string name;
56  iocshArg *argarr[N+1];
57  iocshArg args[N+1];
58  std::string argnames[N+1];
59  iocshFuncInfo(const std::string& n) :name(n) {
60  def.name = name.c_str();
61  def.nargs = N;
62  def.arg = (iocshArg**)&argarr;
63  for(size_t i=0; i<N; i++)
64  argarr[i] = &args[i];
65  }
66  template<int n, typename T>
67  void set(const char *name) {
68  argnames[n] = name;
69  args[n].name = argnames[n].c_str();
71  }
72 };
73 
74 template<void (*fn)()>
75 static void call0(const iocshArgBuf *args)
76 {
77  fn();
78 }
79 
80 template<typename A, void (*fn)(A)>
81 static void call1(const iocshArgBuf *args)
82 {
83  fn(getarg<A>::op(args[0]));
84 }
85 
86 template<typename A, typename B, void (*fn)(A,B)>
87 static void call2(const iocshArgBuf *args)
88 {
89  fn(getarg<A>::op(args[0]),
90  getarg<B>::op(args[1]));
91 }
92 
93 template<typename A, typename B, typename C, void (*fn)(A,B,C)>
94 static void call3(const iocshArgBuf *args)
95 {
96  fn(getarg<A>::op(args[0]),
97  getarg<B>::op(args[1]),
98  getarg<C>::op(args[2]));
99 }
100 
101 } // namespace detail
102 
103 
104 template<void (*fn)()>
105 void iocshRegister(const char *name)
106 {
107  static detail::iocshFuncInfo<0> info(name);
108  iocshRegister(&info.def, &detail::call0<fn>);
109 }
110 
111 template<typename A, void (*fn)(A)>
112 void iocshRegister(const char *name, const char *arg1name)
113 {
114  static detail::iocshFuncInfo<1> info(name);
115  info.set<0,A>(arg1name);
116  iocshRegister(&info.def, &detail::call1<A, fn>);
117 }
118 
119 template<typename A, typename B, void (*fn)(A,B)>
120 void iocshRegister(const char *name,
121  const char *arg1name,
122  const char *arg2name)
123 {
124  static detail::iocshFuncInfo<2> info(name);
125  info.set<0,A>(arg1name);
126  info.set<1,B>(arg2name);
127  iocshRegister(&info.def, &detail::call2<A, B, fn>);
128 }
129 
130 template<typename A, typename B, typename C, void (*fn)(A,B,C)>
131 void iocshRegister(const char *name,
132  const char *arg1name,
133  const char *arg2name,
134  const char *arg3name)
135 {
136  static detail::iocshFuncInfo<3> info(name);
137  info.set<0,A>(arg1name);
138  info.set<1,B>(arg2name);
139  info.set<2,C>(arg3name);
140  iocshRegister(&info.def, &detail::call3<A, B, C, fn>);
141 }
142 
143 template<typename V, V* addr>
144 void iocshVariable(const char *name)
145 {
146  static iocshVarDef def[2];
147  def[0].name = name;
148  def[0].pval = (void*)addr;
150  def[1].name = NULL;
152 }
153 
154 } // namespace epics
155 
156 #endif // IOCSHELPER_H
void set(const char *name)
Definition: iocshelper.h:67
iocshArgType
Definition: iocsh.h:30
int i
Definition: scan.c:967
static const char * op(const iocshArgBuf &a)
Definition: iocshelper.h:47
TODO only here because of the Lockable.
Definition: ntaggregate.cpp:16
const char * name
Definition: iocsh.h:57
#define NULL
Definition: catime.c:38
char * sval
Definition: iocsh.h:42
const char * name
Definition: iocsh.h:51
void iocshRegister(const char *name)
Definition: iocshelper.h:105
iocshArgType type
Definition: iocsh.h:52
const iocshArg *const * arg
Definition: iocsh.h:64
double dval
Definition: iocsh.h:41
static double op(const iocshArgBuf &a)
Definition: iocshelper.h:39
const char * name
Definition: iocsh.h:62
int ival
Definition: iocsh.h:40
iocshFuncInfo(const std::string &n)
Definition: iocshelper.h:59
void epicsStdCall iocshRegisterVariable(const iocshVarDef *piocshVarDef)
Definition: iocsh.cpp:167
const ChannelProviderRegistry::factoryfn_t fn
void iocshVariable(const char *name)
Definition: iocshelper.h:144
int * def
Definition: flex.c:92
static char * op(const iocshArgBuf &a)
Definition: iocshelper.h:43
void * pval
Definition: iocsh.h:53
int nargs
Definition: iocsh.h:63
iocshArgType type
Definition: iocsh.h:58
static int op(const iocshArgBuf &a)
Definition: iocshelper.h:35
Definition: iocsh.h:56