Panda3D
 All Classes Functions Variables Enumerations
configVariableEnum.I
1 // Filename: configVariableEnum.I
2 // Created by: drose (21Oct04)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 
16 ////////////////////////////////////////////////////////////////////
17 // Function: ConfigVariableEnum::Constructor
18 // Access: Published
19 // Description:
20 ////////////////////////////////////////////////////////////////////
21 template<class EnumType>
23 ConfigVariableEnum(const string &name, EnumType default_value,
24  const string &description, int flags) :
25 #ifdef PRC_SAVE_DESCRIPTIONS
26  ConfigVariable(name, ConfigVariableCore::VT_enum, description, flags),
27 #else
28  ConfigVariable(name, ConfigVariableCore::VT_enum, string(), flags),
29 #endif
30  _got_default_value(true),
31  _default_value(default_value),
32  _local_modified(initial_invalid_cache())
33 {
34  _core->set_default_value(format_enum(default_value));
35  _core->set_used();
36 }
37 
38 ////////////////////////////////////////////////////////////////////
39 // Function: ConfigVariableEnum::Constructor
40 // Access: Published
41 // Description:
42 ////////////////////////////////////////////////////////////////////
43 template<class EnumType>
45 ConfigVariableEnum(const string &name, const string &default_value,
46  const string &description, int flags) :
47 #ifdef PRC_SAVE_DESCRIPTIONS
48  ConfigVariable(name, ConfigVariableCore::VT_enum, description, flags),
49 #else
50  ConfigVariable(name, ConfigVariableCore::VT_enum, string(), flags),
51 #endif
52  _got_default_value(true),
53  _default_value(parse_string(default_value)),
54  _local_modified(initial_invalid_cache())
55 {
56  _core->set_default_value(default_value);
57  _core->set_used();
58 }
59 
60 ////////////////////////////////////////////////////////////////////
61 // Function: ConfigVariableEnum::Destructor
62 // Access: Public
63 // Description:
64 ////////////////////////////////////////////////////////////////////
65 template<class EnumType>
68 }
69 
70 ////////////////////////////////////////////////////////////////////
71 // Function: ConfigVariableEnum::operator =
72 // Access: Public
73 // Description: Reassigns the variable's local value.
74 ////////////////////////////////////////////////////////////////////
75 template<class EnumType>
77 operator = (EnumType value) {
78  set_value(value);
79 }
80 
81 ////////////////////////////////////////////////////////////////////
82 // Function: ConfigVariableEnum::typecast operator
83 // Access: Public
84 // Description: Returns the variable's value.
85 ////////////////////////////////////////////////////////////////////
86 template<class EnumType>
88 operator EnumType () const {
89  return get_value();
90 }
91 
92 ////////////////////////////////////////////////////////////////////
93 // Function: ConfigVariableEnum::size()
94 // Access: Public
95 // Description: Returns the number of unique words in the variable.
96 ////////////////////////////////////////////////////////////////////
97 template<class EnumType>
99 size() const {
100  return get_num_words();
101 }
102 
103 ////////////////////////////////////////////////////////////////////
104 // Function: ConfigVariableEnum::operator []
105 // Access: Public
106 // Description: Returns the value of the variable's nth word.
107 ////////////////////////////////////////////////////////////////////
108 template<class EnumType>
109 INLINE EnumType ConfigVariableEnum<EnumType>::
110 operator [] (int n) const {
111  return get_word(n);
112 }
113 
114 ////////////////////////////////////////////////////////////////////
115 // Function: ConfigVariableEnum::set_value
116 // Access: Public
117 // Description: Reassigns the variable's local value.
118 ////////////////////////////////////////////////////////////////////
119 template<class EnumType>
121 set_value(EnumType value) {
122  set_string_value(format_enum(value));
123 }
124 
125 ////////////////////////////////////////////////////////////////////
126 // Function: ConfigVariableEnum::get_value
127 // Access: Public
128 // Description: Returns the variable's value.
129 ////////////////////////////////////////////////////////////////////
130 template<class EnumType>
131 INLINE EnumType ConfigVariableEnum<EnumType>::
132 get_value() const {
133  TAU_PROFILE("EnumType ConfigVariableEnum<EnumType>::get_value() const", " ", TAU_USER);
134  if (!is_cache_valid(_local_modified)) {
135  mark_cache_valid(((ConfigVariableEnum<EnumType> *)this)->_local_modified);
136  ((ConfigVariableEnum<EnumType> *)this)->_cache = (EnumType)parse_string(get_string_value());
137  }
138  return _cache;
139 }
140 
141 ////////////////////////////////////////////////////////////////////
142 // Function: ConfigVariableEnum::get_default_value
143 // Access: Public
144 // Description: Returns the variable's default value.
145 ////////////////////////////////////////////////////////////////////
146 template<class EnumType>
147 INLINE EnumType ConfigVariableEnum<EnumType>::
149  if (!_got_default_value) {
151  if (decl != (ConfigDeclaration *)NULL) {
152  ((ConfigVariableEnum<EnumType> *)this)->_default_value = (EnumType)parse_string(decl->get_string_value());
153  ((ConfigVariableEnum<EnumType> *)this)->_got_default_value = true;
154  }
155  }
156  return _default_value;
157 }
158 
159 ////////////////////////////////////////////////////////////////////
160 // Function: ConfigVariableEnum::get_word
161 // Access: Public
162 // Description: Returns the variable's nth value.
163 ////////////////////////////////////////////////////////////////////
164 template<class EnumType>
165 INLINE EnumType ConfigVariableEnum<EnumType>::
166 get_word(int n) const {
167  return (EnumType)parse_string(get_string_word(n));
168 }
169 
170 ////////////////////////////////////////////////////////////////////
171 // Function: ConfigVariableEnum::set_word
172 // Access: Public
173 // Description: Reassigns the variable's nth value. This makes a
174 // local copy of the variable's overall value.
175 ////////////////////////////////////////////////////////////////////
176 template<class EnumType>
178 set_word(int n, EnumType value) {
179  set_string_word(n, format_enum(value));
180 }
181 
182 ////////////////////////////////////////////////////////////////////
183 // Function: ConfigVariableEnum::parse_string
184 // Access: Public, Virtual
185 // Description: Turns the string value into a value of the enumerated
186 // type by invoking its predefined operator >> (istream)
187 // operator.
188 ////////////////////////////////////////////////////////////////////
189 template<class EnumType>
190 INLINE EnumType ConfigVariableEnum<EnumType>::
191 parse_string(const string &value) const {
192  istringstream strm(value);
193  EnumType result;
194  strm >> result;
195  return result;
196 }
197 
198 ////////////////////////////////////////////////////////////////////
199 // Function: ConfigVariableEnum::format_enum
200 // Access: Public, Virtual
201 // Description: The format_enum() method assumes the enumerated type
202 // has a valid operator << (ostream) defined, which
203 // balances against the operator >> (istream) operator.
204 ////////////////////////////////////////////////////////////////////
205 template<class EnumType>
206 INLINE string ConfigVariableEnum<EnumType>::
207 format_enum(EnumType value) const {
208  ostringstream strm;
209  strm << value;
210  return strm.str();
211 }
EnumType get_default_value() const
Returns the variable&#39;s default value.
The internal definition of a ConfigVariable.
const string & get_string_value() const
Returns the value assigned to this variable.
EnumType get_word(int n) const
Returns the variable&#39;s nth value.
void operator=(EnumType value)
Reassigns the variable&#39;s local value.
void set_word(int n, EnumType value)
Reassigns the variable&#39;s nth value.
const ConfigDeclaration * get_default_value() const
Returns the default variable specified for this variable.
EnumType operator[](int n) const
Returns the value of the variable&#39;s nth word.
void set_value(EnumType value)
Reassigns the variable&#39;s local value.
This is a generic, untyped ConfigVariable.
int size() const
Returns the number of unique words in the variable.
This class specializes ConfigVariable as an enumerated type.
EnumType get_value() const
Returns the variable&#39;s value.
A single declaration of a config variable, typically defined as one line in a .prc file...