From 6cdbf3f05d387ae21aa3ed4feade6a096df449d9 Mon Sep 17 00:00:00 2001 From: vng Date: Sat, 25 Jun 2011 00:43:37 +0300 Subject: [PATCH] Remove TimeInSec from Platform. We have my::Timer cross platform class. --- base/base_tests/base_tests.pro | 1 + base/base_tests/timer_test.cpp | 32 +++++++++++++++++++++ base/timer.cpp | 15 +++++++++- base/timer.hpp | 7 ++++- generator/generator_tool/generator_tool.cpp | 1 - iphone/Maps/Platform/IPhonePlatform.hpp | 23 +++++++-------- iphone/Maps/Platform/IPhonePlatform.mm | 14 --------- map/framework.cpp | 27 ++++++----------- map/framework.hpp | 2 ++ platform/platform.hpp | 3 -- platform/platform_tests/platform_test.cpp | 21 -------------- platform/qtplatform.cpp | 8 +----- 12 files changed, 75 insertions(+), 79 deletions(-) create mode 100644 base/base_tests/timer_test.cpp diff --git a/base/base_tests/base_tests.pro b/base/base_tests/base_tests.pro index ee58014bfc..ed0f561557 100644 --- a/base/base_tests/base_tests.pro +++ b/base/base_tests/base_tests.pro @@ -30,5 +30,6 @@ SOURCES += \ commands_queue_test.cpp \ buffer_vector_test.cpp \ assert_test.cpp \ + timer_test.cpp \ HEADERS += diff --git a/base/base_tests/timer_test.cpp b/base/base_tests/timer_test.cpp new file mode 100644 index 0000000000..ec9bb8e367 --- /dev/null +++ b/base/base_tests/timer_test.cpp @@ -0,0 +1,32 @@ +#include "../../testing/testing.hpp" + +#include "../timer.hpp" +#include "../logging.hpp" + + +UNIT_TEST(Timer_Seconds) +{ + my::Timer timer; + + double t1 = timer.ElapsedSeconds(); + double s = 0.0; + for (int i = 0; i < 10000000; ++i) + s += i*0.01; + double t2 = timer.ElapsedSeconds(); + + TEST_NOT_EQUAL(s, 0.0, ("Fictive, to prevent loop optimization")); + TEST_NOT_EQUAL(t1, t2, ("Timer values should not be equal")); + +#ifndef DEBUG + t1 = timer.ElapsedSeconds(); + for (int i = 0; i < 10000000; ++i) {} + t2 = timer.ElapsedSeconds(); + + TEST_EQUAL(t1, t2, ("Timer values should be equal: compiler loop optimization!")); +#endif +} + +UNIT_TEST(Tiner_CurrentStringTime) +{ + LOG(LINFO, (my::FormatCurrentTime())); +} diff --git a/base/timer.cpp b/base/timer.cpp index 2f3f4b1aa8..a789a57956 100644 --- a/base/timer.cpp +++ b/base/timer.cpp @@ -2,6 +2,7 @@ #include "../std/target_os.hpp" #include "../std/time.hpp" +#include "../std/ctime.hpp" #include "../std/stdint.hpp" namespace my @@ -12,7 +13,7 @@ Timer::Timer() Reset(); } -double Timer::LocalTime() const +double Timer::LocalTime() { #ifdef OMIM_OS_WINDOWS FILETIME ft; @@ -39,4 +40,16 @@ void Timer::Reset() m_startTime = LocalTime(); } +string FormatCurrentTime() +{ + time_t t = time(NULL); + string s = string(ctime(&t)); + + for (size_t i = 0; i < s.size(); ++i) + if (s[i] == ' ') s[i] = '_'; + + s.erase(s.size() - 1, 1); + return s; +} + } diff --git a/base/timer.hpp b/base/timer.hpp index d08c5eb30e..79b2c923fb 100644 --- a/base/timer.hpp +++ b/base/timer.hpp @@ -1,5 +1,7 @@ #pragma once +#include "../std/string.hpp" + namespace my { @@ -8,12 +10,15 @@ class Timer { double m_startTime; - double LocalTime() const; + static double LocalTime(); public: Timer(); + double ElapsedSeconds() const; void Reset(); }; +string FormatCurrentTime(); + } diff --git a/generator/generator_tool/generator_tool.cpp b/generator/generator_tool/generator_tool.cpp index 45a62411d3..3224a5b7a0 100644 --- a/generator/generator_tool/generator_tool.cpp +++ b/generator/generator_tool/generator_tool.cpp @@ -22,7 +22,6 @@ #include "../../3party/gflags/src/gflags/gflags.h" -#include "../../std/ctime.hpp" #include "../../std/iostream.hpp" #include "../../std/iomanip.hpp" #include "../../std/numeric.hpp" diff --git a/iphone/Maps/Platform/IPhonePlatform.hpp b/iphone/Maps/Platform/IPhonePlatform.hpp index 629713fdee..b33a83d606 100644 --- a/iphone/Maps/Platform/IPhonePlatform.hpp +++ b/iphone/Maps/Platform/IPhonePlatform.hpp @@ -1,14 +1,13 @@ #pragma once #include "../../platform/platform.hpp" -#import class IPhonePlatform : public Platform { public: IPhonePlatform(); virtual ~IPhonePlatform(); - virtual double TimeInSec() const; + virtual string WritableDir() const; virtual string ResourcesDir() const; virtual string ReadPathForFile(char const * file) const; @@ -16,23 +15,21 @@ public: virtual bool GetFileSize(string const & file, uint64_t & size) const; virtual bool RenameFileX(string const & original, string const & newName) const; virtual int CpuCores() const; - virtual double VisualScale() const; - virtual string const SkinName() const; - virtual bool IsMultiSampled() const; - virtual bool DoPeriodicalUpdate() const; - virtual double PeriodicalUpdateInterval() const; - virtual vector GetFontNames() const; - virtual bool IsBenchmarking() const; - virtual bool IsVisualLog() const; + virtual double VisualScale() const; + virtual string const SkinName() const; + virtual bool IsMultiSampled() const; + virtual bool DoPeriodicalUpdate() const; + virtual double PeriodicalUpdateInterval() const; + virtual vector GetFontNames() const; + virtual bool IsBenchmarking() const; + virtual bool IsVisualLog() const; virtual string const DeviceID() const; - virtual unsigned ScaleEtalonSize() const; + virtual unsigned ScaleEtalonSize() const; private: string m_deviceID; string m_skinName; double m_visualScale; - NSDate * m_startDate; - NSDateFormatter * m_dateFormatter; bool m_isMultiSampled; bool m_doPeriodicalUpdate; double m_periodicalUpdateInterval; diff --git a/iphone/Maps/Platform/IPhonePlatform.mm b/iphone/Maps/Platform/IPhonePlatform.mm index fcf9aace59..a90b931e87 100644 --- a/iphone/Maps/Platform/IPhonePlatform.mm +++ b/iphone/Maps/Platform/IPhonePlatform.mm @@ -26,10 +26,6 @@ IPhonePlatform::IPhonePlatform() m_writablePath = [docsDir UTF8String]; m_writablePath += '/'; - m_startDate = [[NSDate alloc] init]; - m_dateFormatter = [[NSDateFormatter alloc] init]; - [m_dateFormatter setDateFormat:@"EEE_MMM_dd_HH:mm:ss_yyyy"]; - /// Hardcoding screen resolution depending on the device we are running. m_visualScale = 1.0; m_skinName = "basic.skn"; @@ -72,16 +68,6 @@ IPhonePlatform::IPhonePlatform() IPhonePlatform::~IPhonePlatform() { - [m_dateFormatter release]; - [m_startDate release]; -} - -double IPhonePlatform::TimeInSec() const -{ - NSDate * now = [[NSDate alloc] init]; - double interval = [now timeIntervalSinceDate:m_startDate]; - [now release]; - return interval; } string IPhonePlatform::ReadPathForFile(char const * file) const diff --git a/map/framework.cpp b/map/framework.cpp index e2fb80c616..e0b9957fb5 100644 --- a/map/framework.cpp +++ b/map/framework.cpp @@ -21,7 +21,6 @@ #include "../std/algorithm.hpp" #include "../std/fstream.hpp" -#include "../std/ctime.hpp" #include "../version/version.hpp" @@ -355,12 +354,7 @@ void FrameWork::AddRedrawCommandSure() m_centeringMode(EDoNothing), m_maxDuration(0) { - time_t curTime = time(NULL); - m_startTime = string(ctime(&curTime)); - for (unsigned i = 0; i < m_startTime.size(); ++i) - if (m_startTime[i] == ' ') - m_startTime[i] = '_'; - m_startTime = m_startTime.substr(0, m_startTime.size() - 1); + m_startTime = my::FormatCurrentTime(); m_informationDisplay.setBottomShift(bottomShift); #ifdef DRAW_TOUCH_POINTS @@ -816,7 +810,7 @@ void FrameWork::AddRedrawCommandSure() m_renderQueue.renderStatePtr()->m_isEmptyModelActual = false; } - if (m_navigator.Update(GetPlatform().TimeInSec())) + if (m_navigator.Update(m_timer.ElapsedSeconds())) Invalidate(); } @@ -1014,8 +1008,7 @@ void FrameWork::AddRedrawCommandSure() { m2::PointD ptShift = m_renderQueue.renderState().coordSystemShift(true); m2::PointD pos = m_navigator.OrientPoint(e.Pos()) + ptShift; - m_navigator.StartDrag(pos, - GetPlatform().TimeInSec()); + m_navigator.StartDrag(pos, m_timer.ElapsedSeconds()); #ifdef DRAW_TOUCH_POINTS m_informationDisplay.setDebugPoint(0, pos); @@ -1032,7 +1025,7 @@ void FrameWork::AddRedrawCommandSure() m2::PointD ptShift = m_renderQueue.renderState().coordSystemShift(true); m2::PointD pos = m_navigator.OrientPoint(e.Pos()) + ptShift; - m_navigator.DoDrag(pos, GetPlatform().TimeInSec()); + m_navigator.DoDrag(pos, m_timer.ElapsedSeconds()); #ifdef DRAW_TOUCH_POINTS m_informationDisplay.setDebugPoint(0, pos); @@ -1048,9 +1041,7 @@ void FrameWork::AddRedrawCommandSure() m2::PointD pos = m_navigator.OrientPoint(e.Pos()) + ptShift; - m_navigator.StopDrag(pos, - GetPlatform().TimeInSec(), - true); + m_navigator.StopDrag(pos, m_timer.ElapsedSeconds(), true); #ifdef DRAW_TOUCH_POINTS m_informationDisplay.setDebugPoint(0, m2::PointD(0, 0)); @@ -1076,7 +1067,7 @@ void FrameWork::AddRedrawCommandSure() ? m_navigator.OrientPoint(e.Pt()) + m_renderQueue.renderState().coordSystemShift(true) : m_navigator.Screen().PixelRect().Center(); - m_navigator.ScaleToPoint(pt, e.ScaleFactor(), GetPlatform().TimeInSec()); + m_navigator.ScaleToPoint(pt, e.ScaleFactor(), m_timer.ElapsedSeconds()); UpdateNow(); } @@ -1110,7 +1101,7 @@ void FrameWork::AddRedrawCommandSure() pt2 += ptDiff; } - m_navigator.StartScale(pt1, pt2, GetPlatform().TimeInSec()); + m_navigator.StartScale(pt1, pt2, m_timer.ElapsedSeconds()); #ifdef DRAW_TOUCH_POINTS m_informationDisplay.setDebugPoint(0, pt1); @@ -1136,7 +1127,7 @@ void FrameWork::AddRedrawCommandSure() pt2 += ptDiff; } - m_navigator.DoScale(pt1, pt2, GetPlatform().TimeInSec()); + m_navigator.DoScale(pt1, pt2, m_timer.ElapsedSeconds()); #ifdef DRAW_TOUCH_POINTS m_informationDisplay.setDebugPoint(0, pt1); @@ -1162,7 +1153,7 @@ void FrameWork::AddRedrawCommandSure() pt2 += ptDiff; } - m_navigator.StopScale(pt1, pt2, GetPlatform().TimeInSec()); + m_navigator.StopScale(pt1, pt2, m_timer.ElapsedSeconds()); #ifdef DRAW_TOUCH_POINTS m_informationDisplay.setDebugPoint(0, m2::PointD(0, 0)); diff --git a/map/framework.hpp b/map/framework.hpp index e9156d866a..99641c2aab 100644 --- a/map/framework.hpp +++ b/map/framework.hpp @@ -35,6 +35,7 @@ #include "../base/logging.hpp" #include "../base/profiler.hpp" #include "../base/mutex.hpp" +#include "../base/timer.hpp" #include "../std/bind.hpp" #include "../std/function.hpp" @@ -157,6 +158,7 @@ class FrameWork vector m_benchmarkResults; my::Timer m_benchmarksTimer; string m_startTime; + my::Timer m_timer; struct Benchmark { diff --git a/platform/platform.hpp b/platform/platform.hpp index 8649a83025..93c6e10228 100644 --- a/platform/platform.hpp +++ b/platform/platform.hpp @@ -15,9 +15,6 @@ class Platform public: virtual ~Platform() {} - /// Time in seconds passed from application start - virtual double TimeInSec() const = 0; - /// @return always the same writable dir for current user with slash at the end virtual string WritableDir() const = 0; /// @return full path to file in user's writable directory diff --git a/platform/platform_tests/platform_test.cpp b/platform/platform_tests/platform_test.cpp index 8ac08f5460..7fcd08b03b 100644 --- a/platform/platform_tests/platform_test.cpp +++ b/platform/platform_tests/platform_test.cpp @@ -8,27 +8,6 @@ #include "../../base/start_mem_debug.hpp" -UNIT_TEST(TimeInSec) -{ - Platform & pl = GetPlatform(); - - double t1 = pl.TimeInSec(); - double s = 0.0; - for (int i = 0; i < 10000000; ++i) - s += i*0.01; - double t2 = pl.TimeInSec(); - - TEST_NOT_EQUAL(s, 0.0, ("Fictive, to prevent loop optimization")); - TEST_NOT_EQUAL(t1, t2, ("Timer values should not be equal")); - -#ifndef DEBUG - t1 = pl.TimeInSec(); - for (int i = 0; i < 10000000; ++i) {} - t2 = pl.TimeInSec(); - - TEST_EQUAL(t1, t2, ("Timer values should be equal: compiler loop optimization!")); -#endif -} char const * TEST_FILE_NAME = "some_temporary_unit_test_file.tmp"; diff --git a/platform/qtplatform.cpp b/platform/qtplatform.cpp index 6af19652f5..288755f96f 100644 --- a/platform/qtplatform.cpp +++ b/platform/qtplatform.cpp @@ -2,8 +2,8 @@ #include "../base/assert.hpp" #include "../base/macros.hpp" -#include "../base/timer.hpp" #include "../base/logging.hpp" + #include "../defines.hpp" #include @@ -163,7 +163,6 @@ static bool IsDirectoryWritable(string const & dir) //////////////////////////////////////////////////////////////////////////////////////// class QtPlatform : public Platform { - my::Timer m_startTime; string m_writableDir; string m_resourcesDir; @@ -232,11 +231,6 @@ public: InitResourcesDir(m_resourcesDir); } - virtual double TimeInSec() const - { - return m_startTime.ElapsedSeconds(); - } - /// @return path to /data/ with ending slash virtual string WritableDir() const {