bool cRoot::FindAndDoWithPlayer(const AString & a_PlayerName, cPlayerListCallback & a_Callback) { class cCallback : public cPlayerListCallback { size_t m_BestRating; size_t m_NameLength; const AString m_PlayerName; virtual bool Item (cPlayer * a_pPlayer) { size_t Rating = RateCompareString (m_PlayerName, a_pPlayer->GetName()); if ((Rating > 0) && (Rating >= m_BestRating)) { m_BestMatch = a_pPlayer; if (Rating > m_BestRating) { m_NumMatches = 0; } m_BestRating = Rating; ++m_NumMatches; } if (Rating == m_NameLength) // Perfect match { return true; } return false; } public: cCallback (const AString & a_PlayerName) : m_BestRating(0), m_NameLength(a_PlayerName.length()), m_PlayerName(a_PlayerName), m_BestMatch(nullptr), m_NumMatches(0) {} cPlayer * m_BestMatch; unsigned m_NumMatches; } Callback (a_PlayerName); ForEachPlayer(Callback); if (Callback.m_NumMatches == 1) { return a_Callback.Item(Callback.m_BestMatch); } return false; }
bool cRoot::FindAndDoWithPlayer(const AString & a_PlayerName, cPlayerListCallback & a_Callback) { class cCallback : public cPlayerListCallback { unsigned int BestRating; unsigned int NameLength; const AString PlayerName; cPlayerListCallback & m_Callback; virtual bool Item (cPlayer * a_pPlayer) { unsigned int Rating = RateCompareString (PlayerName, a_pPlayer->GetName()); if (Rating > 0 && Rating >= BestRating) { BestMatch = a_pPlayer; if( Rating > BestRating ) NumMatches = 0; BestRating = Rating; ++NumMatches; } if (Rating == NameLength) // Perfect match { return false; } return true; } public: cCallback (const AString & a_PlayerName, cPlayerListCallback & a_Callback) : m_Callback( a_Callback ) , BestMatch( NULL ) , BestRating( 0 ) , NumMatches( 0 ) , NameLength( a_PlayerName.length() ) , PlayerName( a_PlayerName ) {} cPlayer * BestMatch; unsigned int NumMatches; } Callback (a_PlayerName, a_Callback); ForEachPlayer( Callback ); if (Callback.NumMatches == 1) { return a_Callback.Item (Callback.BestMatch); } return false; }