Panda3D
Loading...
Searching...
No Matches
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 */
17template<class EnumType>
19ConfigVariableEnum(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 */
37template<class EnumType>
39ConfigVariableEnum(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}
54/**
55 *
56 */
57template<class EnumType>
60}
61
62/**
63 * Reassigns the variable's local value.
64 */
65template<class EnumType>
67operator = (EnumType value) {
68 set_value(value);
69}
70
71/**
72 * Returns the variable's value.
73 */
74template<class EnumType>
76operator EnumType () const {
77 return get_value();
78}
79
80/**
81 * Returns the number of unique words in the variable.
82 */
83template<class EnumType>
85size() const {
86 return get_num_words();
87}
88
89/**
90 * Returns the value of the variable's nth word.
91 */
92template<class EnumType>
94operator [] (size_t n) const {
95 return get_word(n);
96}
97
98/**
99 * Reassigns the variable's local value.
100 */
101template<class EnumType>
103set_value(EnumType value) {
104 set_string_value(format_enum(value));
105}
106
107/**
108 * Returns the variable's value.
109 */
110template<class EnumType>
111INLINE EnumType ConfigVariableEnum<EnumType>::
112get_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 */
124template<class EnumType>
125INLINE EnumType ConfigVariableEnum<EnumType>::
126get_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 */
140template<class EnumType>
142get_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 */
150template<class EnumType>
152set_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 */
160template<class EnumType>
161INLINE EnumType ConfigVariableEnum<EnumType>::
162parse_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 */
174template<class EnumType>
175INLINE std::string ConfigVariableEnum<EnumType>::
176format_enum(EnumType value) const {
177 std::ostringstream strm;
178 strm << value;
179 return strm.str();
180}
A single declaration of a config variable, typically defined as one line in a .prc file,...
const std::string & get_string_value() const
Returns the value assigned to this variable.
The internal definition of a ConfigVariable.
This class specializes ConfigVariable as an enumerated type.
get_default_value
Returns the variable's default value.
set_value
Reassigns the variable's local value.
EnumType operator[](size_t n) const
Returns the value of the variable's nth word.
void operator=(EnumType value)
Reassigns the variable's local value.
void set_word(size_t n, EnumType value)
Reassigns the variable's nth value.
get_value
Returns the variable's value.
EnumType get_word(size_t n) const
Returns the variable's nth value.
size_t size() const
Returns the number of unique words in the variable.
This is a generic, untyped ConfigVariable.
STL namespace.