Program Listing for File Exceptions.h

Return to documentation for file (/tmp/ws/src/sick_safetyscanners_base/include/sick_safetyscanners_base/Exceptions.h)

// this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-

// -- BEGIN LICENSE BLOCK ----------------------------------------------

// -- END LICENSE BLOCK ------------------------------------------------

//----------------------------------------------------------------------
//----------------------------------------------------------------------

#ifndef SICK_SAFETYSCANNERS_BASE_EXCEPTIONS_H
#define SICK_SAFETYSCANNERS_BASE_EXCEPTIONS_H

#include <boost/date_time/posix_time/posix_time_types.hpp>
#include <chrono>
#include <exception>
#include <sstream>
#include <stdexcept>

namespace sick {

class runtime_error : public std::runtime_error
{
public:
  explicit runtime_error()
    : runtime_error(""){};

  explicit runtime_error(const std::string& what_arg)
    : std::runtime_error(what_arg){};

  explicit runtime_error(const char* what_arg)
    : std::runtime_error(what_arg){};

  virtual ~runtime_error() = default;
};

class configuration_error : public runtime_error
{
public:
  explicit configuration_error()
    : runtime_error(""){};

  explicit configuration_error(const std::string& what_arg)
    : runtime_error(what_arg){};

  explicit configuration_error(const char* what_arg)
    : runtime_error(what_arg){};
};

// class unsupported_device : public runtime_error
// {
// public:
//     explicit unsupported_device() : runtime_error(""){};
//     explicit unsupported_device(const std::string &msg) : runtime_error(msg){};
// };

class timeout_error : public runtime_error
{
public:
  explicit timeout_error() = delete;

  explicit timeout_error(const std::string& msg, timeval timeout)
    : runtime_error(msg)
  {
    std::stringstream ss;
    ss << msg << " (timeout was set to " << timeout.tv_sec + timeout.tv_usec * 1e-6 << " sec)";
    msg_ = ss.str();
  }

  explicit timeout_error(const std::string& msg, boost::posix_time::time_duration timeout)
  {
    std::stringstream ss;
    ss << msg << " [timeout: " << timeout.total_milliseconds() * 1e-3 << "seconds]";
    msg_ = ss.str();
  }

  virtual const char* what() const noexcept override { return msg_.c_str(); }

private:
  std::string msg_;
};
} // namespace sick
#endif // ifndef SICK_SAFETYSCANNERS_BASE_EXCEPTIONS_H