/* * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or * its licensors. * * For complete copyright and license terms please see the LICENSE at the root of this * distribution (the "License"). All use of this software is governed by the License, * or, if provided, by the license below or the license accompanying this file. Do not * remove or modify any license notices. This file is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * */ // Original file Copyright Crytek GMBH or its affiliates, used under license. #pragma once #ifndef CRYINCLUDE_EDITOR_CONFIGGROUP_H #define CRYINCLUDE_EDITOR_CONFIGGROUP_H namespace Config { // Abstract configurable variable struct IConfigVar { public: enum EType { eType_BOOL, eType_INT, eType_FLOAT, eType_STRING, }; enum EFlags { eFlag_NoUI = 1 << 0, eFlag_NoCVar = 1 << 1, eFlag_DoNotSave = 1 << 2, }; IConfigVar(const char* szName, const char* szDescription, EType varType, uint8 flags) : m_name(szName) , m_description(szDescription) , m_type(varType) , m_flags(flags) , m_ptr(NULL) {}; virtual ~IConfigVar() = default; ILINE EType GetType() const { return m_type; } ILINE const string& GetName() const { return m_name; } ILINE const string& GetDescription() const { return m_description; } ILINE bool IsFlagSet(EFlags flag) const { return 0 != (m_flags & flag); } virtual void Get(void* outPtr) const = 0; virtual void Set(const void* ptr) = 0; virtual bool IsDefault() const = 0; virtual void GetDefault(void* outPtr) const = 0; virtual void Reset() = 0; static EType TranslateType(const bool&) { return eType_BOOL; } static EType TranslateType(const int&) { return eType_INT; } static EType TranslateType(const float&) { return eType_FLOAT; } static EType TranslateType(const string&) { return eType_STRING; } protected: EType m_type; uint8 m_flags; string m_name; string m_description; void* m_ptr; ICVar* m_pCVar; }; // Typed wrapper for config variable template class TConfigVar : public IConfigVar { private: T m_default; public: TConfigVar(const char* szName, const char* szDescription, uint8 flags, T& ptr, const T& defaultValue) : IConfigVar(szName, szDescription, IConfigVar::TranslateType(ptr), flags) , m_default(defaultValue) { m_ptr = &ptr; // reset to default value on initializations ptr = defaultValue; } virtual void Get(void* outPtr) const { *reinterpret_cast(outPtr) = *reinterpret_cast(m_ptr); } virtual void Set(const void* ptr) { *reinterpret_cast(m_ptr) = *reinterpret_cast(ptr); } virtual void Reset() { *reinterpret_cast(m_ptr) = m_default; } virtual void GetDefault(void* outPtr) const { *reinterpret_cast(outPtr) = m_default; } virtual bool IsDefault() const { return *reinterpret_cast(m_ptr) == m_default; } }; // Group of configuration variables with optional mapping to CVars class CConfigGroup { private: typedef std::vector TConfigVariables; TConfigVariables m_vars; typedef std::vector TConsoleVariables; TConsoleVariables m_consoleVars; public: CConfigGroup(); virtual ~CConfigGroup(); void AddVar(IConfigVar* var); uint32 GetVarCount(); IConfigVar* GetVar(const char* szName); IConfigVar* GetVar(uint index); const IConfigVar* GetVar(const char* szName) const; const IConfigVar* GetVar(uint index) const; void SaveToXML(XmlNodeRef node); void LoadFromXML(XmlNodeRef node); template void AddVar(const char* szName, const char* szDescription, T& var, const T& defaultValue, uint8 flags = 0) { AddVar(new TConfigVar(szName, szDescription, flags, var, defaultValue)); } }; }; #endif // CRYINCLUDE_EDITOR_CONFIGGROUP_H