Forráskód Böngészése

make ConfigVariableFilename be more Filename-like

David Rose 21 éve
szülő
commit
f7c34a095a

+ 118 - 8
dtool/src/prc/configVariableFilename.I

@@ -24,7 +24,9 @@
 ////////////////////////////////////////////////////////////////////
 INLINE ConfigVariableFilename::
 ConfigVariableFilename(const string &name) :
-  ConfigVariable(name, VT_filename)
+  ConfigVariable(name, VT_filename),
+  _value_seq(-1),
+  _value_stale(true)
 {
   _core->set_used();
 }
@@ -59,20 +61,125 @@ operator = (const Filename &value) {
 //  Description: Returns the variable's value as a Filename.
 ////////////////////////////////////////////////////////////////////
 INLINE ConfigVariableFilename::
-operator Filename () const {
+operator const Filename & () const {
   return get_value();
 }
 
+////////////////////////////////////////////////////////////////////
+//     Function: ConfigVariableFilename::c_str
+//       Access: Public
+//  Description:
+////////////////////////////////////////////////////////////////////
+INLINE const char *ConfigVariableFilename::
+c_str() const {
+  return get_value().c_str();
+}
+
 ////////////////////////////////////////////////////////////////////
 //     Function: ConfigVariableFilename::empty
-//       Access: Published
-//  Description: Returns true if the filename is empty, false otherwise.
+//       Access: Public
+//  Description:
 ////////////////////////////////////////////////////////////////////
 INLINE bool ConfigVariableFilename::
 empty() const {
   return get_value().empty();
 }
 
+////////////////////////////////////////////////////////////////////
+//     Function: ConfigVariableFilename::length
+//       Access: Public
+//  Description:
+////////////////////////////////////////////////////////////////////
+INLINE size_t ConfigVariableFilename::
+length() const {
+  return get_value().length();
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: ConfigVariableFilename::Indexing operator
+//       Access: Public
+//  Description:
+////////////////////////////////////////////////////////////////////
+INLINE char ConfigVariableFilename::
+operator [] (int n) const {
+  return get_value()[n];
+}
+
+
+////////////////////////////////////////////////////////////////////
+//     Function: ConfigVariableFilename::get_fullpath
+//       Access: Public
+//  Description: Returns the entire filename: directory, basename,
+//               extension.  This is the same thing returned by the
+//               string typecast operator, so this function is a
+//               little redundant.
+////////////////////////////////////////////////////////////////////
+INLINE string ConfigVariableFilename::
+get_fullpath() const {
+  return get_value().get_fullpath();
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: ConfigVariableFilename::get_dirname
+//       Access: Public
+//  Description: Returns the directory part of the filename.  This is
+//               everything in the filename up to, but not including
+//               the rightmost slash.
+////////////////////////////////////////////////////////////////////
+INLINE string ConfigVariableFilename::
+get_dirname() const {
+  return get_value().get_dirname();
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: ConfigVariableFilename::get_basename
+//       Access: Public
+//  Description: Returns the basename part of the filename.  This is
+//               everything in the filename after the rightmost slash,
+//               including any extensions.
+////////////////////////////////////////////////////////////////////
+INLINE string ConfigVariableFilename::
+get_basename() const {
+  return get_value().get_basename();
+}
+
+
+////////////////////////////////////////////////////////////////////
+//     Function: ConfigVariableFilename::get_fullpath_wo_extension
+//       Access: Public
+//  Description: Returns the full filename--directory and basename
+//               parts--except for the extension.
+////////////////////////////////////////////////////////////////////
+INLINE string ConfigVariableFilename::
+get_fullpath_wo_extension() const {
+  return get_value().get_fullpath_wo_extension();
+}
+
+
+////////////////////////////////////////////////////////////////////
+//     Function: ConfigVariableFilename::get_basename_wo_extension
+//       Access: Public
+//  Description: Returns the basename part of the filename, without
+//               the file extension.
+////////////////////////////////////////////////////////////////////
+INLINE string ConfigVariableFilename::
+get_basename_wo_extension() const {
+  return get_value().get_basename_wo_extension();
+}
+
+
+////////////////////////////////////////////////////////////////////
+//     Function: ConfigVariableFilename::get_extension
+//       Access: Public
+//  Description: Returns the file extension.  This is everything after
+//               the rightmost dot, if there is one, or the empty
+//               string if there is not.
+////////////////////////////////////////////////////////////////////
+INLINE string ConfigVariableFilename::
+get_extension() const {
+  return get_value().get_extension();
+}
+
 ////////////////////////////////////////////////////////////////////
 //     Function: ConfigVariableFilename::Equality operator
 //       Access: Public
@@ -118,9 +225,12 @@ set_value(const Filename &value) {
 //       Access: Published
 //  Description: Returns the variable's value.
 ////////////////////////////////////////////////////////////////////
-INLINE Filename ConfigVariableFilename::
+INLINE const Filename &ConfigVariableFilename::
 get_value() const {
-  return Filename::expand_from(get_string_value());
+  if (_value_stale || _value_seq != _core->get_value_seq()) {
+    ((ConfigVariableFilename *)this)->reload_value();
+  }
+  return _value;
 }
 
 ////////////////////////////////////////////////////////////////////
@@ -132,9 +242,9 @@ INLINE Filename ConfigVariableFilename::
 get_default_value() const {
   const ConfigDeclaration *decl = ConfigVariable::get_default_value();
   if (decl != (ConfigDeclaration *)NULL) {
-    return decl->get_string_word(0);
+    return Filename::expand_from(decl->get_string_value());
   }
-  return string();
+  return Filename();
 }
 
 ////////////////////////////////////////////////////////////////////

+ 14 - 0
dtool/src/prc/configVariableFilename.cxx

@@ -17,3 +17,17 @@
 ////////////////////////////////////////////////////////////////////
 
 #include "configVariableFilename.h"
+
+////////////////////////////////////////////////////////////////////
+//     Function: ConfigVariableFilename::reload_value
+//       Access: Private
+//  Description: Recopies the config variable into the Filename for
+//               returning its value.
+////////////////////////////////////////////////////////////////////
+void ConfigVariableFilename::
+reload_value() {
+  _value = Filename::expand_from(get_string_value());
+
+  _value_seq = _core->get_value_seq();
+  _value_stale = false;
+}

+ 23 - 2
dtool/src/prc/configVariableFilename.h

@@ -39,8 +39,21 @@ PUBLISHED:
                                 const string &description = string(), int flags = 0);
 
   INLINE void operator = (const Filename &value);
-  INLINE operator Filename () const;
+  INLINE operator const Filename &() const;
+
+  // These methods help the ConfigVariableFilename act like a Filename
+  // object.
+  INLINE const char *c_str() const;
   INLINE bool empty() const;
+  INLINE size_t length() const;
+  INLINE char operator [] (int n) const;
+
+  INLINE string get_fullpath() const;
+  INLINE string get_dirname() const;
+  INLINE string get_basename() const;
+  INLINE string get_fullpath_wo_extension() const;
+  INLINE string get_basename_wo_extension() const;
+  INLINE string get_extension() const;
 
   // Comparison operators are handy.
   INLINE bool operator == (const Filename &other) const;
@@ -48,11 +61,19 @@ PUBLISHED:
   INLINE bool operator < (const Filename &other) const;
 
   INLINE void set_value(const Filename &value);
-  INLINE Filename get_value() const;
+  INLINE const Filename &get_value() const;
   INLINE Filename get_default_value() const;
 
   INLINE Filename get_word(int n) const;
   INLINE void set_word(int n, const Filename &value);
+
+private:
+  void reload_value();
+
+  int _value_seq;
+  bool _value_stale;
+
+  Filename _value;
 };
 
 #include "configVariableFilename.I"

+ 1 - 1
dtool/src/prc/configVariableString.I

@@ -163,7 +163,7 @@ INLINE string ConfigVariableString::
 get_default_value() const {
   const ConfigDeclaration *decl = ConfigVariable::get_default_value();
   if (decl != (ConfigDeclaration *)NULL) {
-    return decl->get_string_word(0);
+    return decl->get_string_value();
   }
   return string();
 }