Panda3D

configVariableEnum.I

00001 // Filename: configVariableEnum.I
00002 // Created by:  drose (21Oct04)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 
00016 ////////////////////////////////////////////////////////////////////
00017 //     Function: ConfigVariableEnum::Constructor
00018 //       Access: Published
00019 //  Description: 
00020 ////////////////////////////////////////////////////////////////////
00021 template<class EnumType>
00022 INLINE ConfigVariableEnum<EnumType>::
00023 ConfigVariableEnum(const string &name, EnumType default_value, 
00024                    const string &description, int flags) :
00025 #ifdef PRC_SAVE_DESCRIPTIONS
00026   ConfigVariable(name, ConfigVariableCore::VT_enum, description, flags),
00027 #else
00028   ConfigVariable(name, ConfigVariableCore::VT_enum, string(), flags),
00029 #endif
00030   _got_default_value(true),
00031   _default_value(default_value),
00032   _local_modified(initial_invalid_cache())
00033 {
00034   _core->set_default_value(format_enum(default_value));
00035   _core->set_used();
00036 }
00037 
00038 ////////////////////////////////////////////////////////////////////
00039 //     Function: ConfigVariableEnum::Constructor
00040 //       Access: Published
00041 //  Description: 
00042 ////////////////////////////////////////////////////////////////////
00043 template<class EnumType>
00044 INLINE ConfigVariableEnum<EnumType>::
00045 ConfigVariableEnum(const string &name, const string &default_value, 
00046                    const string &description, int flags) :
00047 #ifdef PRC_SAVE_DESCRIPTIONS
00048   ConfigVariable(name, ConfigVariableCore::VT_enum, description, flags),
00049 #else
00050   ConfigVariable(name, ConfigVariableCore::VT_enum, string(), flags),
00051 #endif
00052   _got_default_value(true),
00053   _default_value(parse_string(default_value)),
00054   _local_modified(initial_invalid_cache())
00055 {
00056   _core->set_default_value(default_value);
00057   _core->set_used();
00058 }
00059 
00060 ////////////////////////////////////////////////////////////////////
00061 //     Function: ConfigVariableEnum::Destructor
00062 //       Access: Public
00063 //  Description: 
00064 ////////////////////////////////////////////////////////////////////
00065 template<class EnumType>
00066 INLINE ConfigVariableEnum<EnumType>::
00067 ~ConfigVariableEnum() {
00068 }
00069 
00070 ////////////////////////////////////////////////////////////////////
00071 //     Function: ConfigVariableEnum::operator =
00072 //       Access: Public
00073 //  Description: Reassigns the variable's local value.
00074 ////////////////////////////////////////////////////////////////////
00075 template<class EnumType>
00076 INLINE void ConfigVariableEnum<EnumType>::
00077 operator = (EnumType value) {
00078   set_value(value);
00079 }
00080 
00081 ////////////////////////////////////////////////////////////////////
00082 //     Function: ConfigVariableEnum::typecast operator
00083 //       Access: Public
00084 //  Description: Returns the variable's value.
00085 ////////////////////////////////////////////////////////////////////
00086 template<class EnumType>
00087 INLINE ConfigVariableEnum<EnumType>::
00088 operator EnumType () const {
00089   return get_value();
00090 }
00091 
00092 ////////////////////////////////////////////////////////////////////
00093 //     Function: ConfigVariableEnum::size()
00094 //       Access: Public
00095 //  Description: Returns the number of unique words in the variable.
00096 ////////////////////////////////////////////////////////////////////
00097 template<class EnumType>
00098 INLINE int ConfigVariableEnum<EnumType>::
00099 size() const {
00100   return get_num_words();
00101 }
00102 
00103 ////////////////////////////////////////////////////////////////////
00104 //     Function: ConfigVariableEnum::operator []
00105 //       Access: Public
00106 //  Description: Returns the value of the variable's nth word.
00107 ////////////////////////////////////////////////////////////////////
00108 template<class EnumType>
00109 INLINE EnumType ConfigVariableEnum<EnumType>::
00110 operator [] (int n) const {
00111   return get_word(n);
00112 }
00113 
00114 ////////////////////////////////////////////////////////////////////
00115 //     Function: ConfigVariableEnum::set_value
00116 //       Access: Public
00117 //  Description: Reassigns the variable's local value.
00118 ////////////////////////////////////////////////////////////////////
00119 template<class EnumType>
00120 INLINE void ConfigVariableEnum<EnumType>::
00121 set_value(EnumType value) {
00122   set_string_value(format_enum(value));
00123 }
00124 
00125 ////////////////////////////////////////////////////////////////////
00126 //     Function: ConfigVariableEnum::get_value
00127 //       Access: Public
00128 //  Description: Returns the variable's value.
00129 ////////////////////////////////////////////////////////////////////
00130 template<class EnumType>
00131 INLINE EnumType ConfigVariableEnum<EnumType>::
00132 get_value() const {
00133   TAU_PROFILE("EnumType ConfigVariableEnum<EnumType>::get_value() const", " ", TAU_USER);
00134   if (!is_cache_valid(_local_modified)) {
00135     mark_cache_valid(((ConfigVariableEnum<EnumType> *)this)->_local_modified);
00136     ((ConfigVariableEnum<EnumType> *)this)->_cache = (EnumType)parse_string(get_string_value());
00137   }
00138   return _cache;
00139 }
00140 
00141 ////////////////////////////////////////////////////////////////////
00142 //     Function: ConfigVariableEnum::get_default_value
00143 //       Access: Public
00144 //  Description: Returns the variable's default value.
00145 ////////////////////////////////////////////////////////////////////
00146 template<class EnumType>
00147 INLINE EnumType ConfigVariableEnum<EnumType>::
00148 get_default_value() const {
00149   if (!_got_default_value) {
00150     const ConfigDeclaration *decl = ConfigVariable::get_default_value();
00151     if (decl != (ConfigDeclaration *)NULL) {
00152       ((ConfigVariableEnum<EnumType> *)this)->_default_value = (EnumType)parse_string(decl->get_string_value());
00153       ((ConfigVariableEnum<EnumType> *)this)->_got_default_value = true;
00154     }
00155   }
00156   return _default_value;
00157 }
00158 
00159 ////////////////////////////////////////////////////////////////////
00160 //     Function: ConfigVariableEnum::get_word
00161 //       Access: Public
00162 //  Description: Returns the variable's nth value.
00163 ////////////////////////////////////////////////////////////////////
00164 template<class EnumType>
00165 INLINE EnumType ConfigVariableEnum<EnumType>::
00166 get_word(int n) const {
00167   return (EnumType)parse_string(get_string_word(n));
00168 }
00169 
00170 ////////////////////////////////////////////////////////////////////
00171 //     Function: ConfigVariableEnum::set_word
00172 //       Access: Public
00173 //  Description: Reassigns the variable's nth value.  This makes a
00174 //               local copy of the variable's overall value.
00175 ////////////////////////////////////////////////////////////////////
00176 template<class EnumType>
00177 INLINE void ConfigVariableEnum<EnumType>::
00178 set_word(int n, EnumType value) {
00179   set_string_word(n, format_enum(value));
00180 }
00181 
00182 ////////////////////////////////////////////////////////////////////
00183 //     Function: ConfigVariableEnum::parse_string
00184 //       Access: Public, Virtual
00185 //  Description: Turns the string value into a value of the enumerated
00186 //               type by invoking its predefined operator >> (istream)
00187 //               operator.
00188 ////////////////////////////////////////////////////////////////////
00189 template<class EnumType>
00190 INLINE EnumType ConfigVariableEnum<EnumType>::
00191 parse_string(const string &value) const {
00192   istringstream strm(value);
00193   EnumType result;
00194   strm >> result;
00195   return result;
00196 }
00197 
00198 ////////////////////////////////////////////////////////////////////
00199 //     Function: ConfigVariableEnum::format_enum
00200 //       Access: Public, Virtual
00201 //  Description: The format_enum() method assumes the enumerated type
00202 //               has a valid operator << (ostream) defined, which
00203 //               balances against the operator >> (istream) operator.
00204 ////////////////////////////////////////////////////////////////////
00205 template<class EnumType>
00206 INLINE string ConfigVariableEnum<EnumType>::
00207 format_enum(EnumType value) const {
00208   ostringstream strm;
00209   strm << value;
00210   return strm.str();
00211 }
 All Classes Functions Variables Enumerations