Program Listing for File sleep_pos.hpp

Return to documentation for file (/tmp/ws/src/ecl_core/ecl_time/include/ecl/time/sleep_pos.hpp)

/*****************************************************************************
** Ifdefs
*****************************************************************************/

#ifndef ECL_TIME_SLEEP_POS_HPP_
#define ECL_TIME_SLEEP_POS_HPP_

/*****************************************************************************
** Platform Check
*****************************************************************************/

#include <ecl/config.hpp>
#if defined(ECL_IS_POSIX)

/*****************************************************************************
** Includes
*****************************************************************************/

#include <sstream>
#include <time.h>
#include <errno.h>
#include <ecl/config/macros.hpp>
#include <ecl/exceptions/macros.hpp>
#include <ecl/exceptions/standard_exception.hpp>
#include "duration.hpp"
#include "macros.hpp"

/*****************************************************************************
** Namespaces
*****************************************************************************/

namespace ecl {

/*****************************************************************************
** Interface [Sleep]
*****************************************************************************/
class ecl_time_PUBLIC Sleep {
public:
    Sleep(const Duration &duration);
    Sleep(const unsigned long &seconds = 0);
    virtual ~Sleep() {}

    void operator()();
    Duration duration() { return Duration(required.tv_sec,required.tv_nsec); }

    void operator()(const unsigned long &seconds);
    void operator()(const Duration &duration);

private:
    timespec required, remaining;
};

class ecl_time_PUBLIC MilliSleep {
public:
    MilliSleep(const unsigned long &milliseconds = 0);

    virtual ~MilliSleep() {}

    void operator()();
    Duration duration() { return Duration(required.tv_sec,required.tv_nsec); }

    void operator()(const unsigned long &milliseconds);
private:
    timespec required, remaining;
};

class ecl_time_PUBLIC MicroSleep {
public:
    MicroSleep(const unsigned long &microseconds = 0);

    virtual ~MicroSleep() {}


    void operator()();
    Duration duration() { return Duration(required.tv_sec,required.tv_nsec); }
    void operator()(const unsigned long &micro_seconds);
private:
    timespec required, remaining;
};

class ecl_time_PUBLIC NanoSleep {
public:
    NanoSleep(const unsigned long &nanoseconds = 0);

    virtual ~NanoSleep() {}


    void operator()();
    Duration duration() { return Duration(required.tv_sec,required.tv_nsec); }
    void operator()(const unsigned long &nanoseconds);
private:
    timespec required, remaining;
};


} // namespace ecl

/*****************************************************************************
** Interface [Exceptions][Sleeper Classes]
*****************************************************************************/

#if defined(ECL_HAS_EXCEPTIONS)
namespace ecl {
namespace time {


/*****************************************************************************
** Interface [Sleep Exceptions]
*****************************************************************************/
inline ecl::StandardException throwSleepException(const char* loc ) {
    int error_result = errno;
    switch (error_result) {
        case ( EINTR  ) : return StandardException(loc, ecl::InterruptedError, "A posix signal interrupted the sleep.");
        case ( EINVAL ) : return StandardException(loc, ecl::InvalidInputError, "Specified value was negative or exceeded resolution range.\n\n            Sleep: [N/A]\n            MilliSleep: [0-1000]\n            MicroSleep: [0-1x10^6]\n            NanoSleep: [0-1x10^9]\n");
        case ( EFAULT ) : return StandardException(loc, ecl::MemoryError, "Internal posix issue copying information from user space.");
        default         :
        {
            std::ostringstream ostream;
            ostream << "Unknown posix error " << error_result << ": " << strerror(error_result) << ".";
            return StandardException(loc, UnknownError, ostream.str());
        }
    }

}


} // namespace time
} // namespace ecl
#endif

#endif /* ECL_IS_POSIX */
#endif /* ECL_TIME_SLEEP_POS_HPP_ */