Move RelativeTime to chrono, add ostream support

This commit is contained in:
Vinnie Falco
2013-09-28 07:29:01 -07:00
parent 3f6e7aa05a
commit d37dd46f65
10 changed files with 283 additions and 51 deletions

View File

@@ -6,7 +6,7 @@
<ItemDefinitionGroup>
<ClCompile>
<WarningLevel>Level4</WarningLevel>
<PreprocessorDefinitions>_CRTDBG_MAP_ALLOC;_WIN32_WINNT=0x0600;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>BEAST_COMPILING_STATIC_LIBARARY=1;_CRTDBG_MAP_ALLOC;_WIN32_WINNT=0x0600;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<MinimalRebuild>false</MinimalRebuild>
<AdditionalIncludeDirectories>%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>

View File

@@ -83,6 +83,7 @@
<ClInclude Include="..\..\beast\Arithmetic.h" />
<ClInclude Include="..\..\beast\Atomic.h" />
<ClInclude Include="..\..\beast\ByteOrder.h" />
<ClInclude Include="..\..\beast\chrono\RelativeTime.h" />
<ClInclude Include="..\..\beast\Config.h" />
<ClInclude Include="..\..\beast\config\CompilerConfig.h" />
<ClInclude Include="..\..\beast\config\ConfigCheck.h" />
@@ -134,6 +135,7 @@
<ClInclude Include="..\..\beast\thread\SharedData.h" />
<ClInclude Include="..\..\beast\thread\SharedLockGuard.h" />
<ClInclude Include="..\..\beast\thread\SharedMutexAdapter.h" />
<ClInclude Include="..\..\beast\Chrono.h" />
<ClInclude Include="..\..\beast\TypeTraits.h" />
<ClInclude Include="..\..\beast\type_traits\IntegralConstant.h" />
<ClInclude Include="..\..\beast\type_traits\IsIntegral.h" />
@@ -342,7 +344,6 @@
<ClInclude Include="..\..\modules\beast_core\thread\TrackedMutex.h" />
<ClInclude Include="..\..\modules\beast_core\time\PerformanceCounter.h" />
<ClInclude Include="..\..\modules\beast_core\time\AtExitHook.h" />
<ClInclude Include="..\..\modules\beast_core\time\RelativeTime.h" />
<ClInclude Include="..\..\modules\beast_core\time\Time.h" />
<ClInclude Include="..\..\modules\beast_core\xml\XmlDocument.h" />
<ClInclude Include="..\..\modules\beast_core\xml\XmlElement.h" />
@@ -394,6 +395,13 @@
<ClInclude Include="BeastConfig.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\beast\chrono\Chrono.cpp" />
<ClCompile Include="..\..\beast\chrono\impl\RelativeTime.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\beast\crypto\Crypto.cpp" />
<ClCompile Include="..\..\beast\crypto\impl\Sha256.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
@@ -1255,12 +1263,6 @@
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\modules\beast_core\time\RelativeTime.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\modules\beast_core\time\Time.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>

View File

@@ -264,6 +264,12 @@
<Filter Include="beast\crypto\impl\sha2">
<UniqueIdentifier>{44489531-f44a-439a-a6ea-d32c252b1e8b}</UniqueIdentifier>
</Filter>
<Filter Include="beast\chrono">
<UniqueIdentifier>{57dc7059-cbb2-437c-9c52-79825d9a4cf5}</UniqueIdentifier>
</Filter>
<Filter Include="beast\chrono\impl">
<UniqueIdentifier>{39886e0f-1607-4b7a-81cf-011d83dadee3}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\modules\beast_core\beast_core.h">
@@ -491,9 +497,6 @@
<ClInclude Include="..\..\modules\beast_core\time\PerformanceCounter.h">
<Filter>beast_core\time</Filter>
</ClInclude>
<ClInclude Include="..\..\modules\beast_core\time\RelativeTime.h">
<Filter>beast_core\time</Filter>
</ClInclude>
<ClInclude Include="..\..\modules\beast_core\time\Time.h">
<Filter>beast_core\time</Filter>
</ClInclude>
@@ -1200,6 +1203,12 @@
<ClInclude Include="..\..\modules\beast_core\thread\ServiceQueue.h">
<Filter>beast_core\thread</Filter>
</ClInclude>
<ClInclude Include="..\..\beast\Chrono.h">
<Filter>beast</Filter>
</ClInclude>
<ClInclude Include="..\..\beast\chrono\RelativeTime.h">
<Filter>beast\chrono</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\modules\beast_core\containers\AbstractFifo.cpp">
@@ -1379,9 +1388,6 @@
<ClCompile Include="..\..\modules\beast_core\time\PerformanceCounter.cpp">
<Filter>beast_core\time</Filter>
</ClCompile>
<ClCompile Include="..\..\modules\beast_core\time\RelativeTime.cpp">
<Filter>beast_core\time</Filter>
</ClCompile>
<ClCompile Include="..\..\modules\beast_core\time\Time.cpp">
<Filter>beast_core\time</Filter>
</ClCompile>
@@ -1769,6 +1775,12 @@
<ClCompile Include="..\..\modules\beast_core\thread\ServiceQueue.cpp">
<Filter>beast_core\thread</Filter>
</ClCompile>
<ClCompile Include="..\..\beast\chrono\impl\RelativeTime.cpp">
<Filter>beast\chrono\impl</Filter>
</ClCompile>
<ClCompile Include="..\..\beast\chrono\Chrono.cpp">
<Filter>beast\chrono</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Text Include="..\..\TODO.txt">

25
beast/Chrono.h Normal file
View File

@@ -0,0 +1,25 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#ifndef BEAST_CHRONO_H_INCLUDED
#define BEAST_CHRONO_H_INCLUDED
#include "chrono/RelativeTime.h"
#endif

22
beast/chrono/Chrono.cpp Normal file
View File

@@ -0,0 +1,22 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include "BeastConfig.h"
#include "impl/RelativeTime.cpp"

View File

@@ -21,8 +21,16 @@
*/
//==============================================================================
#ifndef BEAST_RELATIVETIME_H_INCLUDED
#define BEAST_RELATIVETIME_H_INCLUDED
#ifndef BEAST_CHRONO_RELATIVETIME_H_INCLUDED
#define BEAST_CHRONO_RELATIVETIME_H_INCLUDED
#include "../Config.h"
#include "../strings/String.h"
#include <string>
#include <sstream>
namespace beast {
//==============================================================================
/** A relative measure of time.
@@ -135,7 +143,7 @@ public:
@see inMilliseconds, inSeconds, inMinutes, inHours, inDays, inWeeks
*/
String getDescription (const String& returnValueForZeroTime = "0") const;
std::string to_string () const;
//==============================================================================
/** Adds another RelativeTime to this one. */
@@ -173,4 +181,13 @@ RelativeTime operator+ (RelativeTime t1, RelativeTime t2) noexcept;
/** Subtracts two RelativeTimes. */
RelativeTime operator- (RelativeTime t1, RelativeTime t2) noexcept;
#endif // BEAST_RELATIVETIME_H_INCLUDED
inline std::ostream& operator<< (std::ostream& os, RelativeTime const& diff)
{
os << diff.to_string();
return os;
}
}
#endif

View File

@@ -21,48 +21,165 @@
*/
//==============================================================================
RelativeTime::RelativeTime (const double secs) noexcept : numSeconds (secs) {}
RelativeTime::RelativeTime (const RelativeTime& other) noexcept : numSeconds (other.numSeconds) {}
#include "RelativeTime.h"
// VFALCO TODO Migrate the localizable strings interfaces for this file
#ifndef NEEDS_TRANS
#define NEEDS_TRANS(s) (s)
#endif
#ifndef TRANS
#define TRANS(s) (s)
#endif
namespace beast {
RelativeTime::RelativeTime (const double secs) noexcept
: numSeconds (secs)
{
}
RelativeTime::RelativeTime (const RelativeTime& other) noexcept
: numSeconds (other.numSeconds)
{
}
RelativeTime::~RelativeTime() noexcept {}
//==============================================================================
RelativeTime RelativeTime::milliseconds (const int milliseconds) noexcept { return RelativeTime (milliseconds * 0.001); }
RelativeTime RelativeTime::milliseconds (const int64 milliseconds) noexcept { return RelativeTime (milliseconds * 0.001); }
RelativeTime RelativeTime::seconds (double s) noexcept { return RelativeTime (s); }
RelativeTime RelativeTime::minutes (const double numberOfMinutes) noexcept { return RelativeTime (numberOfMinutes * 60.0); }
RelativeTime RelativeTime::hours (const double numberOfHours) noexcept { return RelativeTime (numberOfHours * (60.0 * 60.0)); }
RelativeTime RelativeTime::days (const double numberOfDays) noexcept { return RelativeTime (numberOfDays * (60.0 * 60.0 * 24.0)); }
RelativeTime RelativeTime::weeks (const double numberOfWeeks) noexcept { return RelativeTime (numberOfWeeks * (60.0 * 60.0 * 24.0 * 7.0)); }
RelativeTime RelativeTime::milliseconds (const int milliseconds) noexcept
{
return RelativeTime (milliseconds * 0.001);
}
RelativeTime RelativeTime::milliseconds (const int64 milliseconds) noexcept
{
return RelativeTime (milliseconds * 0.001);
}
RelativeTime RelativeTime::seconds (double s) noexcept
{
return RelativeTime (s);
}
RelativeTime RelativeTime::minutes (const double numberOfMinutes) noexcept
{
return RelativeTime (numberOfMinutes * 60.0);
}
RelativeTime RelativeTime::hours (const double numberOfHours) noexcept
{
return RelativeTime (numberOfHours * (60.0 * 60.0));
}
RelativeTime RelativeTime::days (const double numberOfDays) noexcept
{
return RelativeTime (numberOfDays * (60.0 * 60.0 * 24.0));
}
RelativeTime RelativeTime::weeks (const double numberOfWeeks) noexcept
{
return RelativeTime (numberOfWeeks * (60.0 * 60.0 * 24.0 * 7.0));
}
//==============================================================================
int64 RelativeTime::inMilliseconds() const noexcept { return (int64) (numSeconds * 1000.0); }
double RelativeTime::inMinutes() const noexcept { return numSeconds / 60.0; }
double RelativeTime::inHours() const noexcept { return numSeconds / (60.0 * 60.0); }
double RelativeTime::inDays() const noexcept { return numSeconds / (60.0 * 60.0 * 24.0); }
double RelativeTime::inWeeks() const noexcept { return numSeconds / (60.0 * 60.0 * 24.0 * 7.0); }
int64 RelativeTime::inMilliseconds() const noexcept
{
return (int64) (numSeconds * 1000.0);
}
double RelativeTime::inMinutes() const noexcept
{
return numSeconds / 60.0;
}
double RelativeTime::inHours() const noexcept
{
return numSeconds / (60.0 * 60.0);
}
double RelativeTime::inDays() const noexcept
{
return numSeconds / (60.0 * 60.0 * 24.0);
}
double RelativeTime::inWeeks() const noexcept
{
return numSeconds / (60.0 * 60.0 * 24.0 * 7.0);
}
//==============================================================================
RelativeTime& RelativeTime::operator= (const RelativeTime& other) noexcept { numSeconds = other.numSeconds; return *this; }
RelativeTime RelativeTime::operator+= (RelativeTime t) noexcept { numSeconds += t.numSeconds; return *this; }
RelativeTime RelativeTime::operator-= (RelativeTime t) noexcept { numSeconds -= t.numSeconds; return *this; }
RelativeTime RelativeTime::operator+= (const double secs) noexcept { numSeconds += secs; return *this; }
RelativeTime RelativeTime::operator-= (const double secs) noexcept { numSeconds -= secs; return *this; }
RelativeTime RelativeTime::operator+= (RelativeTime t) noexcept
{
numSeconds += t.numSeconds; return *this;
}
RelativeTime operator+ (RelativeTime t1, RelativeTime t2) noexcept { return t1 += t2; }
RelativeTime operator- (RelativeTime t1, RelativeTime t2) noexcept { return t1 -= t2; }
RelativeTime RelativeTime::operator-= (RelativeTime t) noexcept
{
numSeconds -= t.numSeconds; return *this;
}
bool operator== (RelativeTime t1, RelativeTime t2) noexcept { return t1.inSeconds() == t2.inSeconds(); }
bool operator!= (RelativeTime t1, RelativeTime t2) noexcept { return t1.inSeconds() != t2.inSeconds(); }
bool operator> (RelativeTime t1, RelativeTime t2) noexcept { return t1.inSeconds() > t2.inSeconds(); }
bool operator< (RelativeTime t1, RelativeTime t2) noexcept { return t1.inSeconds() < t2.inSeconds(); }
bool operator>= (RelativeTime t1, RelativeTime t2) noexcept { return t1.inSeconds() >= t2.inSeconds(); }
bool operator<= (RelativeTime t1, RelativeTime t2) noexcept { return t1.inSeconds() <= t2.inSeconds(); }
RelativeTime RelativeTime::operator+= (const double secs) noexcept
{
numSeconds += secs; return *this;
}
RelativeTime RelativeTime::operator-= (const double secs) noexcept
{
numSeconds -= secs; return *this;
}
RelativeTime operator+ (RelativeTime t1, RelativeTime t2) noexcept
{
return t1 += t2;
}
RelativeTime operator- (RelativeTime t1, RelativeTime t2) noexcept
{
return t1 -= t2;
}
bool operator== (RelativeTime t1, RelativeTime t2) noexcept
{
return t1.inSeconds() == t2.inSeconds();
}
bool operator!= (RelativeTime t1, RelativeTime t2) noexcept
{
return t1.inSeconds() != t2.inSeconds();
}
bool operator> (RelativeTime t1, RelativeTime t2) noexcept
{
return t1.inSeconds() > t2.inSeconds();
}
bool operator< (RelativeTime t1, RelativeTime t2) noexcept
{
return t1.inSeconds() < t2.inSeconds();
}
bool operator>= (RelativeTime t1, RelativeTime t2) noexcept
{
return t1.inSeconds() >= t2.inSeconds();
}
bool operator<= (RelativeTime t1, RelativeTime t2) noexcept
{
return t1.inSeconds() <= t2.inSeconds();
}
//==============================================================================
static void translateTimeField (String& result, int n, const char* singular, const char* plural)
{
result << TRANS (n == 1 ? singular : plural)
result << TRANS (String((n == 1) ? singular : plural))
.replace (n == 1 ? "1" : "2", String (n))
<< ' ';
}
@@ -98,7 +215,7 @@ String RelativeTime::getDescription (const String& returnValueForZeroTime) const
n = std::abs ((int) inHours()) % 24;
if (n > 0)
{
translateTimeField (result, n, NEEDS_TRANS("1 hr"), NEEDS_TRANS("2 hrs"));
translateTimeField (result, n, NEEDS_TRANS("1 hour"), NEEDS_TRANS("2 hours"));
++fieldsShown;
}
@@ -107,7 +224,7 @@ String RelativeTime::getDescription (const String& returnValueForZeroTime) const
n = std::abs ((int) inMinutes()) % 60;
if (n > 0)
{
translateTimeField (result, n, NEEDS_TRANS("1 min"), NEEDS_TRANS("2 mins"));
translateTimeField (result, n, NEEDS_TRANS("1 minute"), NEEDS_TRANS("2 minutes"));
++fieldsShown;
}
@@ -116,7 +233,7 @@ String RelativeTime::getDescription (const String& returnValueForZeroTime) const
n = std::abs ((int) inSeconds()) % 60;
if (n > 0)
{
translateTimeField (result, n, NEEDS_TRANS("1 sec"), NEEDS_TRANS("2 secs"));
translateTimeField (result, n, NEEDS_TRANS("1 seconds"), NEEDS_TRANS("2 seconds"));
++fieldsShown;
}
@@ -132,3 +249,10 @@ String RelativeTime::getDescription (const String& returnValueForZeroTime) const
return result.trimEnd();
}
std::string RelativeTime::to_string () const
{
return getDescription ().toStdString();
}
}

View File

@@ -181,6 +181,7 @@ typedef uint64 sha2_word64; /* Exactly 8 bytes */
*/
/* Shift-right (used in SHA-256, SHA-384, and SHA-512): */
#define R(b,x) ((x) >> (b))
/* 32-bit Rotate-right (used in SHA-256): */
#define S32(b,x) (((x) >> (b)) | ((x) << (32 - (b))))
/* 64-bit Rotate-right (used in SHA-384 and SHA-512): */
@@ -1050,3 +1051,17 @@ char* SHA384_Data(const sha2_byte* data, size_t len, char digest[SHA384_DIGEST_S
SHA384_Update(&context, data, len);
return SHA384_End(&context, digest);
}
#undef R
#undef S32
#undef S64
#undef Ch
#undef Maj
#undef Sigma0_256
#undef Sigma1_256
#undef sigma0_256
#undef sigma1_256
#undef Sigma0_512
#undef Sigma1_512
#undef sigma0_512
#undef sigma1_512

View File

@@ -221,7 +221,6 @@ namespace beast
#include "time/PerformanceCounter.cpp"
#include "time/AtExitHook.cpp"
#include "time/RelativeTime.cpp"
#include "time/Time.cpp"
#include "xml/XmlDocument.cpp"
@@ -321,5 +320,21 @@ void beast_reportFatalError (char const* message, char const* fileName, int line
//------------------------------------------------------------------------------
// When we compile the static library, which is really just for browsing the
// sources in the Visual Studio IDE, we want to compile each individual unity
// .cpp in order to make sure that it builds by itself. An application that
// uses beast will typically include all of these .cpp files in one of its
// own unity .cpp
#if ! BEAST_COMPILING_STATIC_LIBARARY
/*
#include "../../beast/chrono/Chrono.cpp"
#include "../../beast/crypto/Crypto.cpp"
#include "../../beast/http/HTTP.cpp"
#include "../../beast/net/Net.cpp"
#include "../../beast/strings/Strings.cpp"
#include "../../beast/utility/Utility.cpp"
*/
#endif
// Must be outside the namespace
#include "system/BoostPlaceholdersFix.cpp"

View File

@@ -58,6 +58,7 @@
#include "../../beast/TypeTraits.h"
#include "../../beast/Thread.h"
#include "../../beast/Utility.h"
#include "../../beast/Chrono.h"
#include "system/StandardIncludes.h"
@@ -82,7 +83,6 @@ class FileOutputStream;
#include "time/AtExitHook.h"
#include "diagnostic/LeakChecked.h"
#include "time/RelativeTime.h"
#include "time/Time.h"
#include "threads/ScopedLock.h"
#include "threads/CriticalSection.h"