duration.h 13.8 KB
Newer Older
1
/*
Marcus Winter committed
2
 * Copyright (c) 2014-2016, 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
 *
 * 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_DURATION_H_
#define EMBB_BASE_DURATION_H_

#include <embb/base/c/duration.h>
#include <embb/base/exceptions.h>
#include <cassert>

namespace embb {
namespace base {

/**
 * \defgroup CPP_BASE_TIMEDURATION Duration and Time
 *
 * Relative time durations and absolute time points
 *
 * \ingroup CPP_BASE
 */

/**
 * Represents a relative time duration for a given tick type.
 *
 * \notthreadsafe
49 50 51
 * \note The typedefs DurationSeconds, DurationMilliseconds,
 *       DurationMicroseconds, and DurationNanoseconds provide directly usable
 *       duration types.
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 274 275 276
 * \tparam Tick Possible tick types are Seconds, Milliseconds, Microseconds,
 *              Nanoseconds
 * \ingroup CPP_BASE_TIMEDURATION
 */
template<typename Tick>
class Duration {
 public:
  /**
   * Returns duration of length zero.
   * \return Duration of length zero
   */
  static const Duration<Tick>& Zero();

  /**
   * Returns duration with maximum ticks representable by implementation.
   *
   * This value depends on the tick type and on the platform.
   *
   * \return Reference to duration with maximum value
   */
  static const Duration<Tick>& Max();

  /**
   * Returns duration with minimum ticks representable by implementation.
   *
   * This value depends on the tick type and on the platform.
   *
   * \return Reference to duration with minimum value
   */
  static const Duration<Tick>& Min();

  /**
   * Constructs a duration of length zero.
   */
  Duration();

  /**
   * Constructs a duration with given number of ticks.
   */
  explicit Duration(
    unsigned long long ticks
    /**< [IN] Number of ticks */
    );

  /**
   * Constructs a duration by copying from an existing duration.
   */
  Duration(
    const Duration<Tick>& to_copy
    /**< [IN] %Duration to copy */
    );

  /**
   * Assigns an existing duration.
   *
   * \return Reference to \c *this
   */
  Duration<Tick>& operator=(
    const Duration<Tick>& to_assign
    /**< [IN] %Duration to assign */
    );

  /**
   * Returns the number of ticks of the duration.
   * \return Number of ticks of the duration
   */
  unsigned long long Count() const;

  /**
   * Assignment by addition of another duration with same tick type.
   *
   * \return Reference to \c *this
   */
  Duration<Tick>& operator+=(
    const Duration<Tick>& rhs
    /**< [IN] %Duration to add to this duration */
    );

 private:
  /**
   * Constructs a duration from the internal representation.
   *
   * \pre \c duration needs to fit into the duration type
   * \throws
   */
  Duration(
    const embb_duration_t& duration
    /**< [IN] %Duration to copy from */
    );

  /**
   * Internal representation from Base C.
   */
  embb_duration_t rep_;

  /**
   * For accessing rep_ and using Base C functionality.
   */
  friend class Time;

  /**
   * For accessing rep_ and using Base C functionality.
   */
  friend class ConditionVariable;
};

/**
 * Compares two durations (equality).
 *
 * \ingroup CPP_BASE_TIMEDURATION
 *
 * \return \c true if \c lhs is equal to \c rhs, otherwise \c false
 */
template<typename Tick>
bool operator==(
  const Duration<Tick>& lhs,
  /**< [IN] Left-hand side of equality operator */
  const Duration<Tick>& rhs
  /**< [IN] Right-hand side of equality operator */
  ) {
  return embb_duration_compare(&lhs, &rhs) == 0;
}

/**
 * Compares two durations (inequality).
 *
 * \ingroup CPP_BASE_TIMEDURATION
 *
 * \return \c true if \c lhs is not equal to \c rhs, otherwise \c false
 */
template<typename Tick>
bool operator!=(
  const Duration<Tick>& lhs,
  /**< [IN] Left-hand side of inequality operator */
  const Duration<Tick>& rhs
  /**< [IN] Right-hand side of inequality operator */
  ) {
  return embb_duration_compare(&lhs, &rhs) != 0;
}

/**
 * Compares two durations (less than)
 *
 * \ingroup CPP_BASE_TIMEDURATION
 *
 * \return \c true if \c lhs is shorter than \c rhs.
 */
template<typename Tick>
bool operator<(
  const Duration<Tick>& lhs,
  /**< [IN] Left-hand side of less than operator */
  const Duration<Tick>& rhs
  /**< [IN] Right-hand side of less than operator */
  ) {
  return embb_duration_compare(&lhs, &rhs) == -1;
}

/**
 * Compares two durations (greater than)
 *
 * \ingroup CPP_BASE_TIMEDURATION
 *
 * \return \c true if \c lhs is longer than \c rhs.
 */
template<typename Tick>
bool operator>(
  const Duration<Tick>& lhs,
  /**< [IN] Left-hand side of greater than operator */
  const Duration<Tick>& rhs
  /**< [IN] Right-hand side of greater than operator */
  ) {
  return embb_duration_compare(&lhs, &rhs) == 1;
}

/**
 * Compares two durations (less than or equal to)
 *
 * \ingroup CPP_BASE_TIMEDURATION
 *
 * \return \c true if \c lhs is shorter than or equal to \c rhs.
 */
template<typename Tick>
bool operator<=(
  const Duration<Tick>& lhs,
  /**< [IN] Left-hand side of less than or equal to operator */
  const Duration<Tick>& rhs
  /**< [IN] Right-hand side of less than or equal to operator */
  ) {
  return embb_duration_compare(&lhs, &rhs) < 1;
}

/**
 * Compares two durations (greater than or equal to)
 *
 * \ingroup CPP_BASE_TIMEDURATION
 *
 * \return \c true if \c lhs is longer than or equal to \c rhs.
 */
template<typename Tick>
bool operator>=(
  const Duration<Tick>& lhs,
  /**< [IN] Left-hand side of greater than or equal to operator */
  const Duration<Tick>& rhs
  /**< [IN] Right-hand side of greater than or equal to operator */
  ) {
  return embb_duration_compare(&lhs, &rhs) > -1;
}

/**
 * Adds two durations
 *
 * \ingroup CPP_BASE_TIMEDURATION
 *
 * \return Sum of \c lhs and \c rhs.
 */
template<typename Tick>
Duration<Tick> operator+(
  const Duration<Tick>& lhs,
  /**< [IN] Left-hand side of addition operator */
  const Duration<Tick>& rhs
  /**< [IN] Right-hand side of addition operator */
  ) {
  return Duration<Tick>(lhs.Count() + rhs.Count());
}

277 278
namespace internal {

279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
/**
 * Base class for ticks.
 */
class Tick {
 public:
  /**
   * Checks the status for under- and overflow and, in such a case, throws an 
   * exception.
   */
  static void CheckExceptions(
    int status,
    /**< [IN] Status code to check */
    const char* msg
    /**< [IN] Exception message if one is thrown */
    );
};

/**
 * %Seconds tick for Duration.
 *
 * \see Milliseconds, Microseconds, Nanoseconds
 * \ingroup CPP_BASE_TIMEDURATION
 */
class Seconds : public Tick {
 public:
  /**
   * Sets the ticks as seconds and returns the status of the duration operation.
   *
   * \return Status code of embb_duration_set_seconds()
   */
  static int Set(
    embb_duration_t& duration,
    /**< [OUT] %Duration representation to be considered */
    unsigned long long ticks
    /**< [IN] Number of ticks to set */
    );

  /**
   * Sets the ticks as seconds and calls CheckExceptions() with the status.
   */
  static void SetAndCheck(
    embb_duration_t& duration,
    /**< [OUT] %Duration representation to be considered */
    unsigned long long ticks
    /**< [IN] Number of ticks to set */
    );

  /**
   * Returns the number of ticks in seconds for the given duration.
   *
   * \return Number of second ticks of \c duration
   */
  static unsigned long long Get(
    const embb_duration_t& duration
    /**< [IN] %Duration representation to be considered */
    );

  /**
   * Returns the minimum number of second ticks for the available implementation.
   *
   * \return Minimum number of second ticks.
   */
  static unsigned long long Min();

  /**
   * Returns the maximum number of second ticks for the available implementation.
   *
   * \return Maximum number of second ticks.
   */
  static unsigned long long Max();
};

/**
 * %Milliseconds tick for Duration.
 *
 * \see Seconds, Microseconds, Nanoseconds
 * \ingroup CPP_BASE_TIMEDURATION
 */
class Milliseconds : public Tick {
 public:
  /**
   * Sets the ticks as milliseconds and returns the status of the duration
   * operation.
   *
   * \return Status code of embb_duration_set_milliseconds()
   */
  static int Set(
    embb_duration_t& duration,
    /**< [OUT] %Duration representation to be considered */
    unsigned long long ticks
    /**< [IN] Number of ticks to set */
    );

  /**
   * Sets the ticks as milliseconds and calls CheckExceptions() with the status.
   */
  static void SetAndCheck(
    embb_duration_t& duration,
    /**< [OUT] %Duration representation to be considered */
    unsigned long long ticks
    /**< [IN] Number of ticks to set */
    );

  /**
   * Returns the number of ticks in milliseconds for the given duration.
   *
   * \return Number of millisecond ticks of \c duration
   */
  static unsigned long long Get(
    const embb_duration_t& duration
    /**< [IN] %Duration representation to be considered */
    );

  /**
   * Returns the minimum number of millisecond ticks for the available
   * implementation.
   *
   * \return Minimum number of microsecond ticks.
   */
  static unsigned long long Min();

  /**
   * Returns the maximum number of millisecond ticks for the available
   * implementation.
   *
   * \return Maximum number of millisecond ticks.
   */
  static unsigned long long Max();
};

/**
 * %Microseconds tick for Duration.
 *
 * \see Seconds, Milliseconds, Nanoseconds
 * \ingroup CPP_BASE_TIMEDURATION
 */
class Microseconds : public Tick {
 public:
  /**
   * Sets the ticks as microseconds and returns the status of the duration
   * operation.
   *
   * \return Status code of embb_duration_set_microseconds()
   */
  static int Set(
    embb_duration_t& duration,
    /**< [OUT] %Duration representation to be considered */
    unsigned long long ticks
    /**< [IN] Number of ticks to set */
    );

  /**
   * Sets the ticks as microseconds and calls CheckExceptions() with the status.
   */
  static void SetAndCheck(
    embb_duration_t& duration,
    /**< [OUT] %Duration representation to be considered */
    unsigned long long ticks
    /**< [IN] Number of ticks to set */
    );

  /**
   * Returns the number of ticks in microseconds for the given duration.
   *
   * \return Number of microsecond ticks of \c duration
   */
  static unsigned long long Get(
    const embb_duration_t& duration
    /**< [IN] %Duration representation to be considered */
    );

  /**
   * Returns the minimum number of microsecond ticks for the available
   * implementation.
   *
   * \return Minimum number of microsecond ticks.
   */
  static unsigned long long Min();

  /**
   * Returns the maximum number of microsecond ticks for the available
   * implementation.
   *
   * \return Maximum number of microsecond ticks.
   */
  static unsigned long long Max();
};

/**
 * %Nanoseconds tick for Duration.
 *
 * \see Seconds, Milliseconds, Microseconds
 * \ingroup CPP_BASE_TIMEDURATION
 */
class Nanoseconds : public Tick {
 public:
  /**
   * Sets the ticks as nanoseconds and returns the status of the duration
   * operation.
   *
   * \return Status code of embb_duration_set_nanoseconds()
   */
  static int Set(
    embb_duration_t& duration,
    /**< [OUT] %Duration representation to be considered */
    unsigned long long ticks
    /**< [IN] Number of ticks to set */
    );

  /**
   * Sets the ticks as nanoseconds and calls CheckExceptions() with the status.
   */
  static void SetAndCheck(
    embb_duration_t& duration,
    /**< [OUT] %Duration representation to be considered */
    unsigned long long ticks
    /**< [IN] Number of ticks to set */
    );

  /**
   * Returns the number of ticks in nanoseconds for the given duration.
   *
   * \return Number of nanosecond ticks of \c duration
   */
  static unsigned long long Get(
    const embb_duration_t& duration
    /**< [IN] %Duration representation to be considered */
    );

  /**
   * Returns the minimum number of nanosecond ticks for the available
   * implementation.
   *
   * \return Minimum number of nanosecond ticks.
   */
  static unsigned long long Min();

  /**
   * Returns the maximum number of nanosecond ticks for the available
   * implementation.
   *
   * \return Maximum number of nanosecond ticks.
   */
  static unsigned long long Max();
};

525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
} // namespace internal

/**
 * Duration with seconds tick.
 *
 * \ingroup CPP_BASE_TIMEDURATION
 */
typedef Duration<internal::Seconds> DurationSeconds;
/**
 * Duration with milliseconds tick.
 *
 * \ingroup CPP_BASE_TIMEDURATION
 */
typedef Duration<internal::Milliseconds> DurationMilliseconds;
/**
 * Duration with microseconds tick.
 *
 * \ingroup CPP_BASE_TIMEDURATION
 */
typedef Duration<internal::Microseconds> DurationMicroseconds;
/**
 * Duration with nanoseconds tick.
 *
 * \ingroup CPP_BASE_TIMEDURATION
 */
typedef Duration<internal::Nanoseconds> DurationNanoseconds;

552 553 554 555 556
} // namespace base
} // namespace embb

#include <embb/base/internal/duration-inl.h>

557
#endif  // EMBB_BASE_DURATION_H_