[Orxonox-commit 6441] r11098 - code/trunk/src/libraries/util

muemart at orxonox.net muemart at orxonox.net
Tue Jan 26 20:32:16 CET 2016


Author: muemart
Date: 2016-01-26 20:32:16 +0100 (Tue, 26 Jan 2016)
New Revision: 11098

Removed:
   code/trunk/src/libraries/util/ImplicitConversion.h
Modified:
   code/trunk/src/libraries/util/Convert.h
   code/trunk/src/libraries/util/MultiType.h
   code/trunk/src/libraries/util/Sleep.cc
   code/trunk/src/libraries/util/SubString.h
Log:
Minor C++11 improvements:
- Drop ImplicitConversion.h in favor of std::is_convertible.
- Move constructor & assignment for MultiType and SubString. I'm not sure if the MultiType should convert types when moving. Currently it doesn't, because otherwise it would have no benefit over copying.
- Use standard library functions for sleeping.

Modified: code/trunk/src/libraries/util/Convert.h
===================================================================
--- code/trunk/src/libraries/util/Convert.h	2016-01-25 20:47:54 UTC (rev 11097)
+++ code/trunk/src/libraries/util/Convert.h	2016-01-26 19:32:16 UTC (rev 11098)
@@ -127,10 +127,10 @@
 #include <string>
 #include <sstream>
 #include <typeinfo>
+#include <type_traits>
 #include <loki/TypeManip.h>
 
 #include "Output.h"
-#include "ImplicitConversion.h"
 
 // disable warnings about possible loss of data
 #ifdef ORXONOX_COMPILER_MSVC
@@ -307,7 +307,7 @@
     template <class FromType, class ToType>
     struct ConverterExplicit
     {
-        enum { probe = ImplicitConversion<FromType, ToType>::exists };
+        static constexpr bool probe = std::is_convertible<FromType, ToType>::value;
         ORX_FORCEINLINE static bool convert(ToType* output, const FromType& input)
         {
             // Use the probe's value to delegate to the right function

Deleted: code/trunk/src/libraries/util/ImplicitConversion.h
===================================================================
--- code/trunk/src/libraries/util/ImplicitConversion.h	2016-01-25 20:47:54 UTC (rev 11097)
+++ code/trunk/src/libraries/util/ImplicitConversion.h	2016-01-26 19:32:16 UTC (rev 11098)
@@ -1,94 +0,0 @@
-/*
- *   ORXONOX - the hottest 3D action shooter ever to exist
- *                    > www.orxonox.net <
- *
- *
- *   License notice:
- *
- *   This program is free software; you can redistribute it and/or
- *   modify it under the terms of the GNU General Public License
- *   as published by the Free Software Foundation; either version 2
- *   of the License, or (at your option) any later version.
- *
- *   This program is distributed in the hope that it will be useful,
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *   GNU General Public License for more details.
- *
- *   You should have received a copy of the GNU General Public License
- *   along with this program; if not, write to the Free Software
- *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- *
- *   Author:
- *      Reto Grieder
- *   Co-authors:
- *      ...
- *
- */
-
-/**
-    @file
-    @ingroup Util
-    @brief Declaration of various helper templates.
-*/
-
-#ifndef _TemplateUtils_H__
-#define _TemplateUtils_H__
-
-#include "UtilPrereqs.h"
-
-namespace orxonox
-{
-    ///////////////////////////////////////////////////
-    // Static detection of implicit type conversions //
-    ///////////////////////////////////////////////////
-
-// disable warnings about possible loss of data
-#ifdef ORXONOX_COMPILER_MSVC
-#  pragma warning(push)
-#  pragma warning(disable:4244)
-#endif
-// GCC generates warnings when implicitely casting from float to int for instance.
-#ifdef ORXONOX_COMPILER_GCC
-#  pragma GCC system_header
-#endif
-
-    /**
-    @brief
-        Detects whether you can implicitely cast FromType to ToType.
-
-        Usage: ImplicitConversion<FromType, ToType>::exists
-        This gives you a compile time constant boolean in the form of an enum value.
-    @note
-        The idea to use the sizeof() operator on return values to determine function existence
-        is described in 'Modern C++ design' by Alexandrescu (2001).
-    */
-    template <class FromType, class ToType>
-    class ImplicitConversion
-    {
-    private:
-        // static class, no instances allowed:
-        ImplicitConversion() = delete;
-        ImplicitConversion(const ImplicitConversion&) = delete;
-        ImplicitConversion& operator=(const ImplicitConversion&) = delete;
-        ~ImplicitConversion() = delete;
-
-        // Gets chosen only if there is an implicit conversion from FromType to ToType.
-        static char test(ToType);
-        // Accepts any argument. Why do we not use a template? The reason is that with templates,
-        // the function above is only taken iff it is an exact type match. But since we want to
-        // check for implicit conversion, we have to use the ellipsis.
-        static long long test(...);
-        static FromType object; // helper object to handle private c'tor and d'tor
-    public:
-        // test(object) only has 'long long' return type iff the compiler doesn't choose test(...)
-        enum { exists = (sizeof(test(object)) == sizeof(char)) };
-    };
-
-#ifdef ORXONOX_COMPILER_MSVC
-#  pragma warning(pop)
-#endif
-
-}
-
-#endif /* _TemplateUtils_H__ */

Modified: code/trunk/src/libraries/util/MultiType.h
===================================================================
--- code/trunk/src/libraries/util/MultiType.h	2016-01-25 20:47:54 UTC (rev 11097)
+++ code/trunk/src/libraries/util/MultiType.h	2016-01-26 19:32:16 UTC (rev 11098)
@@ -99,6 +99,7 @@
 
 #include <cassert>
 #include <string>
+#include <utility>
 #include <OgreVector2.h>
 #include <OgreVector3.h>
 #include <OgreVector4.h>
@@ -309,6 +310,8 @@
             inline MultiType(const V& value)         : value_(nullptr) { this->set(value); }
             /// Copyconstructor: Assigns value and type of the other MultiType.
             inline MultiType(const MultiType& other) : value_(nullptr) { this->set(other); }
+            /// Moveconstructor: Moves the value and its type from the other MultiType
+            inline MultiType(MultiType&& other)      : value_(nullptr) { std::swap(this->value_, other.value_); };
 
             /// Destructor: Deletes the MT_Value.
             inline ~MultiType() { if (this->value_) { delete this->value_; } }
@@ -319,6 +322,8 @@
             template <typename V> inline MultiType& operator=(V* value)               { this->set(value); return (*this); }
             /// Assigns the value of the other MultiType and converts it to the current type of the MultiType.
             inline                       MultiType& operator=(const MultiType& other) { this->set(other); return (*this); }
+            /// Moves the value and the type of the other MultiType to this one.
+            inline                       MultiType& operator=(MultiType&& other)      { std::swap(this->value_, other.value_); return (*this); }
 
             /// Assigns the given value and converts it to the current type.
             template <typename V> inline bool set(const V& value)

Modified: code/trunk/src/libraries/util/Sleep.cc
===================================================================
--- code/trunk/src/libraries/util/Sleep.cc	2016-01-25 20:47:54 UTC (rev 11097)
+++ code/trunk/src/libraries/util/Sleep.cc	2016-01-26 19:32:16 UTC (rev 11098)
@@ -29,57 +29,28 @@
 /**
 @file
 @brief
-    Implementation of three sleep functions. Avoids including windows.h
+    Implementation of three sleep functions.
 */
 
 #include "Sleep.h"
-#include "Output.h"
 
-#ifdef ORXONOX_PLATFORM_WINDOWS
-#ifndef WIN32_LEAN_AND_MEAN
-#  define WIN32_LEAN_AND_MEAN
-#endif
-#include <windows.h>
-#undef min
-#undef max
+#include <chrono>
+#include <thread>
 
 namespace orxonox
 {
     void usleep(unsigned long microseconds)
     {
-        //if (microseconds < 1000)
-        //    orxout(internal_warning) << "Windows cannot sleep less than 1ms, ignoring" << endl;
-        Sleep(microseconds / 1000);
+        std::this_thread::sleep_for(std::chrono::microseconds(microseconds));
     }
 
     void msleep(unsigned long milliseconds)
     {
-        Sleep(milliseconds);
+        std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds));
     }
 
     void sleep(unsigned long seconds)
     {
-        Sleep(seconds * 1000);
+        std::this_thread::sleep_for(std::chrono::seconds(seconds));
     }
 }
-
-#else /* Linux/Apple */
-#include <unistd.h>
-
-namespace orxonox
-{
-    void usleep(unsigned long usec)
-    {
-        ::usleep(usec);
-    }
-    void msleep(unsigned long msec)
-    {
-        ::usleep(msec * 1000);
-    }
-    void sleep(unsigned long sec)
-    {
-        ::usleep(sec * 1000000);
-    }
-}
-
-#endif

Modified: code/trunk/src/libraries/util/SubString.h
===================================================================
--- code/trunk/src/libraries/util/SubString.h	2016-01-25 20:47:54 UTC (rev 11097)
+++ code/trunk/src/libraries/util/SubString.h	2016-01-26 19:32:16 UTC (rev 11098)
@@ -114,6 +114,10 @@
 
     public:
         SubString();
+        /// Copy constructor
+        SubString(const SubString& other) = default;
+        /// Move constructor
+        SubString(SubString&& other) = default;
         SubString(const std::string& line,
                   const std::string& delimiters = SubString::WhiteSpaces,
                   const std::string& delimiterNeighbours = "",
@@ -132,6 +136,8 @@
 
         // operate on the SubString
         SubString& operator=(const SubString& other);
+        /// Move assignment
+        SubString& operator=(SubString&& other) = default;
         bool operator==(const SubString& other) const;
         bool compare(const SubString& other, size_t length = std::string::npos) const;
         SubString operator+(const SubString& other) const;




More information about the Orxonox-commit mailing list