Skip to content

Commit

Permalink
[cleanup] Replace some const std::string& by std::string_view or …
Browse files Browse the repository at this point in the history
…`std::string&&`
  • Loading branch information
Jarod42 committed May 29, 2024
1 parent ae28f66 commit a1be6f4
Show file tree
Hide file tree
Showing 53 changed files with 168 additions and 145 deletions.
2 changes: 1 addition & 1 deletion src/dedicatedserver/dedicatedserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ void cDedicatedServer::stopGames()
}

//------------------------------------------------------------------------
void cDedicatedServer::setProperty (const std::string& property, const std::string& value)
void cDedicatedServer::setProperty (std::string_view property, std::string_view value)
{
if (property == "port")
{
Expand Down
3 changes: 2 additions & 1 deletion src/dedicatedserver/dedicatedserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include <memory>
#include <string>
#include <string_view>
#include <vector>

class cDedicatedServerGame;
Expand Down Expand Up @@ -53,7 +54,7 @@ class cDedicatedServer
void printGames() const;
void printMaps() const;
bool startServer (int saveGameNumber = -1);
void setProperty (const std::string& property, const std::string& value);
void setProperty (std::string_view property, std::string_view value);
void saveGame (int saveGameNumber);
void stopGames();

Expand Down
6 changes: 3 additions & 3 deletions src/dedicatedserver/dedicatedservergame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ cDedicatedServerGame::cDedicatedServerGame (int saveGameNumber) :
signalConnectionManager.connect (lobbyServer.onClientConnected, [this] (const cPlayerBasicData& player) {
lobbyServer.sendChatMessage ("type --server help for dedicated server help", player.getNr());
});
signalConnectionManager.connect (lobbyServer.onDifferentVersion, [] (const std::string& version, const std::string& revision) {
signalConnectionManager.connect (lobbyServer.onDifferentVersion, [] (std::string_view version, std::string_view revision) {
std::cout << "player connects with different version:" << version << " " << revision << std::endl;
});

Expand Down Expand Up @@ -249,13 +249,13 @@ void cDedicatedServerGame::handleChatCommand (int fromPlayer, const std::vector<
lobbyServer.selectMap (map);
std::string reply = senderPlayer->getName();
reply += " changed the map.";
lobbyServer.sendChatMessage (reply);
lobbyServer.sendChatMessage (std::move (reply));
}
else
{
std::string reply = "Could not load map ";
reply += mapName;
lobbyServer.sendChatMessage (reply, senderPlayer->getNr());
lobbyServer.sendChatMessage (std::move (reply), senderPlayer->getNr());
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/chatcommand/chatcommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include "chatcommand.h"

//------------------------------------------------------------------------------
/*static*/ bool cChatCommand::isCommand (const std::string& command)
/*static*/ bool cChatCommand::isCommand (std::string_view command)
{
if (command.empty()) return false;
if (command[0] != '/') return false;
Expand Down
3 changes: 2 additions & 1 deletion src/lib/chatcommand/chatcommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <functional>
#include <memory>
#include <string>
#include <string_view>

template <typename... Arguments>
class cChatCommandParser;
Expand All @@ -35,7 +36,7 @@ class cChatCommandExecutorImpl;
class cChatCommand
{
public:
static bool isCommand (const std::string& command);
static bool isCommand (std::string_view command);

cChatCommand (std::string name, std::function<std::string()> description);

Expand Down
20 changes: 10 additions & 10 deletions src/lib/chatcommand/chatcommandarguments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
/*static*/ const char* const cChatCommandArgumentBool::falseName = "off";

//------------------------------------------------------------------------------
size_t getNextWordLength (const std::string& s, size_t position)
size_t getNextWordLength (std::string_view s, size_t position)
{
const auto begin = s.begin() + position;
const auto end = std::find_if (begin, s.end(), [] (unsigned char c) { return std::isspace (c); });
Expand All @@ -47,7 +47,7 @@ cChatCommandArgumentBool::cChatCommandArgumentBool (bool isOptional, ValueType d
{}

//------------------------------------------------------------------------------
size_t cChatCommandArgumentBool::parse (const std::string& command, size_t position)
size_t cChatCommandArgumentBool::parse (std::string_view command, size_t position)
{
const auto nextWordLength = getNextWordLength (command, position);
if (command.compare (position, nextWordLength, trueName) == 0)
Expand Down Expand Up @@ -111,7 +111,7 @@ cChatCommandArgumentChoice::cChatCommandArgumentChoice (std::vector<std::string>
}

//------------------------------------------------------------------------------
size_t cChatCommandArgumentChoice::parse (const std::string& command, size_t position)
size_t cChatCommandArgumentChoice::parse (std::string_view command, size_t position)
{
const auto nextWordLength = getNextWordLength (command, position);
bool success = false;
Expand Down Expand Up @@ -193,7 +193,7 @@ cChatCommandArgumentString::cChatCommandArgumentString (std::string name, bool i
{}

//------------------------------------------------------------------------------
size_t cChatCommandArgumentString::parse (const std::string& command, size_t position)
size_t cChatCommandArgumentString::parse (std::string_view command, size_t position)
{
value = command.substr (position);

Expand Down Expand Up @@ -238,7 +238,7 @@ cChatCommandArgumentServer::cChatCommandArgumentServer (cServer*& serverPointer,
{}

//------------------------------------------------------------------------------
size_t cChatCommandArgumentServer::parse (const std::string&, size_t position)
size_t cChatCommandArgumentServer::parse (std::string_view, size_t position)
{
value = serverPointer;
if (value == nullptr)
Expand Down Expand Up @@ -277,7 +277,7 @@ cChatCommandArgumentClient::cChatCommandArgumentClient (const std::shared_ptr<cC
{}

//------------------------------------------------------------------------------
size_t cChatCommandArgumentClient::parse (const std::string&, size_t position)
size_t cChatCommandArgumentClient::parse (std::string_view, size_t position)
{
if (activeClientPointer == nullptr)
{
Expand Down Expand Up @@ -319,7 +319,7 @@ cChatCommandArgumentServerPlayer::cChatCommandArgumentServerPlayer (cServer*& se
{}

//------------------------------------------------------------------------------
size_t cChatCommandArgumentServerPlayer::parse (const std::string& command, size_t position)
size_t cChatCommandArgumentServerPlayer::parse (std::string_view command, size_t position)
{
const auto server = serverPointer;
if (server == nullptr)
Expand Down Expand Up @@ -358,7 +358,7 @@ size_t cChatCommandArgumentServerPlayer::parse (const std::string& command, size
else
{
// TODO: translate
throw std::runtime_error ("Could not find player with name '" + playerName + "'");
throw std::runtime_error ("Could not find player with name '" + std::string (playerName) + "'");
}
}
}
Expand Down Expand Up @@ -391,7 +391,7 @@ cChatCommandArgumentClientPlayer::cChatCommandArgumentClientPlayer (const std::s
{}

//------------------------------------------------------------------------------
size_t cChatCommandArgumentClientPlayer::parse (const std::string& command, size_t position)
size_t cChatCommandArgumentClientPlayer::parse (std::string_view command, size_t position)
{
if (activeClientPointer == nullptr)
{
Expand Down Expand Up @@ -425,7 +425,7 @@ size_t cChatCommandArgumentClientPlayer::parse (const std::string& command, size
else
{
// TODO: translate
throw std::runtime_error ("Could not find player with name '" + playerName + "'");
throw std::runtime_error ("Could not find player with name '" + std::string (playerName) + "'");
}
}
}
Expand Down
20 changes: 10 additions & 10 deletions src/lib/chatcommand/chatcommandarguments.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

#include "utility/string/toNumber.h"

size_t getNextWordLength (const std::string& s, size_t position);
size_t getNextWordLength (std::string_view s, size_t position);

class cPlayer;
class cClient;
Expand All @@ -47,7 +47,7 @@ class cChatCommandArgumentBool

explicit cChatCommandArgumentBool (bool isOptional = false, ValueType defaultValue = false);

size_t parse (const std::string& command, size_t position);
size_t parse (std::string_view command, size_t position);

std::string toString() const;

Expand All @@ -71,7 +71,7 @@ class cChatCommandArgumentInt
defaultValue (std::move (defaultValue))
{}

size_t parse (const std::string& command, size_t position);
size_t parse (std::string_view command, size_t position);

std::string toString() const;

Expand All @@ -91,7 +91,7 @@ class cChatCommandArgumentChoice

explicit cChatCommandArgumentChoice (std::vector<std::string> choices, bool isOptional = false, size_t defaultSelection = 0);

size_t parse (const std::string& command, size_t position);
size_t parse (std::string_view command, size_t position);

std::string toString() const;

Expand All @@ -111,7 +111,7 @@ class cChatCommandArgumentString

explicit cChatCommandArgumentString (std::string name, bool isOptional = false, ValueType defaultValue = "");

size_t parse (const std::string& command, size_t position);
size_t parse (std::string_view command, size_t position);

std::string toString() const;

Expand All @@ -131,7 +131,7 @@ class cChatCommandArgumentServer

explicit cChatCommandArgumentServer (cServer*& serverPointer, bool isOptional = false, ValueType defaultValue = nullptr);

size_t parse (const std::string& command, size_t position);
size_t parse (std::string_view command, size_t position);

std::string toString() const;

Expand All @@ -151,7 +151,7 @@ class cChatCommandArgumentClient

explicit cChatCommandArgumentClient (const std::shared_ptr<cClient>& activeClientPointer, bool isOptional = false, ValueType defaultValue = nullptr);

size_t parse (const std::string& command, size_t position);
size_t parse (std::string_view command, size_t position);

std::string toString() const;

Expand All @@ -171,7 +171,7 @@ class cChatCommandArgumentServerPlayer

explicit cChatCommandArgumentServerPlayer (cServer*& serverPointer, bool isOptional = false, ValueType defaultValue = nullptr);

size_t parse (const std::string& command, size_t position);
size_t parse (std::string_view command, size_t position);

std::string toString() const;

Expand All @@ -191,7 +191,7 @@ class cChatCommandArgumentClientPlayer

explicit cChatCommandArgumentClientPlayer (const std::shared_ptr<cClient>& activeClientPointer, bool isOptional = false, ValueType defaultValue = nullptr);

size_t parse (const std::string& command, size_t position);
size_t parse (std::string_view command, size_t position);

std::string toString() const;

Expand All @@ -206,7 +206,7 @@ class cChatCommandArgumentClientPlayer

//------------------------------------------------------------------------------
template <typename T>
size_t cChatCommandArgumentInt<T>::parse (const std::string& command, size_t position)
size_t cChatCommandArgumentInt<T>::parse (std::string_view command, size_t position)
{
const auto nextWordLength = getNextWordLength (command, position);

Expand Down
8 changes: 4 additions & 4 deletions src/lib/chatcommand/chatcommandexecutor.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@

class cChatCommand;

void skipWhiteSpace (const std::string& command, size_t& position);
void skipWhiteSpace (std::string_view command, size_t& position);

class cChatCommandExecutor
{
public:
virtual ~cChatCommandExecutor() = default;
virtual bool tryExecute (const std::string& command) const = 0;
virtual bool tryExecute (std::string_view command) const = 0;
virtual void printArguments (std::ostream& result) const = 0;
virtual const cChatCommand& getCommand() const = 0;
};
Expand All @@ -44,7 +44,7 @@ class cChatCommandExecutorImpl : public cChatCommandExecutor
public:
cChatCommandExecutorImpl (F function_, cChatCommandParser<Arguments...> parser_);

bool tryExecute (const std::string& command) const override;
bool tryExecute (std::string_view command) const override;
void printArguments (std::ostream& result) const override;
const cChatCommand& getCommand() const override;

Expand All @@ -60,7 +60,7 @@ cChatCommandExecutorImpl<F, Arguments...>::cChatCommandExecutorImpl (F function_
{}

template <typename F, typename... Arguments>
bool cChatCommandExecutorImpl<F, Arguments...>::tryExecute (const std::string& command) const
bool cChatCommandExecutorImpl<F, Arguments...>::tryExecute (std::string_view command) const
{
if (!cChatCommand::isCommand (command)) return false;

Expand Down
4 changes: 2 additions & 2 deletions src/lib/chatcommand/chatcommandparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include <string>

//------------------------------------------------------------------------------
void skipWhiteSpace (const std::string& command, size_t& position)
void skipWhiteSpace (std::string_view command, size_t& position)
{
while (position < command.size() && std::isspace (static_cast<unsigned char>(command[position])))
{
Expand All @@ -37,7 +37,7 @@ cChatCommandParser<>::cChatCommandParser (cChatCommand command_) :
{}

//------------------------------------------------------------------------------
size_t cChatCommandParser<>::parse (const std::string&, size_t position) const
size_t cChatCommandParser<>::parse (std::string_view, size_t position) const
{
return position;
}
Expand Down
8 changes: 4 additions & 4 deletions src/lib/chatcommand/chatcommandparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
# include <string>
# include <tuple>

void skipWhiteSpace (const std::string& command, size_t& position);
void skipWhiteSpace (std::string_view command, size_t& position);

template <typename... Arguments>
class cChatCommandParser;
Expand All @@ -40,7 +40,7 @@ class cChatCommandParser<>

cChatCommandParser (cChatCommand command_);

size_t parse (const std::string& command, size_t position) const;
size_t parse (std::string_view command, size_t position) const;
void printArguments (std::ostream& result) const;

const cChatCommand& getCommand() const;
Expand All @@ -58,7 +58,7 @@ class cChatCommandParser<Argument, LastArguments...>
using ArgumentValueTypes = std::tuple<typename LastArguments::ValueTypes..., typename Argument::ValueType>;

cChatCommandParser (cChatCommandParser<LastArguments...> lastParser_, Argument argument_);
size_t parse (const std::string& command, size_t position) const;
size_t parse (std::string_view command, size_t position) const;

template <typename NewArgument, typename... Args>
cChatCommandParser<NewArgument, Argument, LastArguments...> addArgument (Args&&... args) &&;
Expand All @@ -84,7 +84,7 @@ cChatCommandParser<Argument, LastArguments...>::cChatCommandParser (cChatCommand

//------------------------------------------------------------------------------
template <typename Argument, typename... LastArguments>
size_t cChatCommandParser<Argument, LastArguments...>::parse (const std::string& command, size_t position) const
size_t cChatCommandParser<Argument, LastArguments...>::parse (std::string_view command, size_t position) const
{
position = lastParser.parse (command, position);
try
Expand Down
2 changes: 1 addition & 1 deletion src/lib/game/data/map/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ std::string cMap::resourcesToString() const
}

//------------------------------------------------------------------------------
void cMap::setResourcesFromString (const std::string& str)
void cMap::setResourcesFromString (std::string_view str)
{
for (size_t i = 0; i != Resources.size(); ++i)
{
Expand Down
4 changes: 2 additions & 2 deletions src/lib/game/data/map/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#include <cassert>
#include <filesystem>
#include <memory>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

Expand Down Expand Up @@ -303,7 +303,7 @@ class cMap
private:
void init();
std::string resourcesToString() const;
void setResourcesFromString (const std::string&);
void setResourcesFromString (std::string_view);

static int getMapLevel (const cBuilding&);
static int getMapLevel (const cVehicle&);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/game/data/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ const cPlayer* cModel::getPlayer (int playerNr) const
return it == playerList.end() ? nullptr : it->get();
}
//------------------------------------------------------------------------------
const cPlayer* cModel::getPlayer (std::string playerName) const
const cPlayer* cModel::getPlayer (std::string_view playerName) const
{
// first try to find player by number
if (const auto playerNr = toInt (playerName))
Expand Down
3 changes: 2 additions & 1 deletion src/lib/game/data/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <cassert>
#include <forward_list>
#include <memory>
#include <string_view>
#include <vector>

class cBuilding;
Expand Down Expand Up @@ -91,7 +92,7 @@ class cModel

cPlayer* getPlayer (int playerNr);
const cPlayer* getPlayer (int playerNr) const;
const cPlayer* getPlayer (std::string player) const;
const cPlayer* getPlayer (std::string_view player) const;
const std::vector<std::shared_ptr<cPlayer>>& getPlayerList() const { return /*static_cast<std::vector<std::shared_ptr<const cPlayer>>>*/ (playerList); } //TODO: cast to const cPlayer
std::vector<std::shared_ptr<cPlayer>>& getPlayerList() { return playerList; }
void setPlayerList (const std::vector<cPlayerBasicData>&);
Expand Down
Loading

0 comments on commit a1be6f4

Please sign in to comment.