diff --git a/src/CBot/CBotClass.cpp b/src/CBot/CBotClass.cpp index b3778a7b..f1a658ad 100644 --- a/src/CBot/CBotClass.cpp +++ b/src/CBot/CBotClass.cpp @@ -192,7 +192,7 @@ bool CBotClass::AddItem(CBotVar* pVar) } //////////////////////////////////////////////////////////////////////////////// -std::string CBotClass::GetName() +const std::string& CBotClass::GetName() { return m_name; } diff --git a/src/CBot/CBotClass.h b/src/CBot/CBotClass.h index bbdedefc..0f9a8ce4 100644 --- a/src/CBot/CBotClass.h +++ b/src/CBot/CBotClass.h @@ -173,7 +173,7 @@ public: * \brief GetName Gives the name of the class. * \return */ - std::string GetName(); + const std::string& GetName(); /*! * \brief GetParent Gives the parent class (or nullptr). diff --git a/src/CBot/CBotInstr/CBotFunction.cpp b/src/CBot/CBotInstr/CBotFunction.cpp index 492711db..b4473cd8 100644 --- a/src/CBot/CBotInstr/CBotFunction.cpp +++ b/src/CBot/CBotInstr/CBotFunction.cpp @@ -996,7 +996,7 @@ bool CBotFunction::CheckParam(CBotDefParam* pParam) } //////////////////////////////////////////////////////////////////////////////// -std::string CBotFunction::GetName() +const std::string& CBotFunction::GetName() { return m_token.GetString(); } diff --git a/src/CBot/CBotInstr/CBotFunction.h b/src/CBot/CBotInstr/CBotFunction.h index 90735158..b16780cd 100644 --- a/src/CBot/CBotInstr/CBotFunction.h +++ b/src/CBot/CBotInstr/CBotFunction.h @@ -207,7 +207,7 @@ public: * \brief GetName * \return */ - std::string GetName(); + const std::string& GetName(); /*! * \brief GetParams diff --git a/src/CBot/CBotToken.cpp b/src/CBot/CBotToken.cpp index 6f402165..9663e299 100644 --- a/src/CBot/CBotToken.cpp +++ b/src/CBot/CBotToken.cpp @@ -21,13 +21,22 @@ #include #include +#include namespace CBot { +namespace { + template + boost::bimap makeBimap(std::initializer_list::value_type> list) + { + return boost::bimap(list.begin(), list.end()); + } +} + //! \brief Keeps the string corresponding to keyword ID //! Map is filled with id-string pars that are needed for CBot language parsing -static const std::map KEYWORDS = { +static const boost::bimap KEYWORDS = makeBimap({ {ID_IF, "if"}, {ID_ELSE, "else"}, {ID_WHILE, "while"}, @@ -115,7 +124,7 @@ static const std::map KEYWORDS = { {ID_ASSMODULO, "%="}, {TX_UNDEF, "undefined"}, {TX_NAN, "not a number"} -}; +}); namespace { @@ -123,9 +132,10 @@ static const std::string emptyString = ""; } const std::string& LoadString(TokenId id) { - if (KEYWORDS.find(id) != KEYWORDS.end()) + auto it = KEYWORDS.left.find(id); + if (it != KEYWORDS.left.end()) { - return KEYWORDS.at(id); + return it->second; } else { @@ -210,7 +220,7 @@ long CBotToken::GetKeywordId() } //////////////////////////////////////////////////////////////////////////////// -std::string CBotToken::GetString() +const std::string& CBotToken::GetString() { return m_text; } @@ -242,6 +252,7 @@ void CBotToken::SetPos(int start, int end) //////////////////////////////////////////////////////////////////////////////// +// TODO: CharInList could probably be optimized to conditions like (*c >= '0' && *c <= '9') to gain some performance static char sep1[] = " \r\n\t,:()[]{}-+*/=;>!~^|&%.?"; // operational separators @@ -438,10 +449,10 @@ std::unique_ptr CBotToken::CompileTokens(const std::string& program) //////////////////////////////////////////////////////////////////////////////// int CBotToken::GetKeyWord(const std::string& w) { - for (const auto& it : KEYWORDS) - { - if (it.second == w) return it.first; - } + auto it = KEYWORDS.right.find(w); + if (it != KEYWORDS.right.end()) { + return it->second; + } return -1; } diff --git a/src/CBot/CBotToken.h b/src/CBot/CBotToken.h index cfbda812..4dc7450a 100644 --- a/src/CBot/CBotToken.h +++ b/src/CBot/CBotToken.h @@ -120,7 +120,7 @@ public: * \brief Return the token string * \return The string associated with this token */ - std::string GetString(); + const std::string& GetString(); /** * \brief Set the token string diff --git a/src/CBot/CBotVar/CBotVar.cpp b/src/CBot/CBotVar/CBotVar.cpp index 742ef598..62ba41f4 100644 --- a/src/CBot/CBotVar/CBotVar.cpp +++ b/src/CBot/CBotVar/CBotVar.cpp @@ -49,7 +49,7 @@ namespace CBot long CBotVar::m_identcpt = 0; //////////////////////////////////////////////////////////////////////////////// -CBotVar::CBotVar( ) +CBotVar::CBotVar( ) : m_token(nullptr) { m_pMyThis = nullptr; m_pUserPtr = nullptr; @@ -62,9 +62,17 @@ CBotVar::CBotVar( ) m_mPrivate = ProtectionLevel::Public; } -CBotVar::CBotVar(const CBotToken &name) : CBotVar() +CBotVar::CBotVar(const CBotToken &name) : m_token(new CBotToken(name)) { - m_token = new CBotToken(name); + m_pMyThis = nullptr; + m_pUserPtr = nullptr; + m_InitExpr = nullptr; + m_LimExpr = nullptr; + m_type = -1; + m_binit = InitType::UNDEF; + m_ident = 0; + m_bStatic = false; + m_mPrivate = ProtectionLevel::Public; } //////////////////////////////////////////////////////////////////////////////// @@ -378,7 +386,7 @@ void CBotVar::SetInit(CBotVar::InitType initType) } //////////////////////////////////////////////////////////////////////////////// -std::string CBotVar::GetName() +const std::string& CBotVar::GetName() { return m_token->GetString(); } diff --git a/src/CBot/CBotVar/CBotVar.h b/src/CBot/CBotVar/CBotVar.h index a023e2f9..d2805ddf 100644 --- a/src/CBot/CBotVar/CBotVar.h +++ b/src/CBot/CBotVar/CBotVar.h @@ -170,7 +170,7 @@ public: * \brief Returns the name of the variable * \return The name of the variable, empty string if unknown */ - std::string GetName(); + const std::string& GetName(); /** * \brief SetName Changes the name of the variable @@ -650,7 +650,7 @@ public: protected: //! The corresponding token, defines the variable name - CBotToken* m_token; + CBotToken* const m_token; //! Type of value. CBotTypResult m_type; //! Initialization status diff --git a/src/CBot/CBotVar/CBotVarArray.cpp b/src/CBot/CBotVar/CBotVarArray.cpp index cadfe1e5..6462d119 100644 --- a/src/CBot/CBotVar/CBotVarArray.cpp +++ b/src/CBot/CBotVar/CBotVarArray.cpp @@ -31,12 +31,11 @@ namespace CBot { //////////////////////////////////////////////////////////////////////////////// -CBotVarArray::CBotVarArray(const CBotToken& name, CBotTypResult& type) +CBotVarArray::CBotVarArray(const CBotToken& name, CBotTypResult& type) : CBotVar(name) { if ( !type.Eq(CBotTypArrayPointer) && !type.Eq(CBotTypArrayBody)) assert(0); - m_token = new CBotToken(name); m_next = nullptr; m_pMyThis = nullptr; m_pUserPtr = nullptr; diff --git a/src/CBot/CBotVar/CBotVarClass.cpp b/src/CBot/CBotVar/CBotVarClass.cpp index 06944b51..36952d18 100644 --- a/src/CBot/CBotVar/CBotVarClass.cpp +++ b/src/CBot/CBotVar/CBotVarClass.cpp @@ -36,7 +36,7 @@ namespace CBot std::set CBotVarClass::m_instances{}; //////////////////////////////////////////////////////////////////////////////// -CBotVarClass::CBotVarClass(const CBotToken& name, const CBotTypResult& type) +CBotVarClass::CBotVarClass(const CBotToken& name, const CBotTypResult& type) : CBotVar(name) { if ( !type.Eq(CBotTypClass) && !type.Eq(CBotTypIntrinsic) && // by convenience there accepts these types @@ -44,7 +44,6 @@ CBotVarClass::CBotVarClass(const CBotToken& name, const CBotTypResult& type) !type.Eq(CBotTypArrayPointer) && !type.Eq(CBotTypArrayBody)) assert(0); - m_token = new CBotToken(name); m_next = nullptr; m_pMyThis = nullptr; m_pUserPtr = OBJECTCREATED;//nullptr; diff --git a/src/CBot/CBotVar/CBotVarPointer.cpp b/src/CBot/CBotVar/CBotVarPointer.cpp index afc81b93..2f78c90a 100644 --- a/src/CBot/CBotVar/CBotVarPointer.cpp +++ b/src/CBot/CBotVar/CBotVarPointer.cpp @@ -32,14 +32,13 @@ namespace CBot { //////////////////////////////////////////////////////////////////////////////// -CBotVarPointer::CBotVarPointer(const CBotToken& name, CBotTypResult& type) +CBotVarPointer::CBotVarPointer(const CBotToken& name, CBotTypResult& type) : CBotVar(name) { if ( !type.Eq(CBotTypPointer) && !type.Eq(CBotTypNullPointer) && !type.Eq(CBotTypClass) && // for convenience accepts Class and Intrinsic !type.Eq(CBotTypIntrinsic) ) assert(0); - m_token = new CBotToken(name); m_next = nullptr; m_pMyThis = nullptr; m_pUserPtr = nullptr;