Jpp  18.0.0-rc.3
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JFunctionAdaptor.hh
Go to the documentation of this file.
1 #ifndef __JEEP__JFUNCTIONADAPTOR__
2 #define __JEEP__JFUNCTIONADAPTOR__
3 
4 #include <istream>
5 #include <ostream>
6 #include <string>
7 #include <dlfcn.h>
8 
9 #include "JLang/JException.hh"
10 #include "JLang/JSharedCounter.hh"
11 
12 
13 /**
14  * \author mdejong
15  */
16 
17 namespace JEEP {}
18 namespace JPP { using namespace JEEP; }
19 
20 namespace JEEP {
21 
25  using JLANG::JParseError;
26 
27  /**
28  * Auxiliary base class for function adaptor.
29  *
30  * Note that the dynamic load library internally maintains link counts for the file handles.
31  */
32  template<class __pF__>
35  {
36  /**
37  * Separation character between library file name and function name.
38  */
39  static const char SEPARATOR = ':';
40 
41 
42  /**
43  * Virtual destructor.
44  */
46  {
47  reset();
48  }
49 
50 
51  /**
52  * Check validity of function.
53  *
54  * \return true if valid function; else false
55  */
56  bool is_valid() const
57  {
58  return (function != NULL);
59  }
60 
61 
62  /**
63  * Reset function adaptor helper.
64  */
65  void reset()
66  {
67  if (detach()) {
68  close();
69  }
70 
71  function = NULL;
72  }
73 
74 
75  /**
76  * Set function.
77  *
78  * \param pf pointer to function
79  */
80  void set(__pF__ pf)
81  {
82  close();
83 
84  function = pf;
85  }
86 
87 
88  /**
89  * Get function with given name according to specific protocol.
90  *
91  * \param symbol function name
92  */
93  void get(const char* symbol)
94  {
95  function = NULL;
96 
97  if (handle != NULL) {
98 
99  dlerror(); // reset errors
100 
101  function = (__pF__) dlsym(handle, symbol);
102 
103  const char* error = dlerror();
104 
105  if (error != NULL) {
106  THROW(JFileReadException, error);
107  }
108 
109  } else {
110 
111  THROW(JFileReadException, "Invalid handle.");
112  }
113  }
114 
115 
116  /**
117  * Check if shared library file is open.
118  *
119  * \return true if open; else false
120  */
121  bool is_open() const
122  {
123  return (handle != NULL);
124  }
125 
126 
127  /**
128  * Open file.
129  *
130  * \param file_name file name
131  */
132  void open(const char* file_name)
133  {
134  reset();
135 
136  handle = dlopen(file_name, RTLD_LOCAL | RTLD_LAZY);
137 
138  if (!handle) {
139  THROW(JFileOpenException, dlerror());
140  }
141 
142  initialise();
143  }
144 
145 
146  /**
147  * Close file.
148  *
149  * Note that the file should remain open as long as the library function is used.
150  */
151  void close()
152  {
153  if (handle != NULL) {
154  dlclose(handle);
155  dlclose(handle);
156  }
157 
158  handle = NULL;
159  }
160 
161 
162  /**
163  * Load function from shared library.
164  *
165  * \param file_name file name
166  * \param symbol function name
167  */
168  void load(const std::string& file_name, const std::string& symbol)
169  {
170  open(file_name.c_str());
171 
172  get(symbol.c_str());
173  }
174 
175 
176  /**
177  * Load function from shared library.
178  *
179  * The input buffer should consist of <file name>SEPARATOR<symbol>.
180  *
181  * \param buffer input
182  */
183  void load(const std::string& buffer)
184  {
185  using namespace std;
186 
187  const string::size_type pos = buffer.rfind(SEPARATOR);
188 
189  if (pos != string::npos) {
190 
191  load(buffer.substr(0,pos).c_str(), buffer.substr(pos+1).c_str());
192 
193  } else {
194 
195  THROW(JParseError, "Missing separator " << SEPARATOR << " in " << buffer);
196  }
197  }
198 
199 
200  /**
201  * Read function adaptor helper from input stream.
202  *
203  * \param in input stream
204  * \param object function adaptor helper
205  * \return input stream
206  */
207  friend std::istream& operator>>(std::istream& in, JFunctionAdaptorHelper& object)
208  {
209  object.reset();
210 
211  std::string buffer;
212 
213  if (in >> buffer) {
214  object.load(buffer);
215  }
216 
217  return in;
218  }
219 
220 
221  /**
222  * Write function adaptor helper to output stream.
223  *
224  * \param out output stream
225  * \param object function adaptor helper
226  * \return output stream
227  */
228  friend std::ostream& operator<<(std::ostream& out, const JFunctionAdaptorHelper& object)
229  {
230  return out;
231  }
232 
233  protected:
234  /**
235  * Default constructor.
236  */
238  handle (NULL),
239  function(NULL)
240  {}
241 
242 
243  /**
244  * Constructor.
245  *
246  * \param pf pointer to function
247  */
249  handle (NULL),
250  function(pf)
251  {}
252 
253 
254  /**
255  * Constructor.
256  *
257  * \param file_name file name
258  * \param symbol function name
259  */
261  const std::string& symbol) :
262  handle (NULL),
263  function(NULL)
264  {
265  this->load(file_name, symbol);
266  }
267 
268 
269  /**
270  * Copy constructor.
271  *
272  * \param helper helper
273  */
275  handle (helper.handle),
276  function(helper.function)
277  {
278  if (is_open()) {
279  attach(helper);
280  }
281  }
282 
283  void* handle;
284  __pF__ function;
285  };
286 
287 
288  /**
289  * Function adaptor.
290  */
291  template<class JReturn_t = void, class JFirst_t = void, class JSecond_t = void>
293 
294 
295  /**
296  * Function adaptor for function with one argument.
297  */
298  template<class JReturn_t, class JFirst_t>
299  struct JFunctionAdaptor<JReturn_t, JFirst_t, void> :
300  public JFunctionAdaptorHelper<JReturn_t (*)(JFirst_t)>
301  {
302  /**
303  * Type definition of method.
304  */
305  typedef JReturn_t (*pF)(JFirst_t);
306 
307 
308  /**
309  * Default constructor.
310  */
313  {}
314 
315 
316  /**
317  * Constructor.
318  *
319  * \param pf pointer to function
320  */
322  JFunctionAdaptorHelper<pF>(pf)
323  {}
324 
325 
326  /**
327  * Constructor.
328  *
329  * \param file_name file name
330  * \param symbol function name
331  */
332  JFunctionAdaptor(const std::string& file_name,
333  const std::string& symbol) :
334  JFunctionAdaptorHelper<pF>(file_name, symbol)
335  {}
336 
337 
338  /**
339  * Function operation.
340  *
341  * \param first first argument
342  * \return return value
343  */
344  JReturn_t operator()(JFirst_t first) const
345  {
346  return this->function(first);
347  }
348  };
349 
350 
351  /**
352  * Function adaptor for function with two arguments.
353  */
354  template<class JReturn_t, class JFirst_t, class JSecond_t>
355  struct JFunctionAdaptor :
356  public JFunctionAdaptorHelper<JReturn_t (*)(JFirst_t, JSecond_t)>
357  {
358  /**
359  * Type definition of method.
360  */
361  typedef JReturn_t (*pF)(JFirst_t, JSecond_t);
362 
363 
364  /**
365  * Default constructor.
366  */
369  {}
370 
371 
372  /**
373  * Constructor.
374  *
375  * \param pf pointer to function
376  */
378  JFunctionAdaptorHelper<pF>(pf)
379  {}
380 
381 
382  /**
383  * Constructor.
384  *
385  * \param file_name file name
386  * \param symbol function name
387  */
388  JFunctionAdaptor(const std::string& file_name,
389  const std::string& symbol) :
390  JFunctionAdaptorHelper<pF>(file_name, symbol)
391  {}
392 
393 
394  /**
395  * Function operation.
396  *
397  * \param first first argument
398  * \param second second argument
399  * \return return value
400  */
401  JReturn_t operator()(JFirst_t first, JSecond_t second) const
402  {
403  return this->function(first, second);
404  }
405  };
406 }
407 
408 #endif
Exception for opening of file.
Definition: JException.hh:342
void open(const char *file_name)
Open file.
Exception for reading of file.
Definition: JException.hh:378
JFunctionAdaptorHelper(__pF__ pf)
Constructor.
void load(const std::string &buffer)
Load function from shared library.
Exceptions.
Shared counter.
Auxiliary base class for function adaptor.
void set(__pF__ pf)
Set function.
static const char SEPARATOR
Separation character between library file name and function name.
JFunctionAdaptor(const std::string &file_name, const std::string &symbol)
Constructor.
JFunctionAdaptor(const std::string &file_name, const std::string &symbol)
Constructor.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:696
void attach(const JSharedCounter &object)
Attach this counter to given shared counter object.
JFunctionAdaptor()
Default constructor.
JReturn_t operator()(JFirst_t first) const
Function operation.
void load(const std::string &file_name, const std::string &symbol)
Load function from shared library.
then echo The file $DIR KM3NeT_00000001_00000000 root already please rename or remove it first
JFunctionAdaptorHelper(const JFunctionAdaptorHelper &helper)
Copy constructor.
JFunctionAdaptorHelper()
Default constructor.
JFunctionAdaptorHelper(const std::string &file_name, const std::string &symbol)
Constructor.
then awk string
void initialise()
Initialise counter.
friend std::ostream & operator<<(std::ostream &out, const JFunctionAdaptorHelper &object)
Write function adaptor helper to output stream.
Exception for parsing value.
Definition: JException.hh:180
virtual ~JFunctionAdaptorHelper()
Virtual destructor.
void reset()
Reset function adaptor helper.
bool detach()
Detach.
JFunctionAdaptor(pF pf)
Constructor.
Function adaptor.
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY JAcoustics sh $DETECTOR_ID source JAcousticsToolkit sh CHECK_EXIT_CODE typeset A EMITTERS get_tripods $WORKDIR tripod txt EMITTERS get_transmitters $WORKDIR transmitter txt EMITTERS for EMITTER in
Definition: JCanberra.sh:46
bool is_valid() const
Check validity of function.
bool is_open() const
Check if shared library file is open.
friend std::istream & operator>>(std::istream &in, JFunctionAdaptorHelper &object)
Read function adaptor helper from input stream.
JReturn_t operator()(JFirst_t first, JSecond_t second) const
Function operation.