RigsofRods
Soft-body Physics Simulation
CVar.h
Go to the documentation of this file.
1 /*
2  This source file is part of Rigs of Rods
3  Copyright 2005-2012 Pierre-Michel Ricordel
4  Copyright 2007-2012 Thomas Fischer
5  Copyright 2013-2020 Petr Ohlidal
6 
7  For more information, see http://www.rigsofrods.org/
8 
9  Rigs of Rods is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License version 3, as
11  published by the Free Software Foundation.
12 
13  Rigs of Rods is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
23 
24 #pragma once
25 
26 #include "BitFlags.h"
27 
28 #include <Ogre.h>
29 #include <string>
30 
31 namespace RoR {
32 
35 
37 {
43 };
44 
52 class CVar
53 {
54 public:
55 
56  CVar(std::string const& name, std::string const& long_name, int flags):
57  m_name(name),
58  m_long_name(long_name),
59  m_flags(flags),
60  m_value_num(0.f)
61  {
62  // Initialize string representation
64  {
66  }
67  }
68 
69  // Data setters
70 
71  template <typename T>
72  void setVal(T val)
73  {
74  if ((T)m_value_num != val)
75  {
76  std::string str = this->convertStr((float)val);
77  this->logUpdate(str);
78  m_value_num = (float)val;
79  m_value_str = str;
80  }
81  }
82 
83  void setStr(std::string const& str)
84  {
85  if (m_value_str != str)
86  {
87  this->logUpdate(str);
88  m_value_num = 0;
89  m_value_str = str;
90  }
91  }
92 
93  // Data getters
94 
95  std::string const& getStr() const { return m_value_str; }
96  float getFloat() const { return m_value_num; }
97  int getInt() const { return (int)m_value_num; }
98  bool getBool() const { return (bool)m_value_num; }
99  template<typename T> T getEnum() const { return (T)this->getInt(); }
100 
101  // Info getters
102 
103  std::string const& getName() const { return m_name; }
104  std::string const& getLongName() const { return m_long_name; }
105  bool hasFlag(int f) const { return m_flags & f; }
106 
107 private:
108  void logUpdate(std::string const& new_val);
109  std::string convertStr(float val);
110 
111  // Variables
112 
113  std::string m_name;
114  std::string m_long_name;
115 
116  std::string m_value_str;
117  float m_value_num;
118  int m_flags;
119 };
120 
122 
123 } // namespace RoR
RoR::CVAR_NO_LOG
@ CVAR_NO_LOG
Will not be written to RoR.log.
Definition: CVar.h:42
RoR::CVar::m_name
std::string m_name
Definition: CVar.h:113
RoR::CVar::getBool
bool getBool() const
Definition: CVar.h:98
RoR::CVar::logUpdate
void logUpdate(std::string const &new_val)
Definition: CVar.cpp:287
BITMASK
#define BITMASK(OFFSET)
Definition: BitFlags.h:10
RoR::CVar::m_value_str
std::string m_value_str
Definition: CVar.h:116
RoR::CVAR_ARCHIVE
@ CVAR_ARCHIVE
Will be written to RoR.cfg.
Definition: CVar.h:41
RoR::CVar::convertStr
std::string convertStr(float val)
Definition: CVar.cpp:299
RoR::CVar::m_flags
int m_flags
Definition: CVar.h:118
BitFlags.h
Bit operations.
RoR::CVar::getStr
std::string const & getStr() const
Definition: CVar.h:95
RoR::CVar::CVar
CVar(std::string const &name, std::string const &long_name, int flags)
Definition: CVar.h:56
RoR::CVar::hasFlag
bool hasFlag(int f) const
Definition: CVar.h:105
RoR::CVar::getEnum
T getEnum() const
Definition: CVar.h:99
RoR::CVar::m_long_name
std::string m_long_name
Definition: CVar.h:114
RoR::CVarFlags
CVarFlags
Definition: CVar.h:36
RoR::CVar::m_value_num
float m_value_num
Definition: CVar.h:117
RoR::CVar
Quake-style console variable, defined in RoR.cfg or crated via Console UI and scripts.
Definition: CVar.h:52
RoR::CVar::getName
std::string const & getName() const
Definition: CVar.h:103
RoR::CVar::setVal
void setVal(T val)
Definition: CVar.h:72
RoR::CVAR_TYPE_BOOL
@ CVAR_TYPE_BOOL
Definition: CVar.h:38
RoR::CVar::getLongName
std::string const & getLongName() const
Definition: CVar.h:104
RoR::CVAR_TYPE_INT
@ CVAR_TYPE_INT
Definition: CVar.h:39
RoR::CVar::getFloat
float getFloat() const
Definition: CVar.h:96
RoR::CVar::getInt
int getInt() const
Definition: CVar.h:97
RoR::CVAR_TYPE_FLOAT
@ CVAR_TYPE_FLOAT
Definition: CVar.h:40
RoR
Definition: AppContext.h:36
RoR::CVar::setStr
void setStr(std::string const &str)
Definition: CVar.h:83