Robowflex  v0.1
Making MoveIt Easy
util.h
Go to the documentation of this file.
1 /* Author: Zachary Kingston */
2 
3 #ifndef ROBOWFLEX_UTIL_
4 #define ROBOWFLEX_UTIL_
5 
6 #include <string>
7 #include <vector>
8 #include <exception>
9 
10 namespace robowflex
11 {
12  /** \brief Exception that contains a message and an error code.
13  */
14  class Exception : public std::exception
15  {
16  public:
17  /** \brief Constructor.
18  * \param[in] value Error code.
19  * \param[in] message Error message.
20  */
21  Exception(int value, const std::string &message) : value_(value), message_(message)
22  {
23  }
24 
25  /** \brief Get error code.
26  */
27  int getValue() const
28  {
29  return value_;
30  }
31 
32  /** \brief Get error message.
33  */
34  const std::string &getMessage() const
35  {
36  return message_;
37  }
38 
39  virtual const char *what() const throw()
40  {
41  return message_.c_str();
42  }
43 
44  protected:
45  const int value_; ///< Error code.
46  const std::string message_; ///< Error message.
47  };
48 
49  /** \brief RAII-pattern for starting up ROS.
50  */
51  class ROS
52  {
53  public:
54  /** \brief Constructor. Start-up ROS.
55  * If Boost version is greater than 1.64, `rosmaster` is started if it is not already running. A
56  * signal handler or SIGINT and SIGSEGV is installed to gracefully exit.
57  * \param[in] argc Argument count forwarded to ros::init
58  * \param[in] argv Arguments forwarded to ros::init
59  * \param[in] name Name of ROS node.
60  * \param[in] threads Threads to use for ROS spinning. If 0 no spinner is created.
61  */
62  ROS(int argc, char **argv, const std::string &name = "robowflex", unsigned int threads = 1);
63 
64  /** \brief Destructor. Shutdown ROS.
65  */
66  ~ROS();
67 
68  /** \brief Get command-line arguments without ROS parameters.
69  * \return Vector of command line arguments as strings.
70  */
72 
73  /** \brief Waits for the process to be killed via some means (normally Ctrl-C)
74  */
75  void wait() const;
76 
77  private:
78  int argc_; ///< Argument count.
79  char **argv_; ///< Arguments.
80  };
81 
82  /** \brief Trigger a SEGSEGV.
83  */
84  void explode();
85 } // namespace robowflex
86 
87 #endif
T c_str(T... args)
Exception that contains a message and an error code.
Definition: util.h:15
const std::string message_
Error message.
Definition: util.h:46
Exception(int value, const std::string &message)
Constructor.
Definition: util.h:21
int getValue() const
Get error code.
Definition: util.h:27
virtual const char * what() const
Definition: util.h:39
const int value_
Error code.
Definition: util.h:45
const std::string & getMessage() const
Get error message.
Definition: util.h:34
RAII-pattern for starting up ROS.
Definition: util.h:52
char ** argv_
Arguments.
Definition: util.h:79
ROS(int argc, char **argv, const std::string &name="robowflex", unsigned int threads=1)
Constructor. Start-up ROS. If Boost version is greater than 1.64, rosmaster is started if it is not a...
Definition: util.cpp:68
std::vector< std::string > getArgs() const
Get command-line arguments without ROS parameters.
Definition: util.cpp:88
void wait() const
Waits for the process to be killed via some means (normally Ctrl-C)
Definition: util.cpp:96
~ROS()
Destructor. Shutdown ROS.
Definition: util.cpp:83
int argc_
Argument count.
Definition: util.h:78
Main namespace. Contains all library classes and functions.
Definition: scene.cpp:25
void explode()
Trigger a SEGSEGV.
Definition: util.cpp:101