duration.h 7.51 KB
Newer Older
1
/*
2
 * Copyright (c) 2014-2015, Siemens AG. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef EMBB_BASE_C_DURATION_H_
#define EMBB_BASE_C_DURATION_H_

#include <embb/base/c/errors.h>

#ifdef __cplusplus
extern "C" {
#endif

/**
 * \defgroup C_BASE_DURATIONTIME Duration and Time
 *
 * Relative time durations and absolute time points
 *
 * \ingroup C_BASE
 * \{
 *
 * \name Duration
 * \{
 */

/**
 * Opaque type representing a relative time duration.
 */
#ifdef DOXYGEN
typedef opaque_type embb_duration_t;
#else
typedef struct embb_duration_t {
  /**
   * Seconds part of duration
   */
  unsigned long long seconds;
  /**
   * Nanoseconds part of duration, smaller than a second
   */
  unsigned long nanoseconds;
} embb_duration_t;
#endif /* else defined(DOXYGEN) */

/**
 * Macro for initializing a duration with zero length at definition.
 */
#ifdef DOXYGEN
#define EMBB_DURATION_INIT
#else
#define EMBB_DURATION_INIT {0, 0}
#endif /* defined(DOXYGEN) */

/**
 * Returns duration with maximum ticks representable by implementation.
 *
 * \return Pointer to duration with maximum value
 * \notthreadsafe
 * \see embb_duration_min()
 */
const embb_duration_t* embb_duration_max();

/**
 * Returns duration with minimum ticks representable by implementation.
 *
 * \return Pointer to duration with minimum value
 * \notthreadsafe
 * \see embb_duration_max()
 */
const embb_duration_t* embb_duration_min();

/**
 * Returns duration of length zero.
 *
 * \return Pointer to duration of length zero
 * \notthreadsafe
 */
const embb_duration_t* embb_duration_zero();

/**
 * Set duration from nanosecond ticks.
 *
 * \return EMBB_SUCCESS \n
 *         EMBB_UNDERFLOW if given nanosecond interval is too small to be
 *                        represented by implementation \n
 *         EMBB_OVERFLOW if given nanosecond interval is too large to be
 *                       represented by implementation
 * \notthreadsafe
 */
int embb_duration_set_nanoseconds(
  embb_duration_t* duration,
  /**< [OUT] Pointer to duration */
  unsigned long long nanoseconds
  /**< [IN] Nanosecond ticks */
  );

/**
 * Sets duration from microsecond ticks.
 *
 * \return EMBB_SUCCESS \n
 *         EMBB_UNDERFLOW if given microsecond interval is too small to be
 *                        represented by implementation \n
 *         EMBB_OVERFLOW if given microsecond interval is too large to be
 *                       represented by implementation
 * \notthreadsafe
 */
int embb_duration_set_microseconds(
  embb_duration_t* duration,
  /**< [OUT] Pointer to duration */
  unsigned long long microseconds
  /**< [IN] Microsecond ticks */
  );

/**
 * Sets duration from millisecond ticks.
 *
 * \return EMBB_SUCCESS \n
 *         EMBB_UNDERFLOW if given millisecond interval is too small to be
 *                        represented by implementation \n
 *         EMBB_OVERFLOW if given millisecond interval is too large to be
 *                       represented by implementation \n
 * \notthreadsafe
 */
int embb_duration_set_milliseconds(
  embb_duration_t* duration,
  /**< [OUT] Pointer to duration */
  unsigned long long milliseconds
  /**< [IN] Millisecond ticks */
  );

/**
 * Sets duration from second ticks.
 *
 * \return EMBB_SUCCESS \n
 *         EMBB_UNDERFLOW if given second interval is too small to be
 *                        represented by implementation \n
 *         EMBB_OVERFLOW if given second interval is too large to be
 *                       represented by implementation
 * \notthreadsafe
 */
int embb_duration_set_seconds(
  embb_duration_t* duration,
  /**< [OUT] Pointer to duration */
  unsigned long long seconds
  /**< [IN] Second ticks */
  );

/**
 * Adds two durations.
 *
 * Computest the sum of \c rhs and \c lhs and stores the result in \c lhs.
 *
 * \return EMBB_SUCCESS \n
 *         EMBB_OVERFLOW if sum is greater than embb_duration_max()
 * \notthreadsafe
 */
int embb_duration_add(
  embb_duration_t* lhs,
  /**< [IN/OUT] Left-hand side operand, overwritten by result of addition */
  const embb_duration_t* rhs
  /**< [IN] Right-hand side operand of addition */
  );

/**
 * Converts duration to nanosecond ticks.
 *
 * \return EMBB_SUCCESS \n
 *         EMBB_UNDERFLOW if duration contains fractions less than a
 *         nanosecond \n
 *         EMBB_OVERFLOW if duration is not representable by tick type
 * \notthreadsafe
 */
int embb_duration_as_nanoseconds(
  const embb_duration_t* duration,
  /**< [IN] Pointer to duration */
  unsigned long long* nanoseconds
  /**< [OUT] Pointer to nanosecond ticks of duration */
  );

/**
 * Converts duration to microsecond ticks.
 *
 * \return EMBB_SUCCESS \n
 *         EMBB_UNDERFLOW if duration contains fractions less than a
 *         microsecond \n
 *         EMBB_OVERFLOW if duration is not representable by tick type
 * \notthreadsafe
 */
int embb_duration_as_microseconds(
  const embb_duration_t* duration,
  /**< [IN] Pointer to duration */
  unsigned long long* microseconds
  /**< [OUT] Pointer to microsecond ticks of duration */
  );

/**
 * Converts duration to millisecond ticks.
 *
 * \return EMBB_SUCCESS \n
 *         EMBB_UNDERFLOW if duration contains fractions less than a
 *         millisecond \n
 *         EMBB_OVERFLOW if duration is not representable by tick type
 * \notthreadsafe
 */
int embb_duration_as_milliseconds(
  const embb_duration_t* duration,
  /**< [IN] Pointer to duration */
  unsigned long long* milliseconds
  /**< [OUT] Pointer to millisecond ticks of duration */
  );

/**
 * Converts duration to second ticks.
 *
 * \return EMBB_SUCCESS \n
 *         EMBB_UNDERFLOW if duration contains fractions less than a
 *         second \n
 *         EMBB_OVERFLOW if duration is not representable by tick type
 * \notthreadsafe
 */
int embb_duration_as_seconds(
  const embb_duration_t* duration,
  /**< [IN] Pointer to duration */
  unsigned long long* seconds
  /**< [OUT] Pointer to second ticks of duration */
  );

/**
 * Compares two durations.
 *
 * \return -1 if \c lhs < \c rhs \n
 *         0 if \c lhs == \c rhs \n
 *         1 if \c lhs > \c rhs
 * \notthreadsafe
 */
int embb_duration_compare(
  const embb_duration_t* lhs,
  /**< [IN] Pointer to left-hand side operand */
  const embb_duration_t* rhs
  /**< [IN] Pointer to right-hand side operand */
  );

/**
 * \}
 * \}
 */

#ifdef __cplusplus
} /* Close extern "C" { */
#endif

#endif /* EMBB_BASE_C_DURATION_H_ */