From d37dd46f65e44d602b62519ec747a6e920e31241 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Sat, 28 Sep 2013 07:29:01 -0700 Subject: [PATCH] Move RelativeTime to chrono, add ostream support --- Builds/VisualStudio2012/Beast.props | 2 +- Builds/VisualStudio2012/beast.vcxproj | 16 +- Builds/VisualStudio2012/beast.vcxproj.filters | 24 ++- beast/Chrono.h | 25 +++ beast/chrono/Chrono.cpp | 22 +++ .../time => beast/chrono}/RelativeTime.h | 25 ++- .../chrono/impl}/RelativeTime.cpp | 186 +++++++++++++++--- beast/crypto/impl/sha2/sha2.c | 15 ++ modules/beast_core/beast_core.cpp | 17 +- modules/beast_core/beast_core.h | 2 +- 10 files changed, 283 insertions(+), 51 deletions(-) create mode 100644 beast/Chrono.h create mode 100644 beast/chrono/Chrono.cpp rename {modules/beast_core/time => beast/chrono}/RelativeTime.h (94%) rename {modules/beast_core/time => beast/chrono/impl}/RelativeTime.cpp (59%) diff --git a/Builds/VisualStudio2012/Beast.props b/Builds/VisualStudio2012/Beast.props index 2428672bea..c7e4aad4d0 100644 --- a/Builds/VisualStudio2012/Beast.props +++ b/Builds/VisualStudio2012/Beast.props @@ -6,7 +6,7 @@ Level4 - _CRTDBG_MAP_ALLOC;_WIN32_WINNT=0x0600;%(PreprocessorDefinitions) + BEAST_COMPILING_STATIC_LIBARARY=1;_CRTDBG_MAP_ALLOC;_WIN32_WINNT=0x0600;%(PreprocessorDefinitions) true false %(AdditionalIncludeDirectories) diff --git a/Builds/VisualStudio2012/beast.vcxproj b/Builds/VisualStudio2012/beast.vcxproj index f39a231529..3cf894ed28 100644 --- a/Builds/VisualStudio2012/beast.vcxproj +++ b/Builds/VisualStudio2012/beast.vcxproj @@ -83,6 +83,7 @@ + @@ -134,6 +135,7 @@ + @@ -342,7 +344,6 @@ - @@ -394,6 +395,13 @@ + + + true + true + true + true + true @@ -1255,12 +1263,6 @@ true true - - true - true - true - true - true true diff --git a/Builds/VisualStudio2012/beast.vcxproj.filters b/Builds/VisualStudio2012/beast.vcxproj.filters index da38087a55..e83071e182 100644 --- a/Builds/VisualStudio2012/beast.vcxproj.filters +++ b/Builds/VisualStudio2012/beast.vcxproj.filters @@ -264,6 +264,12 @@ {44489531-f44a-439a-a6ea-d32c252b1e8b} + + {57dc7059-cbb2-437c-9c52-79825d9a4cf5} + + + {39886e0f-1607-4b7a-81cf-011d83dadee3} + @@ -491,9 +497,6 @@ beast_core\time - - beast_core\time - beast_core\time @@ -1200,6 +1203,12 @@ beast_core\thread + + beast + + + beast\chrono + @@ -1379,9 +1388,6 @@ beast_core\time - - beast_core\time - beast_core\time @@ -1769,6 +1775,12 @@ beast_core\thread + + beast\chrono\impl + + + beast\chrono + diff --git a/beast/Chrono.h b/beast/Chrono.h new file mode 100644 index 0000000000..26d809afd5 --- /dev/null +++ b/beast/Chrono.h @@ -0,0 +1,25 @@ +//------------------------------------------------------------------------------ +/* + This file is part of Beast: https://github.com/vinniefalco/Beast + Copyright 2013, Vinnie Falco + + 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 diff --git a/beast/chrono/Chrono.cpp b/beast/chrono/Chrono.cpp new file mode 100644 index 0000000000..549f9ba99e --- /dev/null +++ b/beast/chrono/Chrono.cpp @@ -0,0 +1,22 @@ +//------------------------------------------------------------------------------ +/* + This file is part of Beast: https://github.com/vinniefalco/Beast + Copyright 2013, Vinnie Falco + + 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" diff --git a/modules/beast_core/time/RelativeTime.h b/beast/chrono/RelativeTime.h similarity index 94% rename from modules/beast_core/time/RelativeTime.h rename to beast/chrono/RelativeTime.h index ed8c1f3ff2..54da15daa7 100644 --- a/modules/beast_core/time/RelativeTime.h +++ b/beast/chrono/RelativeTime.h @@ -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 +#include + +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 + diff --git a/modules/beast_core/time/RelativeTime.cpp b/beast/chrono/impl/RelativeTime.cpp similarity index 59% rename from modules/beast_core/time/RelativeTime.cpp rename to beast/chrono/impl/RelativeTime.cpp index 2e1cc607af..ca58862b29 100644 --- a/modules/beast_core/time/RelativeTime.cpp +++ b/beast/chrono/impl/RelativeTime.cpp @@ -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; } @@ -131,4 +248,11 @@ String RelativeTime::getDescription (const String& returnValueForZeroTime) const } return result.trimEnd(); -} \ No newline at end of file +} + +std::string RelativeTime::to_string () const +{ + return getDescription ().toStdString(); +} + +} diff --git a/beast/crypto/impl/sha2/sha2.c b/beast/crypto/impl/sha2/sha2.c index 45f996fb47..125aae2cae 100644 --- a/beast/crypto/impl/sha2/sha2.c +++ b/beast/crypto/impl/sha2/sha2.c @@ -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 diff --git a/modules/beast_core/beast_core.cpp b/modules/beast_core/beast_core.cpp index 0fa15dc8d8..af54ca760e 100644 --- a/modules/beast_core/beast_core.cpp +++ b/modules/beast_core/beast_core.cpp @@ -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" diff --git a/modules/beast_core/beast_core.h b/modules/beast_core/beast_core.h index 82f93005b5..e8c4cc9119 100644 --- a/modules/beast_core/beast_core.h +++ b/modules/beast_core/beast_core.h @@ -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"