Skip to content

Commit 6cf7f1a

Browse files
authored
[CHORE] Util classes format and simple cleanup with generated resources (mod-playerbots#2028)
- Did some basic formatting - Some generated docs - Cleaned header/impl Helper.css - Moved PerfMonitor from util to bot/handler/command Still a freaking mess though, but its a start i guess. Cant ask ppl to add more or make use of those when its so messy.
1 parent 9f54d7e commit 6cf7f1a

7 files changed

Lines changed: 254 additions & 51 deletions

File tree

src/Util/Helpers.cpp

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@
55

66
#include "Helpers.h"
77

8+
#include <algorithm>
9+
#include <cctype>
10+
#include <cstdlib>
11+
#include <cstring>
12+
#include <sstream>
13+
#include <string>
14+
#include <vector>
15+
16+
/**
17+
* Case-insensitive substring search.
18+
*/
819
char* strstri(char const* haystack, char const* needle)
920
{
1021
if (!*needle)
@@ -16,7 +27,9 @@ char* strstri(char const* haystack, char const* needle)
1627
{
1728
if (tolower(*haystack) == tolower(*needle))
1829
{
19-
char const *h = haystack, *n = needle;
30+
char const* h = haystack;
31+
char const* n = needle;
32+
2033
for (; *h && *n; ++h, ++n)
2134
{
2235
if (tolower(*h) != tolower(*n))
@@ -35,16 +48,67 @@ char* strstri(char const* haystack, char const* needle)
3548
return 0;
3649
}
3750

51+
/**
52+
* Trim whitespace from the left side of a string (in place).
53+
*/
3854
std::string& ltrim(std::string& s)
3955
{
4056
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int c) { return !std::isspace(c); }));
4157
return s;
4258
}
4359

60+
/**
61+
* Trim whitespace from the right side of a string (in place).
62+
*/
4463
std::string& rtrim(std::string& s)
4564
{
4665
s.erase(std::find_if(s.rbegin(), s.rend(), [](int c) { return !std::isspace(c); }).base(), s.end());
4766
return s;
4867
}
4968

69+
/**
70+
* Trim whitespace from both ends of a string (in place).
71+
*/
5072
std::string& trim(std::string& s) { return ltrim(rtrim(s)); }
73+
74+
/**
75+
* Split a string using a C-string delimiter.
76+
*/
77+
void split(std::vector<std::string>& dest, std::string const str, char const* delim)
78+
{
79+
char* pTempStr = strdup(str.c_str());
80+
char* pWord = strtok(pTempStr, delim);
81+
82+
while (pWord != nullptr)
83+
{
84+
dest.push_back(pWord);
85+
pWord = strtok(nullptr, delim);
86+
}
87+
88+
free(pTempStr);
89+
}
90+
91+
/**
92+
* Split a string using a single character delimiter.
93+
*/
94+
std::vector<std::string>& split(std::string const s, char delim, std::vector<std::string>& elems)
95+
{
96+
std::stringstream ss(s);
97+
std::string item;
98+
99+
while (getline(ss, item, delim))
100+
{
101+
elems.push_back(item);
102+
}
103+
104+
return elems;
105+
}
106+
107+
/**
108+
* Split a string using a single character delimiter.
109+
*/
110+
std::vector<std::string> split(std::string const s, char delim)
111+
{
112+
std::vector<std::string> elems;
113+
return split(s, delim, elems);
114+
}

src/Util/Helpers.h

Lines changed: 55 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,50 +6,68 @@
66
#ifndef _PLAYERBOT_HELPERS_H
77
#define _PLAYERBOT_HELPERS_H
88

9-
#include <stdio.h>
10-
#include <string.h>
11-
12-
#include <algorithm>
13-
#include <cctype>
14-
#include <functional>
15-
#include <locale>
16-
#include <map>
17-
#include <sstream>
9+
#include <string>
1810
#include <vector>
1911

20-
#include "Common.h"
21-
22-
void split(std::vector<std::string>& dest, std::string const str, char const* delim)
23-
{
24-
char* pTempStr = strdup(str.c_str());
25-
char* pWord = strtok(pTempStr, delim);
12+
/**
13+
* Case-insensitive substring search.
14+
*
15+
* @param haystack The string to search in
16+
* @param needle The substring to search for
17+
* @return Pointer to the first matching position in haystack, or nullptr if not found.
18+
*/
19+
char* strstri(char const* haystack, char const* needle);
2620

27-
while (pWord != nullptr)
28-
{
29-
dest.push_back(pWord);
30-
pWord = strtok(nullptr, delim);
31-
}
21+
/**
22+
* Trim whitespace from the left side of a string (in place).
23+
*
24+
* @param s The string to trim
25+
* @return Reference to the modified string
26+
*/
27+
std::string& ltrim(std::string& s);
3228

33-
free(pTempStr);
34-
}
29+
/**
30+
* Trim whitespace from the right side of a string (in place).
31+
*
32+
* @param s The string to trim
33+
* @return Reference to the modified string
34+
*/
35+
std::string& rtrim(std::string& s);
3536

36-
std::vector<std::string>& split(std::string const s, char delim, std::vector<std::string>& elems)
37-
{
38-
std::stringstream ss(s);
39-
std::string item;
37+
/**
38+
* Trim whitespace from both ends of a string (in place).
39+
*
40+
* @param s The string to trim
41+
* @return Reference to the modified string
42+
*/
43+
std::string& trim(std::string& s);
4044

41-
while (getline(ss, item, delim))
42-
{
43-
elems.push_back(item);
44-
}
45+
/**
46+
* Split a string using a C-string delimiter.
47+
*
48+
* @param dest Vector to store split tokens
49+
* @param str String to split
50+
* @param delim C-string delimiter
51+
*/
52+
void split(std::vector<std::string>& dest, std::string const str, char const* delim);
4553

46-
return elems;
47-
}
54+
/**
55+
* Split a string using a single character delimiter.
56+
*
57+
* @param s String to split
58+
* @param delim Delimiter character
59+
* @param elems Vector to store split tokens
60+
* @return Reference to the vector containing tokens
61+
*/
62+
std::vector<std::string>& split(std::string const s, char delim, std::vector<std::string>& elems);
4863

49-
std::vector<std::string> split(std::string const s, char delim)
50-
{
51-
std::vector<std::string> elems;
52-
return split(s, delim, elems);
53-
}
64+
/**
65+
* Split a string using a single character delimiter.
66+
*
67+
* @param s String to split
68+
* @param delim Delimiter character
69+
* @return Vector containing split tokens
70+
*/
71+
std::vector<std::string> split(std::string const s, char delim);
5472

5573
#endif

src/Util/LazyCalculatedValue.h

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,45 @@
66
#ifndef _PLAYERBOT_LAZYCALCULATEDVALUE_H
77
#define _PLAYERBOT_LAZYCALCULATEDVALUE_H
88

9+
/**
10+
* @brief Lazy calculation helper.
11+
*
12+
* Stores a function pointer (calculator) and its owner instance, and
13+
* calculates the value only when it is requested for the first time.
14+
* The result is cached until Reset() is called.
15+
*
16+
* @tparam TValue Type of the calculated value.
17+
* @tparam TOwner Type of the owner class containing the calculator function.
18+
*/
919
template <class TValue, class TOwner>
1020
class LazyCalculatedValue
1121
{
1222
public:
23+
/**
24+
* @brief Type of the calculator function.
25+
*
26+
* This is a pointer to a member function of TOwner returning TValue.
27+
*/
1328
typedef TValue (TOwner::*Calculator)();
1429

1530
public:
31+
/**
32+
* @brief Constructor.
33+
*
34+
* @param owner Pointer to the owner object.
35+
* @param calculator Pointer to the member function used to calculate the value.
36+
*/
1637
LazyCalculatedValue(TOwner* owner, Calculator calculator) : calculator(calculator), owner(owner) { Reset(); }
1738

1839
public:
40+
/**
41+
* @brief Get the cached value or calculate it if needed.
42+
*
43+
* If the value has not been calculated yet, it calls the calculator
44+
* on the owner and caches the result.
45+
*
46+
* @return TValue The calculated or cached value.
47+
*/
1948
TValue GetValue()
2049
{
2150
if (!calculated)
@@ -27,13 +56,19 @@ class LazyCalculatedValue
2756
return value;
2857
}
2958

59+
/**
60+
* @brief Reset the cached state.
61+
*
62+
* After calling Reset(), the next call to GetValue() will recalculate
63+
* the value again.
64+
*/
3065
void Reset() { calculated = false; }
3166

3267
protected:
33-
Calculator calculator;
34-
TOwner* owner;
35-
bool calculated;
36-
TValue value;
68+
Calculator calculator; ///< Pointer to calculator member function
69+
TOwner* owner; ///< Owner instance
70+
bool calculated; ///< Whether value has already been calculated
71+
TValue value; ///< Cached value
3772
};
3873

3974
#endif

src/Util/ServerFacade.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,13 @@ void ServerFacade::SetFacingTo(Player* bot, WorldObject* wo, bool force)
4545
return;
4646

4747
float angle = bot->GetAngle(wo);
48+
4849
// if (!force && bot->isMoving())
4950
// bot->SetFacingTo(bot->GetAngle(wo));
5051
// else
5152
// {
5253
bot->SetOrientation(angle);
54+
5355
if (!bot->IsRooted())
5456
bot->SendMovementFlagUpdate();
5557
// }
@@ -64,16 +66,14 @@ Unit* ServerFacade::GetChaseTarget(Unit* target)
6466
{
6567
return static_cast<ChaseMovementGenerator<Player> const*>(movementGen)->GetTarget();
6668
}
67-
else
68-
{
69-
return static_cast<ChaseMovementGenerator<Creature> const*>(movementGen)->GetTarget();
70-
}
69+
70+
return static_cast<ChaseMovementGenerator<Creature> const*>(movementGen)->GetTarget();
7171
}
7272

7373
return nullptr;
7474
}
7575

76-
void ServerFacade::SendPacket(Player *player, WorldPacket *packet)
76+
void ServerFacade::SendPacket(Player* player, WorldPacket* packet)
7777
{
78-
return player->GetSession()->SendPacket(packet);
78+
player->GetSession()->SendPacket(packet);
7979
}

0 commit comments

Comments
 (0)