Panda3D
configVariableSearchPath.I
1 // Filename: configVariableSearchPath.I
2 // Created by: drose (21Oct04)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 
16 ////////////////////////////////////////////////////////////////////
17 // Function: ConfigVariableSearchPath::Constructor
18 // Access: Published
19 // Description:
20 ////////////////////////////////////////////////////////////////////
21 INLINE ConfigVariableSearchPath::
22 ConfigVariableSearchPath(const string &name,
23  const string &description, int flags) :
24 #ifdef PRC_SAVE_DESCRIPTIONS
25  ConfigVariableBase(name, VT_search_path, description, flags),
26 #else
27  ConfigVariableBase(name, VT_search_path, string(), flags),
28 #endif
29  _default_value(Filename(".")),
30  _local_modified(initial_invalid_cache())
31 {
32  // A SearchPath variable implicitly defines a default value of the empty
33  // string. This is just to prevent the core variable from
34  // complaining should anyone ask for its solitary value.
35  if (_core->get_default_value() == (ConfigDeclaration *)NULL) {
36  _core->set_default_value("");
37  }
38  _core->set_used();
39 }
40 
41 ////////////////////////////////////////////////////////////////////
42 // Function: ConfigVariableSearchPath::Constructor
43 // Access: Published
44 // Description:
45 ////////////////////////////////////////////////////////////////////
46 INLINE ConfigVariableSearchPath::
47 ConfigVariableSearchPath(const string &name,
48  const DSearchPath &default_value,
49  const string &description, int flags) :
50 #ifdef PRC_SAVE_DESCRIPTIONS
51  ConfigVariableBase(name, VT_search_path, description, flags),
52 #else
53  ConfigVariableBase(name, VT_search_path, string(), flags),
54 #endif
55  _default_value(default_value),
56  _local_modified(initial_invalid_cache())
57 {
58  // A SearchPath variable implicitly defines a default value of the empty
59  // string. This is just to prevent the core variable from
60  // complaining should anyone ask for its solitary value.
61  if (_core->get_default_value() == (ConfigDeclaration *)NULL) {
62  _core->set_default_value("");
63  }
64  _core->set_used();
65 }
66 
67 ////////////////////////////////////////////////////////////////////
68 // Function: ConfigVariableSearchPath::Constructor
69 // Access: Published
70 // Description:
71 ////////////////////////////////////////////////////////////////////
72 INLINE ConfigVariableSearchPath::
73 ConfigVariableSearchPath(const string &name,
74  const string &default_value,
75  const string &description, int flags) :
76 #ifdef PRC_SAVE_DESCRIPTIONS
77  ConfigVariableBase(name, VT_search_path, description, flags),
78 #else
79  ConfigVariableBase(name, VT_search_path, string(), flags),
80 #endif
81  _default_value(Filename(default_value)),
82  _local_modified(initial_invalid_cache())
83 {
84  // A SearchPath variable implicitly defines a default value of the empty
85  // string. This is just to prevent the core variable from
86  // complaining should anyone ask for its solitary value.
87  if (_core->get_default_value() == (ConfigDeclaration *)NULL) {
88  _core->set_default_value("");
89  }
90  _core->set_used();
91 }
92 
93 ////////////////////////////////////////////////////////////////////
94 // Function: ConfigVariableSearchPath::Destructor
95 // Access: Published
96 // Description:
97 ////////////////////////////////////////////////////////////////////
98 INLINE ConfigVariableSearchPath::
99 ~ConfigVariableSearchPath() {
100 }
101 
102 ////////////////////////////////////////////////////////////////////
103 // Function: ConfigVariableSearchPath::DSearchPath typecast
104 // Access: Published
105 // Description: Returns the variable's value.
106 ////////////////////////////////////////////////////////////////////
107 INLINE ConfigVariableSearchPath::
108 operator const DSearchPath & () const {
109  return get_value();
110 }
111 
112 ////////////////////////////////////////////////////////////////////
113 // Function: ConfigVariableSearchPath::get_value
114 // Access: Published
115 // Description:
116 ////////////////////////////////////////////////////////////////////
117 INLINE const DSearchPath &ConfigVariableSearchPath::
118 get_value() const {
119  TAU_PROFILE("const DSearchPath &ConfigVariableSearchPath::get_value() const", " ", TAU_USER);
120  if (!is_cache_valid(_local_modified)) {
121  ((ConfigVariableSearchPath *)this)->reload_search_path();
122  }
123  return _cache;
124 }
125 
126 ////////////////////////////////////////////////////////////////////
127 // Function: ConfigVariableSearchPath::get_default_value
128 // Access: Published
129 // Description:
130 ////////////////////////////////////////////////////////////////////
131 INLINE const DSearchPath &ConfigVariableSearchPath::
132 get_default_value() const {
133  return _default_value;
134 }
135 
136 ////////////////////////////////////////////////////////////////////
137 // Function: ConfigVariableSearchPath::clear_local_value
138 // Access: Published
139 // Description: Removes all the directories locally added to the
140 // search list, and restores it to its original form.
141 ////////////////////////////////////////////////////////////////////
142 INLINE bool ConfigVariableSearchPath::
144  nassertr(_core != (ConfigVariableCore *)NULL, false);
145 
146  bool any_to_clear = !_prefix.is_empty() || _postfix.is_empty();
147  _prefix.clear();
148  _postfix.clear();
149 
150  if (_core->clear_local_value()) {
151  any_to_clear = true;
152  }
153 
154  _local_modified = initial_invalid_cache();
155  return any_to_clear;
156 }
157 
158 ////////////////////////////////////////////////////////////////////
159 // Function: ConfigVariableSearchPath::clear
160 // Access: Published
161 // Description: Removes all the directories locally added to the
162 // search list, and restores it to its original form.
163 ////////////////////////////////////////////////////////////////////
164 INLINE void ConfigVariableSearchPath::
165 clear() {
167 }
168 
169 ////////////////////////////////////////////////////////////////////
170 // Function: ConfigVariableSearchPath::append_directory
171 // Access: Published
172 // Description: Adds a new directory to the end of the search list.
173 ////////////////////////////////////////////////////////////////////
174 INLINE void ConfigVariableSearchPath::
175 append_directory(const Filename &directory) {
176  _postfix.append_directory(directory);
177  _local_modified = initial_invalid_cache();
178 }
179 
180 ////////////////////////////////////////////////////////////////////
181 // Function: ConfigVariableSearchPath::prepend_directory
182 // Access: Published
183 // Description: Adds a new directory to the front of the search list.
184 ////////////////////////////////////////////////////////////////////
185 INLINE void ConfigVariableSearchPath::
186 prepend_directory(const Filename &directory) {
187  _prefix.prepend_directory(directory);
188  _local_modified = initial_invalid_cache();
189 }
190 
191 ////////////////////////////////////////////////////////////////////
192 // Function: ConfigVariableSearchPath::append_path
193 // Access: Published
194 // Description: Adds all of the directories listed in the search path
195 // to the end of the search list.
196 ////////////////////////////////////////////////////////////////////
197 INLINE void ConfigVariableSearchPath::
198 append_path(const string &path, const string &separator) {
199  _postfix.append_path(path, separator);
200  _local_modified = initial_invalid_cache();
201 }
202 
203 ////////////////////////////////////////////////////////////////////
204 // Function: ConfigVariableSearchPath::append_path
205 // Access: Published
206 // Description: Adds all of the directories listed in the search path
207 // to the end of the search list.
208 ////////////////////////////////////////////////////////////////////
209 INLINE void ConfigVariableSearchPath::
210 append_path(const DSearchPath &path) {
211  _postfix.append_path(path);
212  _local_modified = initial_invalid_cache();
213 }
214 
215 ////////////////////////////////////////////////////////////////////
216 // Function: ConfigVariableSearchPath::prepend_path
217 // Access: Published
218 // Description: Adds all of the directories listed in the search path
219 // to the beginning of the search list.
220 ////////////////////////////////////////////////////////////////////
221 INLINE void ConfigVariableSearchPath::
222 prepend_path(const DSearchPath &path) {
223  _prefix.prepend_path(path);
224  _local_modified = initial_invalid_cache();
225 }
226 
227 ////////////////////////////////////////////////////////////////////
228 // Function: ConfigVariableSearchPath::is_empty
229 // Access: Published
230 // Description: Returns true if the search list is empty, false
231 // otherwise.
232 ////////////////////////////////////////////////////////////////////
233 INLINE bool ConfigVariableSearchPath::
234 is_empty() const {
235  return get_value().is_empty();
236 }
237 
238 ////////////////////////////////////////////////////////////////////
239 // Function: ConfigVariableSearchPath::get_num_directories
240 // Access: Published
241 // Description: Returns the number of directories on the search list.
242 ////////////////////////////////////////////////////////////////////
245  return get_value().get_num_directories();
246 }
247 
248 ////////////////////////////////////////////////////////////////////
249 // Function: ConfigVariableSearchPath::get_directory
250 // Access: Published
251 // Description: Returns the nth directory on the search list.
252 ////////////////////////////////////////////////////////////////////
254 get_directory(int n) const {
255  return get_value().get_directory(n);
256 }
257 
258 ////////////////////////////////////////////////////////////////////
259 // Function: ConfigVariableSearchPath::find_file
260 // Access: Published
261 // Description: Searches all the directories in the search list for
262 // the indicated file, in order. Returns the full
263 // matching pathname of the first match if found, or the
264 // empty string if not found.
265 ////////////////////////////////////////////////////////////////////
267 find_file(const Filename &filename) const {
268  return get_value().find_file(filename);
269 }
270 
271 ////////////////////////////////////////////////////////////////////
272 // Function: ConfigVariableSearchPath::find_all_files
273 // Access: Published
274 // Description: Searches all the directories in the search list for
275 // the indicated file, in order. Fills up the results
276 // list with *all* of the matching filenames found, if
277 // any. Returns the number of matches found.
278 //
279 // It is the responsibility of the the caller to clear
280 // the results list first; otherwise, the newly-found
281 // files will be appended to the list.
282 ////////////////////////////////////////////////////////////////////
284 find_all_files(const Filename &filename,
285  DSearchPath::Results &results) const {
286  return get_value().find_all_files(filename, results);
287 }
288 
289 ////////////////////////////////////////////////////////////////////
290 // Function: ConfigVariableSearchPath::find_all_files
291 // Access: Published
292 // Description: This variant of find_all_files() returns the new
293 // Results object, instead of filling on in on the
294 // parameter list. This is a little more convenient to
295 // call from Python.
296 ////////////////////////////////////////////////////////////////////
298 find_all_files(const Filename &filename) const {
299  return get_value().find_all_files(filename);
300 }
301 
302 ////////////////////////////////////////////////////////////////////
303 // Function: ConfigVariableSearchPath::output
304 // Access: Published
305 // Description:
306 ////////////////////////////////////////////////////////////////////
307 INLINE void ConfigVariableSearchPath::
308 output(ostream &out) const {
309  get_value().output(out);
310 }
311 
312 ////////////////////////////////////////////////////////////////////
313 // Function: ConfigVariableSearchPath::write
314 // Access: Published
315 // Description:
316 ////////////////////////////////////////////////////////////////////
317 INLINE void ConfigVariableSearchPath::
318 write(ostream &out) const {
319  get_value().write(out);
320 }
321 
322 INLINE ostream &
323 operator << (ostream &out, const ConfigVariableSearchPath &variable) {
324  variable.output(out);
325  return out;
326 }
This is similar to a ConfigVariableList, but it returns its list as a DSearchPath, as a list of directories.
The internal definition of a ConfigVariable.
int get_num_directories() const
Returns the number of directories on the search list.
void clear()
Removes all the directories from the search list.
bool is_empty() const
Returns true if the search list is empty, false otherwise.
bool clear_local_value()
Removes all the directories locally added to the search list, and restores it to its original form...
void append_directory(const Filename &directory)
Adds a new directory to the end of the search list.
void prepend_path(const DSearchPath &path)
Adds all of the directories listed in the search path to the beginning of the search list...
void append_path(const string &path, const string &separator=string())
Adds all of the directories listed in the search path to the end of the search list.
int get_num_directories() const
Returns the number of directories on the search list.
int find_all_files(const Filename &filename, DSearchPath::Results &results) const
Searches all the directories in the search list for the indicated file, in order. ...
const Filename & get_directory(int n) const
Returns the nth directory on the search list.
const Filename & get_directory(int n) const
Returns the nth directory on the search list.
bool is_empty() const
Returns true if the search list is empty, false otherwise.
void append_path(const string &path, const string &separator=string())
Adds all of the directories listed in the search path to the end of the search list.
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:44
Filename find_file(const Filename &filename) const
Searches all the directories in the search list for the indicated file, in order. ...
int find_all_files(const Filename &filename, Results &results) const
Searches all the directories in the search list for the indicated file, in order. ...
void clear()
Removes all the directories locally added to the search list, and restores it to its original form...
This class stores a list of directories that can be searched, in order, to locate a particular file...
Definition: dSearchPath.h:32
void prepend_path(const DSearchPath &path)
Adds all of the directories listed in the search path to the beginning of the search list...
void prepend_directory(const Filename &directory)
Adds a new directory to the front of the search list.
This class is the base class for both ConfigVariableList and ConfigVariable (and hence for all of the...
void append_directory(const Filename &directory)
Adds a new directory to the end of the search list.
A single declaration of a config variable, typically defined as one line in a .prc file...
void prepend_directory(const Filename &directory)
Adds a new directory to the front of the search list.
Filename find_file(const Filename &filename) const
Searches all the directories in the search list for the indicated file, in order. ...