1 /*
2  * Copyright (c) 2014-2017, Siemens AG. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #ifndef EMBB_BASE_INTERNAL_DURATION_INL_H_
28 #define EMBB_BASE_INTERNAL_DURATION_INL_H_
29 
30 #include <embb/base/internal/config.h>
31 
32 namespace embb {
33 namespace base {
34 
35 template<typename Tick>
Zero()36 const Duration<Tick>& Duration<Tick>::Zero() {
37   static Duration<Tick> zero;
38   return zero;
39 }
40 
41 #ifdef EMBB_PLATFORM_COMPILER_MSVC
42 // Suppress non-thread-safe static initialization warning
43 // in Max() and Min()
44 #pragma warning(push)
45 #pragma warning(disable : 4640)
46 #endif
47 
48 template<typename Tick>
Max()49 const Duration<Tick>& Duration<Tick>::Max() {
50   static Duration<Tick> maximum(Tick::Max());
51   return maximum;
52 }
53 
54 template<typename Tick>
Min()55 const Duration<Tick>& Duration<Tick>::Min() {
56   static Duration<Tick> minimum(Tick::Min());
57   return minimum;
58 }
59 
60 #ifdef EMBB_PLATFORM_COMPILER_MSVC
61 #pragma warning(pop) // Reset warning 4640
62 #endif
63 
64 template<typename Tick>
Duration()65 Duration<Tick>::Duration() /*: rep_(EMBB_DURATION_INIT)*/ {
66   rep_.nanoseconds = 0;
67   rep_.seconds = 0;
68 }
69 
70 template<typename Tick>
Duration(unsigned long long ticks)71 Duration<Tick>::Duration(unsigned long long ticks) {
72   /*: rep_(EMBB_DURATION_INIT) << does not work with vs2012*/
73   rep_.nanoseconds = 0;
74   rep_.seconds = 0;
75   Tick::SetAndCheck(rep_, ticks);
76 }
77 
78 template<typename Tick>
Duration(const Duration<Tick> & to_copy)79 Duration<Tick>::Duration(const Duration<Tick>& to_copy) {
80   /*: rep_(EMBB_DURATION_INIT)*/
81   rep_.nanoseconds = 0;
82   rep_.seconds = 0;
83   Tick::SetAndCheck(rep_, Tick::Get(to_copy.rep_));
84 }
85 
86 template<typename Tick>
87 Duration<Tick>& Duration<Tick>::operator=(const Duration<Tick>& to_assign) {
88   Tick::SetAndCheck(rep_, Tick::Get(to_assign.rep_));
89   return *this;
90 }
91 
92 template<typename Tick>
Count()93 unsigned long long Duration<Tick>::Count() const {
94   return Tick::Get(rep_);
95 }
96 
97 template<typename Tick>
98 Duration<Tick>& Duration<Tick>::operator+=(const Duration<Tick>& rhs) {
99   int status = embb_duration_add(&rep_, &(rhs.rep_));
100   Tick::CheckExceptions(status, "Add-assign duration");
101   return *this;
102 }
103 
104 template<typename Tick>
Duration(const embb_duration_t & duration)105 Duration<Tick>::Duration(const embb_duration_t& duration) : rep_() {
106   int status = Tick::Set(rep_, 0);
107   assert(status == EMBB_SUCCESS);
108   status = embb_duration_add(&rep_, &duration);
109   assert(status == EMBB_SUCCESS);
110 }
111 
112 } // namespace base
113 } // namespace embb
114 
115 #endif  // EMBB_BASE_INTERNAL_DURATION_INL_H_
116