Cafu Engine
ConVar.hpp
1 /*
2 Cafu Engine, http://www.cafu.de/
3 Copyright (c) Carsten Fuchs and other contributors.
4 This project is licensed under the terms of the MIT license.
5 */
6 
7 /*************************/
8 /*** Console Variables ***/
9 /*************************/
10 
11 #ifndef CAFU_CONSOLE_VARIABLES_HPP_INCLUDED
12 #define CAFU_CONSOLE_VARIABLES_HPP_INCLUDED
13 
14 #include <string>
15 #include "Templates/Array.hpp"
16 
17 #undef Bool
18 
19 
20 // TODO: - Argument completion callbacks.
21 
22 /// This class implements Console Variables ("convars").
23 /// A convar is a special variable that can be modified by the user at the game console and is globally accessible in the entire application.
24 ///
25 /// In user code, a convar can be declared as a global variable or as a local, static variable.
26 /// Use as a non-static local variable or on the heap (with new and delete) is also possible,
27 /// but comes at a performance cost and is against the (gobal) nature of convars.
28 /// Such a local variable would also never be visible at the console, and thus it makes no sense to have it.
29 ///
30 /// a) Before ConVarT::RegisterStaticList() is called, all instantiated convars register themselves in a list that is global to the module,
31 /// that is, the exe and each dll has its own list.
32 /// b) Then, the ConsoleInterpreter pointer is set to point to the implementation of the ConsoleInterpreterI in the exe.
33 /// (There is only exactly one "global" singleton ConsoleInterpreterI instance for the entire application.)
34 /// c) Calling ConVarT::RegisterStaticList() then iterates over all (pointers to) convars in the per-module list,
35 /// and for each tries to find a convar with the same name in the unique, application-global list inside the ConsoleInterpreter.
36 /// d) Convars that are instantiated after the call to ConVarT::RegisterStaticList(), e.g. local static variables that are reached by the
37 /// control-flow for the first time, automatically register themselves with the ConsoleInterpreter directly, rather than attempting to
38 /// add themselves to the obsolete list from step a).
39 class ConVarT
40 {
41  public:
42 
43  enum TypeT { String, Integer, Bool, Double };
44 
45  enum FlagT
46  {
47  FLAG_ALL = -1,
48 
49  FLAG_MAIN_EXE = 0x0001,
50  FLAG_MATSYS = 0x0002,
51  FLAG_SOUNDSYS = 0x0004,
52  FLAG_GAMEDLL = 0x0008,
53 
54  FLAG_USERINFO = 0x0010, ///< Sent from clients to the server.
55  FLAG_SERVERINFO = 0x0020, ///< Sent from servers to the clients.
56  FLAG_NETWORK_SYNC = 0x0040, ///< Value of this variable is sync'ed from the server to the clients.
57  FLAG_CHEAT = 0x0080, ///< Use of this variable is considered a cheat.
58 
59  FLAG_FOR_INIT = 0x0100, ///< This variable is used during initialization, and can thus only be changed at the command-line, not at the console.
60  FLAG_READ_ONLY = 0x0200, ///< This variable is read-only, i.e. it cannot be modified by the user (neither in console nor at command-line).
61  FLAG_BY_CODE = 0x0400, ///< This variable has been statically declared by code, not by a user via the "set var xy" command.
62  FLAG_PERSISTENT = 0x0800 ///< This variable (and its value) is archived in a config file.
63  };
64 
65 
66  // Constructors.
67  ConVarT(const std::string& Name_, const std::string& Value_, const int Flags_, const std::string& Description_, const char** AllowedValues_=NULL);
68  ConVarT(const std::string& Name_, const char* Value_, const int Flags_, const std::string& Description_, const char** AllowedValues_=NULL); // This constructor is required for cases when "" is given for Value_, which the VC++ 2005 compiler seems to cast and match to the "bool" constructor...
69  ConVarT(const std::string& Name_, const int Value_, const int Flags_, const std::string& Description_, const int MinValue_= 1, const int MaxValue_= -1);
70  ConVarT(const std::string& Name_, const bool Value_, const int Flags_, const std::string& Description_);
71  ConVarT(const std::string& Name_, const double Value_, const int Flags_, const std::string& Description_, const double MinValue_=1.0, const double MaxValue_=-1.0);
72 
73  // Destructor.
74  ~ConVarT();
75 
76  // Get methods.
77  const std::string& GetName() const { return Name; }
78  const std::string& GetDescription() const { return Description; }
79  int GetFlags() const { return Flags; }
80  TypeT GetType() const { return Type; }
81  const std::string& GetValueString() const { assert(Type==String); return ValueString; }
82  int GetValueInt() const { assert(Type==Integer); return ValueInt; }
83  bool GetValueBool() const { assert(Type==Bool); return ValueInt!=0; }
84  double GetValueDouble() const { assert(Type==Double); return ValueDouble; }
85 
86  // Set methods.
87  void SetValue(const std::string& Value_);
88  void SetValue(const char* Value_); // Without this, literal strings are cast to bool rather than std::string.
89  void SetValue(const int Value_);
90  void SetValue(const bool Value_);
91  void SetValue(const double Value_);
92 
93  void operator = (const std::string& Value_) { SetValue(Value_); }
94  void operator = (const char* Value_) { SetValue(Value_); }
95  void operator = (const int Value_) { SetValue(Value_); }
96  void operator = (const bool Value_) { SetValue(Value_); }
97  void operator = (const double Value_) { SetValue(Value_); }
98 
99  // Special methods for dealing with the IsModified flag.
100  bool HasBeenModified() const { return IsModified; }
101  // void SetModified() { IsModified=true; } // Should probably be private, but instead I wrote IsModified=true; everywhere directly.
102  void ClearModified() { IsModified=false; }
103 
104 
105  /// Registers all convars in the StaticList with the ConsoleInterpreter and invalidates the StaticList.
106  /// This has to be called after the ConsoleInterpreter pointer is set when the dll is first initialized!
107  /// The ctors of all convars that are instantiated after this call (those that are declared as local (static) variables)
108  /// then automatically register themselves with the ConsoleInterpreter rather than with the StaticList.
109  static void RegisterStaticList();
110 
111 
112  private:
113 
114  /// Give the implementation of the console interpreter full access.
116 
117  const std::string Name; ///< The name of this console variable.
118  const std::string Description; ///< The description (i.e. user help text) for this console variable.
119  const int Flags; ///< The flags for this convar.
120  const TypeT Type; ///< The type of this convar. Cannot be changed later.
121  std::string ValueString; ///< The value of this convar if it is of type String.
122  int ValueInt; ///< The value of this convar if it is of type Integer or Bool.
123  double ValueDouble; ///< The value of this convar if it is of type Double.
124  const double MinValue; ///< The minimum value of this convar if Type is Double or Integer.
125  const double MaxValue; ///< The maximum value of this convar if Type is Double or Integer.
126  const ArrayT<std::string> AllowedValues; ///< The list of allowed values if Type is String.
127  bool IsModified; ///< Whether this variable has been modified. Set by the SetValue() methods.
128 };
129 
130 #endif
This variable has been statically declared by code, not by a user via the "set var xy" command...
Definition: ConVar.hpp:61
This variable (and its value) is archived in a config file.
Definition: ConVar.hpp:62
This class implements Console Variables ("convars").
Definition: ConVar.hpp:39
Use of this variable is considered a cheat.
Definition: ConVar.hpp:57
This class provides an implementation for the ConsoleInterpreterI interface.
Definition: ConsoleInterpreterImpl.hpp:33
Value of this variable is sync'ed from the server to the clients.
Definition: ConVar.hpp:56
Sent from clients to the server.
Definition: ConVar.hpp:54
static void RegisterStaticList()
Registers all convars in the StaticList with the ConsoleInterpreter and invalidates the StaticList...
Definition: ConVar.cpp:30
This variable is read-only, i.e. it cannot be modified by the user (neither in console nor at command...
Definition: ConVar.hpp:60
FlagT
Definition: ConVar.hpp:45
Sent from servers to the clients.
Definition: ConVar.hpp:55
This variable is used during initialization, and can thus only be changed at the command-line, not at the console.
Definition: ConVar.hpp:59