From 8b6eee672b63efc3eb5147d7bf63b124c88b5720 Mon Sep 17 00:00:00 2001 From: elexis Date: Mon, 19 Aug 2019 19:43:13 +0000 Subject: [PATCH] Move GUI string parsing specializations to a separate file and remove __ParseString proxy. Differential Revision: https://code.wildfiregames.com/D2194 This was SVN commit r22696. --- source/gui/GUIStringConversions.cpp | 247 ++++++++++++++++++++++++++++ source/gui/GUIutil.cpp | 221 ------------------------- source/gui/GUIutil.h | 13 +- source/gui/tests/test_ParseString.h | 24 +-- 4 files changed, 260 insertions(+), 245 deletions(-) create mode 100644 source/gui/GUIStringConversions.cpp diff --git a/source/gui/GUIStringConversions.cpp b/source/gui/GUIStringConversions.cpp new file mode 100644 index 0000000000..254ef188f4 --- /dev/null +++ b/source/gui/GUIStringConversions.cpp @@ -0,0 +1,247 @@ +/* Copyright (C) 2019 Wildfire Games. + * This file is part of 0 A.D. + * + * 0 A.D. 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. + * + * 0 A.D. 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 0 A.D. If not, see . + */ + +#include "precompiled.h" + +#include "GUIutil.h" + +#include "gui/CGUIString.h" +#include "ps/CLogger.h" + +class CGUIList; +class CGUISeries; + +template <> +bool GUI::ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, bool& Output) +{ + if (Value == L"true") + Output = true; + else if (Value == L"false") + Output = false; + else + return false; + + return true; +} + +template <> +bool GUI::ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, int& Output) +{ + Output = Value.ToInt(); + return true; +} + +template <> +bool GUI::ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, u32& Output) +{ + Output = Value.ToUInt(); + return true; +} + +template <> +bool GUI::ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, float& Output) +{ + Output = Value.ToFloat(); + return true; +} + +template <> +bool GUI::ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, CRect& Output) +{ + const unsigned int NUM_COORDS = 4; + float coords[NUM_COORDS]; + std::wstringstream stream; + stream.str(Value); + // Parse each coordinate + for (unsigned int i = 0; i < NUM_COORDS; ++i) + { + if (stream.eof()) + { + LOGWARNING("Too few CRect parameters (min %i). Your input: '%s'", NUM_COORDS, Value.ToUTF8().c_str()); + return false; + } + stream >> coords[i]; + if ((stream.rdstate() & std::wstringstream::failbit) != 0) + { + LOGWARNING("Unable to parse CRect parameters. Your input: '%s'", Value.ToUTF8().c_str()); + return false; + } + } + + if (!stream.eof()) + { + LOGWARNING("Too many CRect parameters (max %i). Your input: '%s'", NUM_COORDS, Value.ToUTF8().c_str()); + return false; + } + + // Finally the rectangle values + Output = CRect(coords[0], coords[1], coords[2], coords[3]); + + return true; +} + +template <> +bool GUI::ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, CClientArea& Output) +{ + return Output.SetClientArea(Value.ToUTF8()); +} + +template <> +bool GUI::ParseString(const CGUI* pGUI, const CStrW& Value, CGUIColor& Output) +{ + return Output.ParseString(pGUI, Value.ToUTF8()); +} + +template <> +bool GUI::ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, CSize& Output) +{ + const unsigned int NUM_COORDS = 2; + float coords[NUM_COORDS]; + std::wstringstream stream; + stream.str(Value); + // Parse each coordinate + for (unsigned int i = 0; i < NUM_COORDS; ++i) + { + if (stream.eof()) + { + LOGWARNING("Too few CSize parameters (min %i). Your input: '%s'", NUM_COORDS, Value.ToUTF8().c_str()); + return false; + } + stream >> coords[i]; + if ((stream.rdstate() & std::wstringstream::failbit) != 0) + { + LOGWARNING("Unable to parse CSize parameters. Your input: '%s'", Value.ToUTF8().c_str()); + return false; + } + } + + Output.cx = coords[0]; + Output.cy = coords[1]; + + if (!stream.eof()) + { + LOGWARNING("Too many CSize parameters (max %i). Your input: '%s'", NUM_COORDS, Value.ToUTF8().c_str()); + return false; + } + + return true; +} + +template <> +bool GUI::ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, CPos& Output) +{ + const unsigned int NUM_COORDS = 2; + float coords[NUM_COORDS]; + std::wstringstream stream; + stream.str(Value); + // Parse each coordinate + for (unsigned int i = 0; i < NUM_COORDS; ++i) + { + if (stream.eof()) + { + LOGWARNING("Too few CPos parameters (min %i). Your input: '%s'", NUM_COORDS, Value.ToUTF8().c_str()); + return false; + } + stream >> coords[i]; + if ((stream.rdstate() & std::wstringstream::failbit) != 0) + { + LOGWARNING("Unable to parse CPos parameters. Your input: '%s'", Value.ToUTF8().c_str()); + return false; + } + } + + Output.x = coords[0]; + Output.y = coords[1]; + + if (!stream.eof()) + { + LOGWARNING("Too many CPos parameters (max %i). Your input: '%s'", NUM_COORDS, Value.ToUTF8().c_str()); + return false; + } + + return true; +} + +template <> +bool GUI::ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, EAlign& Output) +{ + if (Value == L"left") + Output = EAlign_Left; + else if (Value == L"center") + Output = EAlign_Center; + else if (Value == L"right") + Output = EAlign_Right; + else + return false; + + return true; +} + +template <> +bool GUI::ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, EVAlign& Output) +{ + if (Value == L"top") + Output = EVAlign_Top; + else if (Value == L"center") + Output = EVAlign_Center; + else if (Value == L"bottom") + Output = EVAlign_Bottom; + else + return false; + + return true; +} + +template <> +bool GUI::ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, CGUIString& Output) +{ + Output.SetValue(Value); + return true; +} + +template <> +bool GUI::ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, CStr& Output) +{ + Output = Value.ToUTF8(); + return true; +} + +template <> +bool GUI::ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, CStrW& Output) +{ + Output = Value; + return true; +} + +template <> +bool GUI::ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, CGUISpriteInstance& Output) +{ + Output = CGUISpriteInstance(Value.ToUTF8()); + return true; +} + +template <> +bool GUI::ParseString(const CGUI* UNUSED(pGUI), const CStrW& UNUSED(Value), CGUISeries& UNUSED(Output)) +{ + return false; +} + +template <> +bool GUI::ParseString(const CGUI* UNUSED(pGUI), const CStrW& UNUSED(Value), CGUIList& UNUSED(Output)) +{ + return false; +} diff --git a/source/gui/GUIutil.cpp b/source/gui/GUIutil.cpp index d9f341cf53..5d6bea090d 100644 --- a/source/gui/GUIutil.cpp +++ b/source/gui/GUIutil.cpp @@ -81,227 +81,6 @@ void CGUISetting::ToJSVal(JSContext* cx, JS::MutableHandleValue Value) ScriptInterface::ToJSVal(cx, Value, m_pSetting); }; -template <> -bool __ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, bool& Output) -{ - if (Value == L"true") - Output = true; - else if (Value == L"false") - Output = false; - else - return false; - - return true; -} - -template <> -bool __ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, int& Output) -{ - Output = Value.ToInt(); - return true; -} - -template <> -bool __ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, u32& Output) -{ - Output = Value.ToUInt(); - return true; -} - -template <> -bool __ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, float& Output) -{ - Output = Value.ToFloat(); - return true; -} - -template <> -bool __ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, CRect& Output) -{ - const unsigned int NUM_COORDS = 4; - float coords[NUM_COORDS]; - std::wstringstream stream; - stream.str(Value); - // Parse each coordinate - for (unsigned int i = 0; i < NUM_COORDS; ++i) - { - if (stream.eof()) - { - LOGWARNING("Too few CRect parameters (min %i). Your input: '%s'", NUM_COORDS, Value.ToUTF8().c_str()); - return false; - } - stream >> coords[i]; - if ((stream.rdstate() & std::wstringstream::failbit) != 0) - { - LOGWARNING("Unable to parse CRect parameters. Your input: '%s'", Value.ToUTF8().c_str()); - return false; - } - } - - if (!stream.eof()) - { - LOGWARNING("Too many CRect parameters (max %i). Your input: '%s'", NUM_COORDS, Value.ToUTF8().c_str()); - return false; - } - - // Finally the rectangle values - Output = CRect(coords[0], coords[1], coords[2], coords[3]); - - return true; -} - -template <> -bool __ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, CClientArea& Output) -{ - return Output.SetClientArea(Value.ToUTF8()); -} - -template <> -bool __ParseString(const CGUI* pGUI, const CStrW& Value, CGUIColor& Output) -{ - return Output.ParseString(pGUI, Value.ToUTF8()); -} - -template <> -bool __ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, CSize& Output) -{ - const unsigned int NUM_COORDS = 2; - float coords[NUM_COORDS]; - std::wstringstream stream; - stream.str(Value); - // Parse each coordinate - for (unsigned int i = 0; i < NUM_COORDS; ++i) - { - if (stream.eof()) - { - LOGWARNING("Too few CSize parameters (min %i). Your input: '%s'", NUM_COORDS, Value.ToUTF8().c_str()); - return false; - } - stream >> coords[i]; - if ((stream.rdstate() & std::wstringstream::failbit) != 0) - { - LOGWARNING("Unable to parse CSize parameters. Your input: '%s'", Value.ToUTF8().c_str()); - return false; - } - } - - Output.cx = coords[0]; - Output.cy = coords[1]; - - if (!stream.eof()) - { - LOGWARNING("Too many CSize parameters (max %i). Your input: '%s'", NUM_COORDS, Value.ToUTF8().c_str()); - return false; - } - - return true; -} - -template <> -bool __ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, CPos& Output) -{ - const unsigned int NUM_COORDS = 2; - float coords[NUM_COORDS]; - std::wstringstream stream; - stream.str(Value); - // Parse each coordinate - for (unsigned int i = 0; i < NUM_COORDS; ++i) - { - if (stream.eof()) - { - LOGWARNING("Too few CPos parameters (min %i). Your input: '%s'", NUM_COORDS, Value.ToUTF8().c_str()); - return false; - } - stream >> coords[i]; - if ((stream.rdstate() & std::wstringstream::failbit) != 0) - { - LOGWARNING("Unable to parse CPos parameters. Your input: '%s'", Value.ToUTF8().c_str()); - return false; - } - } - - Output.x = coords[0]; - Output.y = coords[1]; - - if (!stream.eof()) - { - LOGWARNING("Too many CPos parameters (max %i). Your input: '%s'", NUM_COORDS, Value.ToUTF8().c_str()); - return false; - } - - return true; -} - -template <> -bool __ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, EAlign& Output) -{ - if (Value == L"left") - Output = EAlign_Left; - else if (Value == L"center") - Output = EAlign_Center; - else if (Value == L"right") - Output = EAlign_Right; - else - return false; - - return true; -} - -template <> -bool __ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, EVAlign& Output) -{ - if (Value == L"top") - Output = EVAlign_Top; - else if (Value == L"center") - Output = EVAlign_Center; - else if (Value == L"bottom") - Output = EVAlign_Bottom; - else - return false; - - return true; -} - -template <> -bool __ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, CGUIString& Output) -{ - Output.SetValue(Value); - return true; -} - -template <> -bool __ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, CStr& Output) -{ - Output = Value.ToUTF8(); - return true; -} - -template <> -bool __ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, CStrW& Output) -{ - Output = Value; - return true; -} - -template <> -bool __ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, CGUISpriteInstance& Output) -{ - Output = CGUISpriteInstance(Value.ToUTF8()); - return true; -} - -template <> -bool __ParseString(const CGUI* UNUSED(pGUI), const CStrW& UNUSED(Value), CGUIList& UNUSED(Output)) -{ - return false; -} - -template <> -bool __ParseString(const CGUI* UNUSED(pGUI), const CStrW& UNUSED(Value), CGUISeries& UNUSED(Output)) -{ - return false; -} - template PSRETURN GUI::GetSettingPointer(const IGUIObject* pObject, const CStr& Setting, T*& Value) { diff --git a/source/gui/GUIutil.h b/source/gui/GUIutil.h index e73cecb3c4..906616a709 100644 --- a/source/gui/GUIutil.h +++ b/source/gui/GUIutil.h @@ -110,9 +110,6 @@ private: T m_pSetting; }; -template -bool __ParseString(const CGUI* pGUI, const CStrW& Value, T& tOutput); - struct SGUIMessage; /** @@ -180,19 +177,11 @@ public: * Sets a value by setting and object name using a real * datatype as input. * - * This is just a wrapper for __ParseString() which really - * works the magic. - * * @param Value The value in string form, like "0 0 100% 100%" * @param tOutput Parsed value of type T * @return True at success. - * - * @see __ParseString() */ - static bool ParseString(const CGUI* pGUI, const CStrW& Value, T& tOutput) - { - return __ParseString(pGUI, Value, tOutput); - } + static bool ParseString(const CGUI* pGUI, const CStrW& Value, T& tOutput); private: diff --git a/source/gui/tests/test_ParseString.h b/source/gui/tests/test_ParseString.h index e575b17c16..2338af399c 100644 --- a/source/gui/tests/test_ParseString.h +++ b/source/gui/tests/test_ParseString.h @@ -68,12 +68,12 @@ public: TestLogger nolog; CRect test; - TS_ASSERT(__ParseString(nullptr, CStrW(L"0.0 10.0 20.0 30.0"), test)); + TS_ASSERT(GUI::ParseString(nullptr, CStrW(L"0.0 10.0 20.0 30.0"), test)); TS_ASSERT_EQUALS(CRect(0.0, 10.0, 20.0, 30.0), test); - TS_ASSERT(!__ParseString(nullptr, CStrW(L"0 10 20"), test)); - TS_ASSERT(!__ParseString(nullptr, CStrW(L"0 10 20 30 40"), test)); - TS_ASSERT(!__ParseString(nullptr, CStrW(L"0,0 10,0 20,0 30,0"), test)); + TS_ASSERT(!GUI::ParseString(nullptr, CStrW(L"0 10 20"), test)); + TS_ASSERT(!GUI::ParseString(nullptr, CStrW(L"0 10 20 30 40"), test)); + TS_ASSERT(!GUI::ParseString(nullptr, CStrW(L"0,0 10,0 20,0 30,0"), test)); } void test_size() @@ -81,12 +81,12 @@ public: TestLogger nolog; CSize test; - TS_ASSERT(__ParseString(nullptr, CStrW(L"0.0 10.0"), test)); + TS_ASSERT(GUI::ParseString(nullptr, CStrW(L"0.0 10.0"), test)); TS_ASSERT_EQUALS(CSize(0.0, 10.0), test); - TS_ASSERT(!__ParseString(nullptr, CStrW(L"0"), test)); - TS_ASSERT(!__ParseString(nullptr, CStrW(L"0 10 20"), test)); - TS_ASSERT(!__ParseString(nullptr, CStrW(L"0,0 10,0"), test)); + TS_ASSERT(!GUI::ParseString(nullptr, CStrW(L"0"), test)); + TS_ASSERT(!GUI::ParseString(nullptr, CStrW(L"0 10 20"), test)); + TS_ASSERT(!GUI::ParseString(nullptr, CStrW(L"0,0 10,0"), test)); } void test_pos() @@ -94,11 +94,11 @@ public: TestLogger nolog; CPos test; - TS_ASSERT(__ParseString(nullptr, CStrW(L"0.0 10.0"), test)); + TS_ASSERT(GUI::ParseString(nullptr, CStrW(L"0.0 10.0"), test)); TS_ASSERT_EQUALS(CPos(0.0, 10.0), test); - TS_ASSERT(!__ParseString(nullptr, CStrW(L"0"), test)); - TS_ASSERT(!__ParseString(nullptr, CStrW(L"0 10 20"), test)); - TS_ASSERT(!__ParseString(nullptr, CStrW(L"0,0 10,0"), test)); + TS_ASSERT(!GUI::ParseString(nullptr, CStrW(L"0"), test)); + TS_ASSERT(!GUI::ParseString(nullptr, CStrW(L"0 10 20"), test)); + TS_ASSERT(!GUI::ParseString(nullptr, CStrW(L"0,0 10,0"), test)); } };