Image Component Library (ICL)
|
00001 /******************************************************************** 00002 ** Image Component Library (ICL) ** 00003 ** ** 00004 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld ** 00005 ** Neuroinformatics Group ** 00006 ** Website: www.iclcv.org and ** 00007 ** http://opensource.cit-ec.de/projects/icl ** 00008 ** ** 00009 ** File : ICLUtils/src/ICLUtils/Time.h ** 00010 ** Module : ICLUtils ** 00011 ** Authors: Christof Elbrechter, Robert Haschke ** 00012 ** ** 00013 ** ** 00014 ** GNU LESSER GENERAL PUBLIC LICENSE ** 00015 ** This file may be used under the terms of the GNU Lesser General ** 00016 ** Public License version 3.0 as published by the ** 00017 ** ** 00018 ** Free Software Foundation and appearing in the file LICENSE.GPL ** 00019 ** included in the packaging of this file. Please review the ** 00020 ** following information to ensure the license requirements will ** 00021 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt ** 00022 ** ** 00023 ** The development of this software was supported by the ** 00024 ** Excellence Cluster EXC 277 Cognitive Interaction Technology. ** 00025 ** The Excellence Cluster EXC 277 is a grant of the Deutsche ** 00026 ** Forschungsgemeinschaft (DFG) in the context of the German ** 00027 ** Excellence Initiative. ** 00028 ** ** 00029 ********************************************************************/ 00030 00031 #include <string> 00032 #include <iostream> 00033 00034 #include <stdint.h> 00035 // ********************************************************************** 00036 // 00037 // Copyright (c) 2003-2005 ZeroC, Inc. All rights reserved. 00038 // 00039 // This copy of Ice is licensed to you under the terms described in the 00040 // ICE_LICENSE file included in this distribution. 00041 // 00042 // ********************************************************************** 00043 00044 #pragma once 00045 00046 00047 namespace icl{ 00048 namespace utils{ 00049 00051 class Time { 00052 public: 00053 00055 typedef int64_t value_type; 00056 00057 // undefined time: 0 00058 static const Time null; 00059 00060 Time(); 00061 00062 Time(value_type); 00063 00064 // No copy constructor and assignment operator necessary. The 00065 // automatically generated copy constructor and assignment 00066 // operator do the right thing. 00067 00068 static Time now(); 00069 static Time seconds(value_type); 00070 static Time milliSeconds(value_type); 00071 static Time microSeconds(value_type); 00072 00073 value_type toSeconds() const; 00074 value_type toMilliSeconds() const; 00075 value_type toMicroSeconds() const; 00076 00077 double toSecondsDouble() const; 00078 double toMilliSecondsDouble() const; 00079 double toMicroSecondsDouble() const; 00080 00081 std::string toString() const; 00082 00084 00095 std::string toStringFormated(const std::string &pattern, unsigned int bufferSize=32, bool zeropadded = false) const; 00096 00097 //xcf4cis stuff 00098 Time age() const { 00099 return Time::microSeconds(Time::now().m_usec - m_usec); 00100 } 00101 00103 inline std::string getAgeString() const{ 00104 return age().toString(); 00105 } 00106 00108 00114 inline void showAge(const std::string &title="") const{ 00115 std::cout << title << ":" << age().toMilliSecondsDouble() << "ms" << std::endl; 00116 } 00117 00118 00119 Time operator-() const 00120 { 00121 return Time(-m_usec); 00122 } 00123 00124 Time operator-(const Time& rhs) const 00125 { 00126 return Time(m_usec - rhs.m_usec); 00127 } 00128 00129 Time operator+(const Time& rhs) const 00130 { 00131 return Time(m_usec + rhs.m_usec); 00132 } 00133 00134 Time& operator+=(const Time& rhs) 00135 { 00136 m_usec += rhs.m_usec; 00137 return *this; 00138 } 00139 00140 Time& operator-=(const Time& rhs) 00141 { 00142 m_usec -= rhs.m_usec; 00143 return *this; 00144 } 00145 00146 bool operator<(const Time& rhs) const 00147 { 00148 return m_usec < rhs.m_usec; 00149 } 00150 00151 bool operator<=(const Time& rhs) const 00152 { 00153 return m_usec <= rhs.m_usec; 00154 } 00155 00156 bool operator>(const Time& rhs) const 00157 { 00158 return m_usec > rhs.m_usec; 00159 } 00160 00161 bool operator>=(const Time& rhs) const 00162 { 00163 return m_usec >= rhs.m_usec; 00164 } 00165 00166 bool operator==(const Time& rhs) const 00167 { 00168 return m_usec == rhs.m_usec; 00169 } 00170 00171 bool operator!=(const Time& rhs) const 00172 { 00173 return m_usec != rhs.m_usec; 00174 } 00175 00176 Time& operator*=(const Time& rhs) 00177 { 00178 m_usec *= rhs.m_usec; 00179 return *this; 00180 } 00181 00182 Time operator*(const Time& rhs) const 00183 { 00184 Time t; 00185 t.m_usec = m_usec * rhs.m_usec; 00186 return t; 00187 } 00188 00189 Time& operator/=(const Time& rhs) 00190 { 00191 m_usec /= rhs.m_usec; 00192 return *this; 00193 } 00194 00195 Time operator/(const Time& rhs) const 00196 { 00197 Time t; 00198 t.m_usec = m_usec / rhs.m_usec; 00199 return t; 00200 } 00201 00202 Time& operator*=(int rhs) 00203 { 00204 m_usec *= rhs; 00205 return *this; 00206 } 00207 00208 Time operator*(int rhs) const 00209 { 00210 Time t; 00211 t.m_usec = m_usec * rhs; 00212 return t; 00213 } 00214 00215 Time& operator/=(int rhs) 00216 { 00217 m_usec /= rhs; 00218 return *this; 00219 } 00220 00221 Time operator/(int rhs) const 00222 { 00223 Time t; 00224 t.m_usec = m_usec / rhs; 00225 return t; 00226 } 00227 00228 Time& operator*=(value_type rhs) 00229 { 00230 m_usec *= rhs; 00231 return *this; 00232 } 00233 00234 Time operator*(value_type rhs) const 00235 { 00236 Time t; 00237 t.m_usec = m_usec * rhs; 00238 return t; 00239 } 00240 00241 Time& operator/=(value_type rhs) 00242 { 00243 m_usec /= rhs; 00244 return *this; 00245 } 00246 00247 Time operator/(value_type rhs) const 00248 { 00249 Time t; 00250 t.m_usec = m_usec / rhs; 00251 return t; 00252 } 00253 00254 Time& operator*=(double rhs) 00255 { 00256 m_usec = static_cast<value_type>(static_cast<double>(m_usec) * rhs); 00257 return *this; 00258 } 00259 00260 Time operator*(double rhs) const 00261 { 00262 Time t; 00263 t.m_usec = static_cast<value_type>(static_cast<double>(m_usec) * rhs); 00264 return t; 00265 } 00266 00267 Time& operator/=(double rhs) 00268 { 00269 m_usec = static_cast<value_type>(static_cast<double>(m_usec) / rhs); 00270 return *this; 00271 } 00272 00273 Time operator/(double rhs) const 00274 { 00275 Time t; 00276 t.m_usec = static_cast<value_type>(static_cast<double>(m_usec) / rhs); 00277 return t; 00278 } 00279 00280 friend std::ostream& operator<<(std::ostream&, const Time&); 00281 00282 friend std::istream& operator>>(std::istream&, Time&); 00283 00284 private: 00285 00286 value_type m_usec; 00287 }; 00288 00290 std::ostream& operator<<(std::ostream&, const Time&); 00291 00293 std::istream& operator>>(std::istream&, Time&); 00294 00295 } // namespace utils 00296 } // End namespace icl 00297