00001 /* 00002 Aseba - an event-based framework for distributed robot control 00003 Copyright (C) 2007--2012: 00004 Stephane Magnenat <stephane at magnenat dot net> 00005 (http://stephane.magnenat.net) 00006 and other contributors, see authors.txt for details 00007 00008 This program is free software: you can redistribute it and/or modify 00009 it under the terms of the GNU Lesser General Public License as published 00010 by the Free Software Foundation, version 3 of the License. 00011 00012 This program is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU Lesser General Public License for more details. 00016 00017 You should have received a copy of the GNU Lesser General Public License 00018 along with this program. If not, see <http://www.gnu.org/licenses/>. 00019 */ 00020 00021 #ifndef ASEBA_UTILS_H 00022 #define ASEBA_UTILS_H 00023 00024 #include <iostream> 00025 #include <string> 00026 #include <cassert> 00027 #include <cstdlib> 00028 #include "../common/types.h" 00029 00030 namespace Aseba 00031 { 00036 00038 template<typename Derived, typename Base> 00039 static inline Derived polymorphic_downcast(Base base) 00040 { 00041 Derived derived = dynamic_cast<Derived>(base); 00042 if (!derived) 00043 abort(); 00044 return derived; 00045 } 00046 00048 template<typename Derived, typename Base> 00049 static inline Derived polymorphic_downcast_or_null(Base base) 00050 { 00051 if (!base) 00052 return 0; 00053 Derived derived = dynamic_cast<Derived>(base); 00054 if (!derived) 00055 abort(); 00056 return derived; 00057 } 00058 00060 struct UnifiedTime 00061 { 00063 typedef long long unsigned Value; 00065 Value value; 00066 00068 UnifiedTime(); 00070 UnifiedTime(Value ms); 00072 UnifiedTime(Value seconds, Value milliseconds); 00073 00075 void operator +=(const UnifiedTime &that) { value += that.value; } 00077 void operator -=(const UnifiedTime &that) { value -= that.value; } 00079 void operator /=(const long long unsigned factor) { assert(factor); value /= factor; } 00081 void operator *=(const long long unsigned factor) { value *= factor; } 00083 UnifiedTime operator +(const UnifiedTime &that) const { return UnifiedTime(value + that.value); } 00085 UnifiedTime operator -(const UnifiedTime &that) const { return UnifiedTime(value - that.value); } 00087 UnifiedTime operator /(const long long unsigned factor) const { assert(factor); return UnifiedTime(value / factor); } 00089 UnifiedTime operator *(const long long unsigned factor) const { return UnifiedTime(value * factor); } 00091 bool operator <(const UnifiedTime &that) const { return value < that.value; } 00092 00094 void sleep() const; 00095 00097 std::string toHumanReadableStringFromEpoch() const; 00098 00100 std::string toRawTimeString() const; 00101 00103 static UnifiedTime fromRawTimeString(const std::string& rawTimeString); 00104 }; 00105 00107 void dumpTime(std::ostream &stream, bool raw = false); 00108 00110 std::string WStringToUTF8(const std::wstring& s); 00111 00113 std::wstring UTF8ToWString(const std::string& s); 00114 00116 uint16 crcXModem(const uint16 oldCrc, const std::wstring& s); 00117 00119 uint16 crcXModem(const uint16 oldCrc, const uint16 v); 00120 00123 }; 00124 00125 #endif