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: 11 16 68.8 %
Date: 2023-01-19 00:18:29 Functions: 15 145 10.3 %

          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         176 :     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         176 : class CGUISimpleSetting : public IGUISetting
      85             : {
      86             : public:
      87             :     template<typename... Args>
      88         176 :     CGUISimpleSetting(IGUIObject* pObject, const CStr& Name, Args&&... args)
      89         176 :         : IGUISetting(Name, pObject), m_Name(Name), m_Setting(args...)
      90         176 :     {}
      91             :     NONCOPYABLE(CGUISimpleSetting);
      92           0 :     MOVABLE(CGUISimpleSetting);
      93             : 
      94          52 :     operator const T&() const { return m_Setting; }
      95           0 :     const T& operator*() const { return m_Setting; }
      96           4 :     const T* operator->() const { return &m_Setting; }
      97             : 
      98             :     /**
      99             :      * 'Uglified' getter when you want direct access without triggering messages.
     100             :      */
     101           0 :     T& GetMutable() { return m_Setting; }
     102             : 
     103             :     /**
     104             :      * 'Uglified' operator=, so that SendMessage is explicit.
     105             :      */
     106           4 :     void Set(T value, bool sendMessage)
     107             :     {
     108           4 :         m_Setting = std::move(value);
     109           4 :         OnSettingChange(m_Name, sendMessage);
     110           4 :     }
     111             : 
     112             : protected:
     113           0 :     CStr GetName() const override
     114             :     {
     115           0 :         return m_Name;
     116             :     }
     117             : 
     118             :     bool DoFromString(const CStrW& value) override;
     119             :     bool DoFromJSVal(const ScriptRequest& rq, JS::HandleValue value) override;
     120             :     void ToJSVal(const ScriptRequest& rq, JS::MutableHandleValue value) override;
     121             : 
     122             :     const CStr m_Name;
     123             :     T m_Setting;
     124             : };
     125             : 
     126             : #endif // INCLUDED_CGUISETTINGS

Generated by: LCOV version 1.13