Panda3D
configVariableEnum.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 configVariableEnum.I
10  * @author drose
11  * @date 2004-10-21
12  */
13 
14 /**
15  *
16  */
17 template<class EnumType>
19 ConfigVariableEnum(const std::string &name, EnumType default_value,
20  const std::string &description, int flags) :
21 #ifdef PRC_SAVE_DESCRIPTIONS
22  ConfigVariable(name, ConfigVariableCore::VT_enum, description, flags),
23 #else
24  ConfigVariable(name, ConfigVariableCore::VT_enum, std::string(), flags),
25 #endif
26  _got_default_value(true),
27  _default_value(default_value),
28  _local_modified(initial_invalid_cache())
29 {
30  _core->set_default_value(format_enum(default_value));
31  _core->set_used();
32 }
33 
34 /**
35  *
36  */
37 template<class EnumType>
39 ConfigVariableEnum(const std::string &name, const std::string &default_value,
40  const std::string &description, int flags) :
41 #ifdef PRC_SAVE_DESCRIPTIONS
42  ConfigVariable(name, ConfigVariableCore::VT_enum, description, flags),
43 #else
44  ConfigVariable(name, ConfigVariableCore::VT_enum, std::string(), flags),
45 #endif
46  _got_default_value(true),
47  _default_value(parse_string(default_value)),
48  _local_modified(initial_invalid_cache())
49 {
50  _core->set_default_value(default_value);
51  _core->set_used();
52 }
53 
54 /**
55  *
56  */
57 template<class EnumType>
60 }
61 
62 /**
63  * Reassigns the variable's local value.
64  */
65 template<class EnumType>
67 operator = (EnumType value) {
68  set_value(value);
69 }
70 
71 /**
72  * Returns the variable's value.
73  */
74 template<class EnumType>
76 operator EnumType () const {
77  return get_value();
78 }
79 
80 /**
81  * Returns the number of unique words in the variable.
82  */
83 template<class EnumType>
85 size() const {
86  return get_num_words();
87 }
88 
89 /**
90  * Returns the value of the variable's nth word.
91  */
92 template<class EnumType>
93 INLINE EnumType ConfigVariableEnum<EnumType>::
94 operator [] (size_t n) const {
95  return get_word(n);
96 }
97 
98 /**
99  * Reassigns the variable's local value.
100  */
101 template<class EnumType>
103 set_value(EnumType value) {
104  set_string_value(format_enum(value));
105 }
106 
107 /**
108  * Returns the variable's value.
109  */
110 template<class EnumType>
111 INLINE EnumType ConfigVariableEnum<EnumType>::
112 get_value() const {
113  TAU_PROFILE("EnumType ConfigVariableEnum<EnumType>::get_value() const", " ", TAU_USER);
114  if (!is_cache_valid(_local_modified)) {
115  mark_cache_valid(((ConfigVariableEnum<EnumType> *)this)->_local_modified);
116  ((ConfigVariableEnum<EnumType> *)this)->_cache = (EnumType)parse_string(get_string_value());
117  }
118  return _cache;
119 }
120 
121 /**
122  * Returns the variable's default value.
123  */
124 template<class EnumType>
125 INLINE EnumType ConfigVariableEnum<EnumType>::
126 get_default_value() const {
127  if (!_got_default_value) {
128  const ConfigDeclaration *decl = ConfigVariable::get_default_value();
129  if (decl != nullptr) {
130  ((ConfigVariableEnum<EnumType> *)this)->_default_value = (EnumType)parse_string(decl->get_string_value());
131  ((ConfigVariableEnum<EnumType> *)this)->_got_default_value = true;
132  }
133  }
134  return _default_value;
135 }
136 
137 /**
138  * Returns the variable's nth value.
139  */
140 template<class EnumType>
141 INLINE EnumType ConfigVariableEnum<EnumType>::
142 get_word(size_t n) const {
143  return (EnumType)parse_string(get_string_word(n));
144 }
145 
146 /**
147  * Reassigns the variable's nth value. This makes a local copy of the
148  * variable's overall value.
149  */
150 template<class EnumType>
152 set_word(size_t n, EnumType value) {
153  set_string_word(n, format_enum(value));
154 }
155 
156 /**
157  * Turns the string value into a value of the enumerated type by invoking its
158  * predefined operator >> (istream) operator.
159  */
160 template<class EnumType>
161 INLINE EnumType ConfigVariableEnum<EnumType>::
162 parse_string(const std::string &value) const {
163  std::istringstream strm(value);
164  EnumType result;
165  strm >> result;
166  return result;
167 }
168 
169 /**
170  * The format_enum() method assumes the enumerated type has a valid operator
171  * << (ostream) defined, which balances against the operator >> (istream)
172  * operator.
173  */
174 template<class EnumType>
175 INLINE std::string ConfigVariableEnum<EnumType>::
176 format_enum(EnumType value) const {
177  std::ostringstream strm;
178  strm << value;
179  return strm.str();
180 }
const std::string & get_string_value() const
Returns the value assigned to this variable.
The internal definition of a ConfigVariable.
size_t size() const
Returns the number of unique words in the variable.
void operator=(EnumType value)
Reassigns the variable's local value.
This is a generic, untyped ConfigVariable.
This class specializes ConfigVariable as an enumerated type.
void set_word(size_t n, EnumType value)
Reassigns the variable's nth value.
EnumType operator [](size_t n) const
Returns the value of the variable's nth word.
EnumType get_word(size_t n) const
Returns the variable's nth value.
A single declaration of a config variable, typically defined as one line in a .prc file,...
set_value
Reassigns the variable's local value.