LCOV - code coverage report
Current view: top level - source/gui - CGUISetting.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 22 93 23.7 %
Date: 2022-06-14 00:41:00 Functions: 5 102 4.9 %

          Line data    Source code
       1             : /* Copyright (C) 2021 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #ifndef INCLUDED_CGUISETTINGS
      19             : #define INCLUDED_CGUISETTINGS
      20             : 
      21             : #include "ps/CStr.h"
      22             : #include "scriptinterface/ScriptForward.h"
      23             : 
      24             : 
      25             : class IGUIObject;
      26             : 
      27             : /**
      28             :  * This setting interface allows GUI objects to call setting function functions without having to know the setting type.
      29             :  * This is fact is used for setting the value from a JS value or XML value (string) and when deleting the setting,
      30             :  * when the type of the setting value is not known in advance.
      31             :  */
      32             : class IGUISetting
      33             : {
      34             : public:
      35             :     NONCOPYABLE(IGUISetting);
      36             :     IGUISetting(const CStr& name, IGUIObject* owner);
      37             : 
      38             :     /**
      39             :      * Parses the given string and assigns to the setting value. Used for parsing XML attributes.
      40             :      */
      41             :     bool FromString(const CStrW& value, const bool sendMessage);
      42             : 
      43             :     /**
      44             :      * Parses the given JS::Value using Script::FromJSVal and assigns it to the setting data.
      45             :      */
      46             :     bool FromJSVal(const ScriptRequest& rq, JS::HandleValue value, const bool sendMessage);
      47             : 
      48             :     /**
      49             :      * Converts the setting data to a JS::Value using Script::ToJSVal.
      50             :      */
      51             :     virtual void ToJSVal(const ScriptRequest& rq, JS::MutableHandleValue value) = 0;
      52             : 
      53             : protected:
      54             :     IGUISetting(IGUISetting&& o);
      55           0 :     virtual ~IGUISetting() = default;
      56             : 
      57             :     virtual bool DoFromString(const CStrW& value) = 0;
      58             :     virtual bool DoFromJSVal(const ScriptRequest& rq, JS::HandleValue value) = 0;
      59             : 
      60             :     /**
      61             :      * Triggers the IGUIObject logic when a setting changes.
      62             :      * This should be called by derived classes when something externally visible changes,
      63             :      * unless overloaded to provide similar behaviour.
      64             :      */
      65             :     virtual void OnSettingChange(const CStr& setting, bool sendMessage);
      66             : 
      67             :     /**
      68             :      * Return the name of the setting, from JS.
      69             :      */
      70             :     virtual CStr GetName() const = 0;
      71             : 
      72             :     /**
      73             :      * The object that stores this setting.
      74             :      */
      75             :     IGUIObject& m_pObject;
      76             : };
      77             : 
      78             : /**
      79             :  * Wraps a T. Makes sure the appropriate setting functions are called when modifying T,
      80             :  * and likewise makes sure that JS/xml settings affect T appropriately,
      81             :  * while being as transparent as possible to use from C++ code.
      82             :  */
      83             : template<typename T>
      84          30 : class CGUISimpleSetting : public IGUISetting
      85             : {
      86             : public:
      87             :     template<typename... Args>
      88         165 :     CGUISimpleSetting(IGUIObject* pObject, const CStr& Name, Args&&... args)
      89         165 :         : IGUISetting(Name, pObject), m_Name(Name), m_Setting(args...)
      90         165 :     {}
      91          15 :     NONCOPYABLE(CGUISimpleSetting);
      92          15 :     MOVABLE(CGUISimpleSetting);
      93          15 : 
      94          30 :     operator const T&() const { return m_Setting; }
      95          30 :     const T& operator*() const { return m_Setting; }
      96          30 :     const T* operator->() const { return &m_Setting; }
      97          15 : 
      98          15 :     /**
      99          15 :      * 'Uglified' getter when you want direct access without triggering messages.
     100          60 :      */
     101          60 :     T& GetMutable() { return m_Setting; }
     102          60 : 
     103          45 :     /**
     104          45 :      * 'Uglified' operator=, so that SendMessage is explicit.
     105          45 :      */
     106           0 :     void Set(T value, bool sendMessage)
     107           0 :     {
     108           4 :         m_Setting = std::move(value);
     109           4 :         OnSettingChange(m_Name, sendMessage);
     110           0 :     }
     111           4 : 
     112           0 : protected:
     113           0 :     CStr GetName() const override
     114           0 :     {
     115           0 :         return m_Name;
     116           0 :     }
     117           0 : 
     118           0 :     bool DoFromString(const CStrW& value) override;
     119           0 :     bool DoFromJSVal(const ScriptRequest& rq, JS::HandleValue value) override;
     120           0 :     void ToJSVal(const ScriptRequest& rq, JS::MutableHandleValue value) override;
     121           0 : 
     122           0 :     const CStr m_Name;
     123           0 :     T m_Setting;
     124           0 : };
     125           0 : 
     126           0 : #endif // INCLUDED_CGUISETTINGS

Generated by: LCOV version 1.13