Menu

Time.h

Go to the documentation of this file.
00001 /*******************************************************************************
00002  *
00003  * Copyright (c) 2009-2010 Philipp Werner <philipp.werner@st.ovgu.de>
00004  *                         Michael Schulze <mschulze@ivs.cs.uni-magdeburg.de>
00005  * All rights reserved.
00006  *
00007  *    Redistribution and use in source and binary forms, with or without
00008  *    modification, are permitted provided that the following conditions
00009  *    are met:
00010  *
00011  *    * Redistributions of source code must retain the above copyright
00012  *      notice, this list of conditions and the following disclaimer.
00013  *
00014  *    * Redistributions in binary form must reproduce the above copyright
00015  *      notice, this list of conditions and the following disclaimer in
00016  *      the documentation and/or other materials provided with the
00017  *      distribution.
00018  *
00019  *    * Neither the name of the copyright holders nor the names of
00020  *      contributors may be used to endorse or promote products derived
00021  *      from this software without specific prior written permission.
00022  *
00023  *
00024  *    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
00025  *    IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00026  *    TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00027  *    PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00028  *    OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00029  *    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00030  *    LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00031  *    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00032  *    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00033  *    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00034  *    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  *
00037  * $Id$
00038  *
00039  ******************************************************************************/
00040 
00041 
00042 #ifndef __TIME_H_2DD6595E115022__
00043 #define __TIME_H_2DD6595E115022__
00044 
00045 
00046 #ifdef __AVR__
00047 
00048 // AVR version
00049 namespace famouso {
00050     namespace mw {
00051         namespace afp {
00052             namespace shared {
00053 
00054 //#warning Time class for AVR platform missing. Need timing framework replacing dummy implementation.
00055 
00059                 class Time {
00060                         int t;
00061                     public:
00062 
00063                         // Template function to hide warning if unused
00064                         template <typename Time_Type>
00065                         static void get_current_time(Time_Type & time) {
00066                             int Warning__Time_class_for_AVR_platform_is_dummy_implementation__Need_timing_framework;
00067                             static int curr = 0;
00068                             time.t = curr;
00069                             curr++;
00070                         }
00071 
00072                         template <typename Time_Type>
00073                         static Time_Type current() {
00074                             Time t;
00075                             get_current_time(t);
00076                             return t;
00077                         }
00078 
00079                         Time & add_sec(unsigned int s) {
00080                             t += s;
00081                             return *this;
00082                         }
00083 
00084                         uint64_t get_usec() const {
00085                             return 0;
00086                         }
00087 
00088                         uint64_t get_sec() const {
00089                             return t;
00090                         }
00091 
00092                         bool operator<(const Time & t2) const {
00093                             return t < t2.t;
00094                         }
00095                 };
00096 
00097             } // namespace shared
00098         } // namespace afp
00099     } // namespace mw
00100 } // namespace famouso
00101 
00102 #else
00103 
00104 #include "boost/thread/xtime.hpp"
00105 
00106 // General purpose OS version
00107 namespace famouso {
00108     namespace mw {
00109         namespace afp {
00110             namespace shared {
00111 
00115                 class Time {
00116                         boost::xtime time;
00117                     public:
00118 
00119                         static void get_current_time(Time & time) {
00120                             boost::xtime_get(&time.time, boost::TIME_UTC);
00121                         }
00122 
00123                         static Time current() {
00124                             Time t;
00125                             get_current_time(t);
00126                             return t;
00127                         }
00128 
00129                         Time & add_sec(unsigned int s) {
00130                             time.sec += s;
00131                             return *this;
00132                         }
00133 
00134                         Time & add_usec(unsigned int us) {
00135                             time.nsec += us * 1000;
00136                             return *this;
00137                         }
00138 
00139                         uint64_t get_usec() const {
00140                             return time.sec * 1000000 + time.nsec / 1000;
00141                         }
00142 
00143                         uint64_t get_sec() const {
00144                             return time.sec;
00145                         }
00146 
00147                         bool operator<(const Time & t2) const {
00148                             return boost::xtime_cmp(time, t2.time) < 0;
00149                         }
00150 
00151                 };
00152 
00153             } // namespace shared
00154         } // namespace afp
00155     } // namespace mw
00156 } // namespace famouso
00157 
00158 #endif
00159 
00160 
00161 #endif // __TIME_H_2DD6595E115022__
00162