aboutsummaryrefslogtreecommitdiff
path: root/src/common/Utilities
diff options
context:
space:
mode:
authorShauren <shauren.trinity@gmail.com>2017-05-11 22:29:51 +0200
committerShauren <shauren.trinity@gmail.com>2017-05-11 22:29:51 +0200
commit3a418a0bbc8e155e5395595c5e25c038d3c7c773 (patch)
tree45359566bd6e360dd133e7ef43bc2a42e26baf56 /src/common/Utilities
parent8abc56c540b5d2c583e40ad3d302c43068778ed1 (diff)
Core/Common: Include cleanup
Diffstat (limited to 'src/common/Utilities')
-rw-r--r--src/common/Utilities/AsioHacksFwd.h48
-rw-r--r--src/common/Utilities/AsioHacksImpl.h32
-rw-r--r--src/common/Utilities/EventMap.cpp16
-rw-r--r--src/common/Utilities/EventMap.h19
-rw-r--r--src/common/Utilities/EventProcessor.cpp2
-rw-r--r--src/common/Utilities/EventProcessor.h4
-rw-r--r--src/common/Utilities/Hash.h51
-rw-r--r--src/common/Utilities/Optional.h28
-rw-r--r--src/common/Utilities/Random.cpp5
-rw-r--r--src/common/Utilities/Random.h1
-rw-r--r--src/common/Utilities/StartProcess.cpp27
-rw-r--r--src/common/Utilities/StartProcess.h8
-rw-r--r--src/common/Utilities/TaskScheduler.cpp1
-rw-r--r--src/common/Utilities/TaskScheduler.h12
-rw-r--r--src/common/Utilities/Timer.h1
-rw-r--r--src/common/Utilities/Util.cpp42
-rw-r--r--src/common/Utilities/Util.h27
17 files changed, 233 insertions, 91 deletions
diff --git a/src/common/Utilities/AsioHacksFwd.h b/src/common/Utilities/AsioHacksFwd.h
new file mode 100644
index 00000000000..6d4c9f5a3ed
--- /dev/null
+++ b/src/common/Utilities/AsioHacksFwd.h
@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) 2008-2017 TrinityCore <http://www.trinitycore.org/>
+ *
+ * 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef AsioHacksFwd_h__
+#define AsioHacksFwd_h__
+
+namespace boost
+{
+ namespace posix_time
+ {
+ class ptime;
+ }
+
+ namespace asio
+ {
+ template <typename Time>
+ struct time_traits;
+
+ template <typename TimeType, typename TimeTraits>
+ class deadline_timer_service;
+
+ template <typename Time, typename TimeTraits, typename TimerService>
+ class basic_deadline_timer;
+
+ typedef basic_deadline_timer<posix_time::ptime, time_traits<posix_time::ptime>, deadline_timer_service<posix_time::ptime, time_traits<posix_time::ptime>>> deadline_timer;
+ }
+}
+
+namespace Trinity
+{
+ class AsioStrand;
+}
+
+#endif // AsioHacksFwd_h__
diff --git a/src/common/Utilities/AsioHacksImpl.h b/src/common/Utilities/AsioHacksImpl.h
new file mode 100644
index 00000000000..86888927a72
--- /dev/null
+++ b/src/common/Utilities/AsioHacksImpl.h
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2008-2017 TrinityCore <http://www.trinitycore.org/>
+ *
+ * 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef AsioHacksImpl_h__
+#define AsioHacksImpl_h__
+
+#include <boost/asio/strand.hpp>
+
+namespace Trinity
+{
+ class AsioStrand : public boost::asio::io_service::strand
+ {
+ public:
+ AsioStrand(boost::asio::io_service& io_service) : boost::asio::io_service::strand(io_service) { }
+ };
+}
+
+#endif // AsioHacksImpl_h__
diff --git a/src/common/Utilities/EventMap.cpp b/src/common/Utilities/EventMap.cpp
index d4ea70174c4..e1c46d0ae65 100644
--- a/src/common/Utilities/EventMap.cpp
+++ b/src/common/Utilities/EventMap.cpp
@@ -16,6 +16,7 @@
*/
#include "EventMap.h"
+#include "Random.h"
void EventMap::Reset()
{
@@ -32,6 +33,11 @@ void EventMap::SetPhase(uint8 phase)
_phase = uint8(1 << (phase - 1));
}
+void EventMap::ScheduleEvent(uint32 eventId, Milliseconds const& minTime, Milliseconds const& maxTime, uint32 group /*= 0*/, uint32 phase /*= 0*/)
+{
+ ScheduleEvent(eventId, urand(uint32(minTime.count()), uint32(maxTime.count())), group, phase);
+}
+
void EventMap::ScheduleEvent(uint32 eventId, uint32 time, uint32 group /*= 0*/, uint8 phase /*= 0*/)
{
if (group && group <= 8)
@@ -43,6 +49,16 @@ void EventMap::ScheduleEvent(uint32 eventId, uint32 time, uint32 group /*= 0*/,
_eventMap.insert(EventStore::value_type(_time + time, eventId));
}
+void EventMap::RescheduleEvent(uint32 eventId, Milliseconds const& minTime, Milliseconds const& maxTime, uint32 group /*= 0*/, uint32 phase /*= 0*/)
+{
+ RescheduleEvent(eventId, urand(uint32(minTime.count()), uint32(maxTime.count())), group, phase);
+}
+
+void EventMap::Repeat(uint32 minTime, uint32 maxTime)
+{
+ Repeat(urand(minTime, maxTime));
+}
+
uint32 EventMap::ExecuteEvent()
{
while (!Empty())
diff --git a/src/common/Utilities/EventMap.h b/src/common/Utilities/EventMap.h
index edcc7a64455..22899ea6b60 100644
--- a/src/common/Utilities/EventMap.h
+++ b/src/common/Utilities/EventMap.h
@@ -18,9 +18,9 @@
#ifndef _EVENT_MAP_H_
#define _EVENT_MAP_H_
-#include "Common.h"
+#include "Define.h"
#include "Duration.h"
-#include "Util.h"
+#include <map>
class TC_COMMON_API EventMap
{
@@ -134,10 +134,7 @@ public:
* @param group The group which the event is associated to. Has to be between 1 and 8. 0 means it has no group.
* @param phase The phase in which the event can occur. Has to be between 1 and 8. 0 means it can occur in all phases.
*/
- void ScheduleEvent(uint32 eventId, Milliseconds const& minTime, Milliseconds const& maxTime, uint32 group = 0, uint32 phase = 0)
- {
- ScheduleEvent(eventId, urand(uint32(minTime.count()), uint32(maxTime.count())), group, phase);
- }
+ void ScheduleEvent(uint32 eventId, Milliseconds const& minTime, Milliseconds const& maxTime, uint32 group = 0, uint32 phase = 0);
/**
* @name ScheduleEvent
@@ -171,10 +168,7 @@ public:
* @param group The group which the event is associated to. Has to be between 1 and 8. 0 means it has no group.
* @param phase The phase in which the event can occur. Has to be between 1 and 8. 0 means it can occur in all phases.
*/
- void RescheduleEvent(uint32 eventId, Milliseconds const& minTime, Milliseconds const& maxTime, uint32 group = 0, uint32 phase = 0)
- {
- RescheduleEvent(eventId, urand(uint32(minTime.count()), uint32(maxTime.count())), group, phase);
- }
+ void RescheduleEvent(uint32 eventId, Milliseconds const& minTime, Milliseconds const& maxTime, uint32 group = 0, uint32 phase = 0);
/**
* @name RescheduleEvent
@@ -227,10 +221,7 @@ public:
* @param minTime Minimum time until the event occurs.
* @param maxTime Maximum time until the event occurs.
*/
- void Repeat(uint32 minTime, uint32 maxTime)
- {
- Repeat(urand(minTime, maxTime));
- }
+ void Repeat(uint32 minTime, uint32 maxTime);
/**
* @name ExecuteEvent
diff --git a/src/common/Utilities/EventProcessor.cpp b/src/common/Utilities/EventProcessor.cpp
index 07fce187ad3..76517abf007 100644
--- a/src/common/Utilities/EventProcessor.cpp
+++ b/src/common/Utilities/EventProcessor.cpp
@@ -44,7 +44,7 @@ void EventProcessor::Update(uint32 p_time)
m_time += p_time;
// main event loop
- EventList::iterator i;
+ std::multimap<uint64, BasicEvent*>::iterator i;
while (((i = m_events.begin()) != m_events.end()) && i->first <= m_time)
{
// get and remove event from queue
diff --git a/src/common/Utilities/EventProcessor.h b/src/common/Utilities/EventProcessor.h
index 3b924dbb3e5..081c9e82884 100644
--- a/src/common/Utilities/EventProcessor.h
+++ b/src/common/Utilities/EventProcessor.h
@@ -68,8 +68,6 @@ class TC_COMMON_API BasicEvent
uint64 m_execTime; // planned time of next execution, filled by event handler
};
-typedef std::multimap<uint64, BasicEvent*> EventList;
-
class TC_COMMON_API EventProcessor
{
public:
@@ -83,7 +81,7 @@ class TC_COMMON_API EventProcessor
protected:
uint64 m_time;
- EventList m_events;
+ std::multimap<uint64, BasicEvent*> m_events;
};
#endif
diff --git a/src/common/Utilities/Hash.h b/src/common/Utilities/Hash.h
new file mode 100644
index 00000000000..fe2047f7024
--- /dev/null
+++ b/src/common/Utilities/Hash.h
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2008-2017 TrinityCore <http://www.trinitycore.org/>
+ *
+ * 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef TrinityCore_Hash_h__
+#define TrinityCore_Hash_h__
+
+#include <functional>
+#include <utility>
+
+namespace Trinity
+{
+ template<typename T>
+ inline void hash_combine(std::size_t& seed, T const& val)
+ {
+ seed ^= std::hash<T>()(val) + 0x9E3779B9 + (seed << 6) + (seed >> 2);
+ }
+}
+
+ //! Hash implementation for std::pair to allow using pairs in unordered_set or as key for unordered_map
+ //! Individual types used in pair must be hashable by std::hash
+namespace std
+{
+ template<class K, class V>
+ struct hash<std::pair<K, V>>
+ {
+ public:
+ size_t operator()(std::pair<K, V> const& p) const
+ {
+ size_t hashVal = 0;
+ Trinity::hash_combine(hashVal, p.first);
+ Trinity::hash_combine(hashVal, p.second);
+ return hashVal;
+ }
+ };
+}
+
+#endif // TrinityCore_Hash_h__
diff --git a/src/common/Utilities/Optional.h b/src/common/Utilities/Optional.h
new file mode 100644
index 00000000000..c13a3a6a298
--- /dev/null
+++ b/src/common/Utilities/Optional.h
@@ -0,0 +1,28 @@
+/*
+ * Copyright (C) 2008-2017 TrinityCore <http://www.trinitycore.org/>
+ *
+ * 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef TrinityCore_Optional_h__
+#define TrinityCore_Optional_h__
+
+#include <boost/optional.hpp>
+#include <boost/utility/in_place_factory.hpp>
+
+ //! Optional helper class to wrap optional values within.
+template <typename T>
+using Optional = boost::optional<T>;
+
+#endif // TrinityCore_Optional_h__
diff --git a/src/common/Utilities/Random.cpp b/src/common/Utilities/Random.cpp
index 3a0cdedcf4b..890ba1b8fce 100644
--- a/src/common/Utilities/Random.cpp
+++ b/src/common/Utilities/Random.cpp
@@ -16,7 +16,6 @@
*/
#include "Random.h"
-#include "Common.h"
#include "Errors.h"
#include "SFMT.h"
#include <boost/thread/tss.hpp>
@@ -53,8 +52,8 @@ uint32 urand(uint32 min, uint32 max)
uint32 urandms(uint32 min, uint32 max)
{
ASSERT(max >= min);
- ASSERT(INT_MAX / IN_MILLISECONDS >= max);
- return GetRng()->URandom(min * IN_MILLISECONDS, max * IN_MILLISECONDS);
+ ASSERT(std::numeric_limits<uint32>::max() / Milliseconds::period::den >= max);
+ return GetRng()->URandom(min * Milliseconds::period::den, max * Milliseconds::period::den);
}
float frand(float min, float max)
diff --git a/src/common/Utilities/Random.h b/src/common/Utilities/Random.h
index ded0a6c6e5c..d200282093a 100644
--- a/src/common/Utilities/Random.h
+++ b/src/common/Utilities/Random.h
@@ -21,7 +21,6 @@
#include "Define.h"
#include "Duration.h"
#include <limits>
-#include <random>
/* Return a random number in the range min..max. */
TC_COMMON_API int32 irand(int32 min, int32 max);
diff --git a/src/common/Utilities/StartProcess.cpp b/src/common/Utilities/StartProcess.cpp
index 6f4676232ca..32182f67761 100644
--- a/src/common/Utilities/StartProcess.cpp
+++ b/src/common/Utilities/StartProcess.cpp
@@ -16,27 +16,20 @@
*/
#include "StartProcess.h"
-
-#include <atomic>
-#include <thread>
-#include <functional>
+#include "Errors.h"
+#include "Log.h"
+#include "Optional.h"
#include <boost/algorithm/string/join.hpp>
-#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/copy.hpp>
-#include <boost/iostreams/concepts.hpp>
-#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/process.hpp>
-#include <boost/system/system_error.hpp>
-
-#include "Common.h"
-#include "Log.h"
using namespace boost::process;
using namespace boost::process::initializers;
using namespace boost::iostreams;
-namespace Trinity {
+namespace Trinity
+{
template<typename T>
class TCLogSink
@@ -250,19 +243,15 @@ std::shared_ptr<AsyncProcessResult>
return handle;
}
-Optional<std::string> SearchExecutableInPath(std::string const& filename)
+std::string SearchExecutableInPath(std::string const& filename)
{
try
{
- auto result = search_path(filename);
- if (result.empty())
- return boost::none;
- else
- return result;
+ return search_path(filename);
}
catch (...)
{
- return boost::none;
+ return "";
}
}
diff --git a/src/common/Utilities/StartProcess.h b/src/common/Utilities/StartProcess.h
index 120c4f26ea6..249d4ebd47e 100644
--- a/src/common/Utilities/StartProcess.h
+++ b/src/common/Utilities/StartProcess.h
@@ -18,11 +18,13 @@
#ifndef Process_h__
#define Process_h__
+#include "Define.h"
#include <future>
#include <memory>
-#include "Common.h"
+#include <vector>
-namespace Trinity {
+namespace Trinity
+{
/// Starts a process with the given arguments and parameters and will block
/// until the process is finished.
@@ -60,7 +62,7 @@ TC_COMMON_API std::shared_ptr<AsyncProcessResult>
/// Searches for the given executable in the PATH variable
/// and returns a present optional when it was found.
-TC_COMMON_API Optional<std::string> SearchExecutableInPath(std::string const& filename);
+TC_COMMON_API std::string SearchExecutableInPath(std::string const& filename);
} // namespace Trinity
diff --git a/src/common/Utilities/TaskScheduler.cpp b/src/common/Utilities/TaskScheduler.cpp
index 730ef8d4b89..a9dcf5dc09b 100644
--- a/src/common/Utilities/TaskScheduler.cpp
+++ b/src/common/Utilities/TaskScheduler.cpp
@@ -16,6 +16,7 @@
*/
#include "TaskScheduler.h"
+#include "Errors.h"
TaskScheduler& TaskScheduler::ClearValidator()
{
diff --git a/src/common/Utilities/TaskScheduler.h b/src/common/Utilities/TaskScheduler.h
index d6c399639bc..0213fb90f9b 100644
--- a/src/common/Utilities/TaskScheduler.h
+++ b/src/common/Utilities/TaskScheduler.h
@@ -18,6 +18,9 @@
#ifndef _TASK_SCHEDULER_H_
#define _TASK_SCHEDULER_H_
+#include "Duration.h"
+#include "Optional.h"
+#include "Random.h"
#include <algorithm>
#include <chrono>
#include <vector>
@@ -26,11 +29,6 @@
#include <utility>
#include <set>
-#include <boost/optional.hpp>
-
-#include "Util.h"
-#include "Duration.h"
-
class TaskContext;
/// The TaskScheduler class provides the ability to schedule std::function's in the near future.
@@ -73,13 +71,13 @@ class TC_COMMON_API TaskScheduler
timepoint_t _end;
duration_t _duration;
- boost::optional<group_t> _group;
+ Optional<group_t> _group;
repeated_t _repeated;
task_handler_t _task;
public:
// All Argument construct
- Task(timepoint_t const& end, duration_t const& duration, boost::optional<group_t> const& group,
+ Task(timepoint_t const& end, duration_t const& duration, Optional<group_t> const& group,
repeated_t const repeated, task_handler_t const& task)
: _end(end), _duration(duration), _group(group), _repeated(repeated), _task(task) { }
diff --git a/src/common/Utilities/Timer.h b/src/common/Utilities/Timer.h
index e774808bcb0..30a8873102c 100644
--- a/src/common/Utilities/Timer.h
+++ b/src/common/Utilities/Timer.h
@@ -19,6 +19,7 @@
#ifndef TRINITY_TIMER_H
#define TRINITY_TIMER_H
+#include "Define.h"
#include <chrono>
inline uint32 getMSTime()
diff --git a/src/common/Utilities/Util.cpp b/src/common/Utilities/Util.cpp
index ae08d3ba5a6..05dfee273d8 100644
--- a/src/common/Utilities/Util.cpp
+++ b/src/common/Utilities/Util.cpp
@@ -18,11 +18,12 @@
#include "Util.h"
#include "Common.h"
-#include "CompilerDefs.h"
-#include "utf8.h"
-#include "Errors.h" // for ASSERT
-#include <stdarg.h>
-#include <boost/algorithm/string/case_conv.hpp>
+#include <boost/asio/ip/address.hpp>
+#include <utf8.h>
+#include <algorithm>
+#include <sstream>
+#include <cstdarg>
+#include <ctime>
#if TRINITY_COMPILER == TRINITY_COMPILER_GNU
#include <sys/socket.h>
@@ -149,7 +150,7 @@ int64 MoneyStringToMoney(const std::string& moneyString)
if (gCount + sCount + cCount != 1)
return 0;
- uint64 amount = atoull(*itr);
+ uint64 amount = strtoull(*itr, nullptr, 10);
if (gCount == 1)
money += amount * 100 * 100;
else if (sCount == 1)
@@ -171,8 +172,8 @@ uint32 TimeStringToSecs(const std::string& timestring)
{
if (isdigit(*itr))
{
- buffer*=10;
- buffer+= (*itr)-'0';
+ buffer *= 10;
+ buffer += *itr - '0';
}
else
{
@@ -184,9 +185,9 @@ uint32 TimeStringToSecs(const std::string& timestring)
case 's': multiplier = 1; break;
default : return 0; //bad format
}
- buffer*=multiplier;
- secs+=buffer;
- buffer=0;
+ buffer *= multiplier;
+ secs += buffer;
+ buffer = 0;
}
}
@@ -214,9 +215,9 @@ bool IsIPAddress(char const* ipaddress)
if (!ipaddress)
return false;
- // Let the big boys do it.
- // Drawback: all valid ip address formats are recognized e.g.: 12.23, 121234, 0xABCD)
- return inet_addr(ipaddress) != INADDR_NONE;
+ boost::system::error_code error;
+ boost::asio::ip::address::from_string(ipaddress, error);
+ return !error;
}
/// create PID file
@@ -374,6 +375,16 @@ bool WStrToUtf8(std::wstring const& wstr, std::string& utf8str)
typedef wchar_t const* const* wstrlist;
+void wstrToUpper(std::wstring& str)
+{
+ std::transform(str.begin(), str.end(), str.begin(), wcharToUpper);
+}
+
+void wstrToLower(std::wstring& str)
+{
+ std::transform(str.begin(), str.end(), str.begin(), wcharToLower);
+}
+
std::wstring GetMainPartOfName(std::wstring const& wname, uint32 declension)
{
// supported only Cyrillic cases
@@ -558,6 +569,7 @@ void HexStrToByteArray(std::string const& str, uint8* out, bool reverse /*= fals
bool StringToBool(std::string const& str)
{
- std::string lowerStr = boost::algorithm::to_lower_copy(str);
+ std::string lowerStr = str;
+ std::transform(str.begin(), str.end(), lowerStr.begin(), ::tolower);
return lowerStr == "1" || lowerStr == "true" || lowerStr == "yes";
}
diff --git a/src/common/Utilities/Util.h b/src/common/Utilities/Util.h
index c7a8289cb74..cd17946a40b 100644
--- a/src/common/Utilities/Util.h
+++ b/src/common/Utilities/Util.h
@@ -21,25 +21,9 @@
#include "Define.h"
#include "Errors.h"
-#include "Random.h"
-
-#include <algorithm>
#include <string>
#include <sstream>
#include <vector>
-#include <list>
-#include <map>
-#include <ctime>
-
-// Searcher for map of structs
-template<typename T, class S> struct Finder
-{
- T val_;
- T S::* idMember_;
-
- Finder(T val, T S::* idMember) : val_(val), idMember_(idMember) {}
- bool operator()(const std::pair<int, S> &obj) { return obj.second.*idMember_ == val_; }
-};
class TC_COMMON_API Tokenizer
{
@@ -297,15 +281,8 @@ inline wchar_t wcharToLower(wchar_t wchar)
return wchar;
}
-inline void wstrToUpper(std::wstring& str)
-{
- std::transform( str.begin(), str.end(), str.begin(), wcharToUpper );
-}
-
-inline void wstrToLower(std::wstring& str)
-{
- std::transform( str.begin(), str.end(), str.begin(), wcharToLower );
-}
+TC_COMMON_API void wstrToUpper(std::wstring& str);
+TC_COMMON_API void wstrToLower(std::wstring& str);
TC_COMMON_API std::wstring GetMainPartOfName(std::wstring const& wname, uint32 declension);