Panda3D
configVariable.I
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file configVariable.I
10  * @author drose
11  * @date 2004-10-18
12  */
13 
14 /**
15  * This constructor is only intended to be called from a specialized
16  * ConfigVariableFoo derived class.
17  */
18 INLINE ConfigVariable::
19 ConfigVariable(const std::string &name, ConfigVariable::ValueType value_type) :
20  ConfigVariableBase(name, value_type)
21 {
22 }
23 
24 /**
25  * This constructor is only intended to be called from a specialized
26  * ConfigVariableFoo derived class.
27  */
28 INLINE ConfigVariable::
29 ConfigVariable(const std::string &name, ConfigVariable::ValueType value_type,
30  const std::string &description, int flags) :
31  ConfigVariableBase(name, value_type, description, flags)
32 {
33 }
34 
35 /**
36  * Use this constructor to make a ConfigVariable of an unspecified type.
37  * Usually you'd want to do this just to reference a previously-defined
38  * ConfigVariable of a specific type, without having to know what type it is.
39  */
40 INLINE ConfigVariable::
41 ConfigVariable(const std::string &name) :
42  ConfigVariableBase(name, VT_undefined)
43 {
44  _core->set_used();
45 }
46 
47 /**
48  *
49  */
50 INLINE ConfigVariable::
51 ~ConfigVariable() {
52 }
53 
54 /**
55  * Returns the default variable specified for this variable. If the variable
56  * has not yet been defined, this will return NULL.
57  */
58 INLINE const ConfigDeclaration *ConfigVariable::
59 get_default_value() const {
60  nassertr(is_constructed(), nullptr);
61  return _core->get_default_value();
62 }
63 
64 /**
65  * Returns the toplevel value of the variable, formatted as a string.
66  */
67 INLINE const std::string &ConfigVariable::
69  nassertr(is_constructed(), *new std::string());
70  const ConfigDeclaration *decl = _core->get_declaration(0);
71  return decl->get_string_value();
72 }
73 
74 /**
75  * Changes the value assigned to this variable. This creates a local value
76  * that shadows any values defined in the .prc files, until
77  * clear_local_value() is called.
78  */
79 INLINE void ConfigVariable::
80 set_string_value(const std::string &string_value) {
81  nassertv(is_constructed());
82  _core->make_local_value()->set_string_value(string_value);
83 }
84 
85 /**
86  * Removes the value assigned to this variable, and lets its original value
87  * (as read from the prc files) show through.
88  */
89 INLINE void ConfigVariable::
91  nassertv(is_constructed());
92  _core->clear_local_value();
93 }
94 
95 /**
96  * Returns the number of words in the variable's value. A word is defined as
97  * a sequence of non-whitespace characters delimited by whitespace.
98  */
99 INLINE size_t ConfigVariable::
100 get_num_words() const {
101  nassertr(is_constructed(), 0);
102  const ConfigDeclaration *decl = _core->get_declaration(0);
103  return decl->get_num_words();
104 }
105 
106 /**
107  * Returns true if the variable's value has a valid string value for the nth
108  * word. This is really the same thing as asking if there are at least n
109  * words in the value.
110  */
111 INLINE bool ConfigVariable::
112 has_string_word(size_t n) const {
113  nassertr(is_constructed(), false);
114  const ConfigDeclaration *decl = _core->get_declaration(0);
115  return decl->has_string_word(n);
116 }
117 
118 /**
119  * Returns true if the variable's value has a valid boolean value for the nth
120  * word.
121  */
122 INLINE bool ConfigVariable::
123 has_bool_word(size_t n) const {
124  nassertr(is_constructed(), false);
125  const ConfigDeclaration *decl = _core->get_declaration(0);
126  return decl->has_bool_word(n);
127 }
128 
129 /**
130  * Returns true if the variable's value has a valid integer value for the nth
131  * word.
132  */
133 INLINE bool ConfigVariable::
134 has_int_word(size_t n) const {
135  nassertr(is_constructed(), false);
136  const ConfigDeclaration *decl = _core->get_declaration(0);
137  return decl->has_int_word(n);
138 }
139 
140 /**
141  * Returns true if the variable's value has a valid 64-bit integer value for
142  * the nth word.
143  */
144 INLINE bool ConfigVariable::
145 has_int64_word(size_t n) const {
146  nassertr(is_constructed(), false);
147  const ConfigDeclaration *decl = _core->get_declaration(0);
148  return decl->has_int64_word(n);
149 }
150 
151 /**
152  * Returns true if the variable's value has a valid integer value for the nth
153  * word.
154  */
155 INLINE bool ConfigVariable::
156 has_double_word(size_t n) const {
157  nassertr(is_constructed(), false);
158  const ConfigDeclaration *decl = _core->get_declaration(0);
159  return decl->has_double_word(n);
160 }
161 
162 /**
163  * Returns the string value of the nth word of the variable's value, or empty
164  * string if there is no nth value. See also has_string_word().
165  */
166 INLINE std::string ConfigVariable::
167 get_string_word(size_t n) const {
168  nassertr(is_constructed(), std::string());
169  const ConfigDeclaration *decl = _core->get_declaration(0);
170  return decl->get_string_word(n);
171 }
172 
173 /**
174  * Returns the boolean value of the nth word of the variable's value, or false
175  * if there is no nth value. See also has_bool_word().
176  */
177 INLINE bool ConfigVariable::
178 get_bool_word(size_t n) const {
179  nassertr(is_constructed(), false);
180  const ConfigDeclaration *decl = _core->get_declaration(0);
181  return decl->get_bool_word(n);
182 }
183 
184 /**
185  * Returns the integer value of the nth word of the variable's value, or 0 if
186  * there is no nth value. See also has_int_word().
187  */
188 INLINE int ConfigVariable::
189 get_int_word(size_t n) const {
190  nassertr(is_constructed(), 0);
191  const ConfigDeclaration *decl = _core->get_declaration(0);
192  return decl->get_int_word(n);
193 }
194 
195 /**
196  * Returns the int64 value of the nth word of the variable's value, or 0 if
197  * there is no nth value. See also has_int_word().
198  */
199 INLINE int64_t ConfigVariable::
200 get_int64_word(size_t n) const {
201  nassertr(is_constructed(), 0);
202  const ConfigDeclaration *decl = _core->get_declaration(0);
203  return decl->get_int64_word(n);
204 }
205 
206 /**
207  * Returns the integer value of the nth word of the variable's value, or 0 if
208  * there is no nth value. See also has_double_word().
209  */
210 INLINE double ConfigVariable::
211 get_double_word(size_t n) const {
212  nassertr(is_constructed(), 0.0);
213  const ConfigDeclaration *decl = _core->get_declaration(0);
214  return decl->get_double_word(n);
215 }
216 
217 /**
218  * Changes the nth word to the indicated value without affecting the other
219  * words.
220  */
221 INLINE void ConfigVariable::
222 set_string_word(size_t n, const std::string &value) {
223  nassertv(is_constructed());
224  _core->make_local_value()->set_string_word(n, value);
225 }
226 
227 /**
228  * Changes the nth word to the indicated value without affecting the other
229  * words.
230  */
231 INLINE void ConfigVariable::
232 set_bool_word(size_t n, bool value) {
233  nassertv(is_constructed());
234  _core->make_local_value()->set_bool_word(n, value);
235 }
236 
237 /**
238  * Changes the nth word to the indicated value without affecting the other
239  * words.
240  */
241 INLINE void ConfigVariable::
242 set_int_word(size_t n, int value) {
243  nassertv(is_constructed());
244  _core->make_local_value()->set_int_word(n, value);
245 }
246 
247 /**
248  * Changes the nth word to the indicated value without affecting the other
249  * words.
250  */
251 INLINE void ConfigVariable::
252 set_int64_word(size_t n, int64_t value) {
253  nassertv(is_constructed());
254  _core->make_local_value()->set_int64_word(n, value);
255 }
256 
257 /**
258  * Changes the nth word to the indicated value without affecting the other
259  * words.
260  */
261 INLINE void ConfigVariable::
262 set_double_word(size_t n, double value) {
263  nassertv(is_constructed());
264  _core->make_local_value()->set_double_word(n, value);
265 }
266 
267 /**
268  * Returns true if the constructor has been called and _core initialized,
269  * false if the constructor has not yet been called and _core is NULL. This
270  * is intended to be placed in an assertion check, to guard against static-
271  * init ordering issues.
272  */
273 INLINE bool ConfigVariable::
274 is_constructed() const {
275 #ifndef NDEBUG
276  if (_core == nullptr) {
277  report_unconstructed();
278  return false;
279  }
280 #endif
281  return true;
282 }
double get_double_word(size_t n) const
Returns the integer value of the nth word of the declaration's value, or 0 if there is no nth value.
const std::string & get_string_value() const
Returns the value assigned to this variable.
bool has_double_word(size_t n) const
Returns true if the declaration's value has a valid integer value for the nth word.
void set_string_value(const std::string &value)
Changes the value assigned to this variable.
std::string get_string_word(size_t n) const
Returns the string value of the nth word of the declaration's value, or empty string if there is no n...
bool get_bool_word(size_t n) const
Returns the boolean value of the nth word of the declaration's value, or false if there is no nth val...
size_t get_num_words() const
Returns the number of words in the variable's value.
int64_t get_int64_word(size_t n) const
Returns the int64 value of the nth word of the declaration's value, or 0 if there is no nth value.
int get_int_word(size_t n) const
Returns the integer value of the nth word of the declaration's value, or 0 if there is no nth value.
bool has_int_word(size_t n) const
Returns true if the declaration's value has a valid integer value for the nth word.
bool has_bool_word(size_t n) const
Returns true if the declaration's value has a valid boolean value for the nth word.
const std::string & get_string_value() const
Returns the toplevel value of the variable, formatted as a string.
void clear_value()
Removes the value assigned to this variable, and lets its original value (as read from the prc files)...
size_t get_num_words() const
Returns the number of words in the declaration's value.
bool has_string_word(size_t n) const
Returns true if the declaration's value has a valid string value for the nth word.
bool has_int64_word(size_t n) const
Returns true if the declaration's value has a valid int64 value for the nth word.
This class is the base class for both ConfigVariableList and ConfigVariable (and hence for all of the...
A single declaration of a config variable, typically defined as one line in a .prc file,...