Panda3D
bamCacheRecord.I
1 // Filename: bamCacheRecord.I
2 // Created by: drose (09Jun06)
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: BamCacheRecord::make_copy
18 // Access: Published
19 // Description: Returns a duplicate of the BamCacheRecord. The
20 // duplicate will not have a data pointer set, even
21 // though one may have been assigned to the original via
22 // set_data().
23 ////////////////////////////////////////////////////////////////////
24 INLINE PT(BamCacheRecord) BamCacheRecord::
25 make_copy() const {
26  return new BamCacheRecord(*this);
27 }
28 
29 ////////////////////////////////////////////////////////////////////
30 // Function: BamCacheRecord::operator ==
31 // Access: Published
32 // Description: Returns true if the record matches the other record
33 // in those attributes which get written to disk. Does
34 // not compare the data pointer.
35 ////////////////////////////////////////////////////////////////////
36 INLINE bool BamCacheRecord::
37 operator == (const BamCacheRecord &other) const {
38  return (_source_pathname == other._source_pathname &&
39  _cache_filename == other._cache_filename &&
40  _recorded_time == other._recorded_time &&
41  _record_size == other._record_size);
42 }
43 
44 ////////////////////////////////////////////////////////////////////
45 // Function: BamCacheRecord::get_source_pathname
46 // Access: Published
47 // Description: Returns the full pathname to the source file that
48 // originally generated this cache request. In some
49 // cases, for instance in the case of a of a multipage
50 // texture like "cube_#.png", this may not not a true
51 // filename on disk.
52 ////////////////////////////////////////////////////////////////////
53 INLINE const Filename &BamCacheRecord::
54 get_source_pathname() const {
55  return _source_pathname;
56 }
57 
58 ////////////////////////////////////////////////////////////////////
59 // Function: BamCacheRecord::get_cache_filename
60 // Access: Published
61 // Description: Returns the name of the cache file as hashed from the
62 // source_pathname. This will be relative to the root
63 // of the cache directory, and it will not include any
64 // suffixes that may be appended to resolve hash
65 // conflicts.
66 ////////////////////////////////////////////////////////////////////
67 INLINE const Filename &BamCacheRecord::
68 get_cache_filename() const {
69  return _cache_filename;
70 }
71 
72 ////////////////////////////////////////////////////////////////////
73 // Function: BamCacheRecord::get_source_timestamp
74 // Access: Published
75 // Description: Returns the file timestamp of the original source
76 // file that generated this cache record, if available.
77 // In some cases the original file timestamp is not
78 // available, and this will return 0.
79 ////////////////////////////////////////////////////////////////////
80 INLINE time_t BamCacheRecord::
81 get_source_timestamp() const {
82  return _source_timestamp;
83 }
84 
85 ////////////////////////////////////////////////////////////////////
86 // Function: BamCacheRecord::get_recorded_time
87 // Access: Published
88 // Description: Returns the time at which this particular record was
89 // recorded or updated.
90 ////////////////////////////////////////////////////////////////////
91 INLINE time_t BamCacheRecord::
92 get_recorded_time() const {
93  return _recorded_time;
94 }
95 
96 ////////////////////////////////////////////////////////////////////
97 // Function: BamCacheRecord::get_num_dependent_files
98 // Access: Published
99 // Description: Returns the number of source files that contribute to
100 // the cache.
101 ////////////////////////////////////////////////////////////////////
102 INLINE int BamCacheRecord::
103 get_num_dependent_files() const {
104  return _files.size();
105 }
106 
107 ////////////////////////////////////////////////////////////////////
108 // Function: BamCacheRecord::get_dependent_pathname
109 // Access: Published
110 // Description: Returns the full pathname of the nth source files
111 // that contributes to the cache.
112 ////////////////////////////////////////////////////////////////////
113 INLINE const Filename &BamCacheRecord::
114 get_dependent_pathname(int n) const {
115  nassertr(n >= 0 && n < (int)_files.size(), _files[0]._pathname);
116  return _files[n]._pathname;
117 }
118 
119 ////////////////////////////////////////////////////////////////////
120 // Function: BamCacheRecord::has_data
121 // Access: Published
122 // Description: Returns true if this cache record has an in-memory
123 // data object associated--that is, the object stored in
124 // the cache.
125 ////////////////////////////////////////////////////////////////////
126 INLINE bool BamCacheRecord::
127 has_data() const {
128  return (_ptr != (TypedWritable *)NULL);
129 }
130 
131 ////////////////////////////////////////////////////////////////////
132 // Function: BamCacheRecord::clear_data
133 // Access: Published
134 // Description: Removes the in-memory data object associated with
135 // this record, if any. This does not affect the
136 // on-disk representation of the record.
137 ////////////////////////////////////////////////////////////////////
138 INLINE void BamCacheRecord::
139 clear_data() {
140  if (_ref_ptr != NULL) {
141  unref_delete(_ref_ptr);
142  }
143 
144  _ptr = NULL;
145  _ref_ptr = NULL;
146 }
147 
148 ////////////////////////////////////////////////////////////////////
149 // Function: BamCacheRecord::get_data
150 // Access: Published
151 // Description: Returns a pointer to the data stored in the
152 // record, or NULL if there is no data. The pointer is
153 // not removed from the record.
154 ////////////////////////////////////////////////////////////////////
156 get_data() const {
157  return _ptr;
158 }
159 
160 ////////////////////////////////////////////////////////////////////
161 // Function: BamCacheRecord::extract_data
162 // Access: Published
163 // Description: Fills ptr and ref_ptr with the two different-typed
164 // pointers to the same object, the data stored within
165 // this record. This transfers ownership of the data
166 // pointer; the caller will be responsible for managing
167 // the reference counts on this object subsequently.
168 //
169 // Returns true if the record contained any data (and
170 // the pointers have been filled), false if it didn't
171 // (and the pointers are NULL).
172 ////////////////////////////////////////////////////////////////////
173 INLINE bool BamCacheRecord::
175  ptr = _ptr;
176  ref_ptr = _ref_ptr;
177  clear_data();
178  return (ptr != (TypedWritable *)NULL);
179 }
180 
181 ////////////////////////////////////////////////////////////////////
182 // Function: BamCacheRecord::set_data
183 // Access: Published
184 // Description: Stores a new data object on the record. You should
185 // pass the same pointer twice, to both parameters; this
186 // allows the C++ typecasting to automatically convert
187 // the pointer into both a TypedWritable and a
188 // ReferenceCount pointer, so that the BamCacheRecord
189 // object can reliably manage the reference counts.
190 //
191 // You may pass 0 or NULL as the second parameter. If
192 // you do this, the BamCacheRecord will not manage the
193 // object's reference count; it will be up to you to
194 // ensure the object is not deleted during the lifetime
195 // of the BamCacheRecord object.
196 ////////////////////////////////////////////////////////////////////
197 INLINE void BamCacheRecord::
199  if (_ptr != ptr) {
200  clear_data();
201  _ptr = ptr;
202  _ref_ptr = ref_ptr;
203  if (_ref_ptr != NULL) {
204  _ref_ptr->ref();
205  }
206  }
207 }
208 
209 ////////////////////////////////////////////////////////////////////
210 // Function: BamCacheRecord::set_data
211 // Access: Published
212 // Description: This variant on set_data() is provided just to allow
213 // Python code to pass a 0 as the second parameter.
214 ////////////////////////////////////////////////////////////////////
215 INLINE void BamCacheRecord::
216 set_data(TypedWritable *ptr, int dummy) {
217  nassertv(dummy == 0);
218  set_data(ptr, (ReferenceCount *)NULL);
219 }
220 
221 ////////////////////////////////////////////////////////////////////
222 // Function: BamCacheRecord::SortByAccessTime::operator ()
223 // Access: Public
224 // Description: Returns true if a sorts before b, false otherwise.
225 ////////////////////////////////////////////////////////////////////
226 INLINE bool BamCacheRecord::SortByAccessTime::
227 operator () (const BamCacheRecord *a, const BamCacheRecord *b) const {
228  return (a->_record_access_time < b->_record_access_time);
229 }
void set_data(TypedWritable *ptr, ReferenceCount *ref_ptr)
Stores a new data object on the record.
TypedWritable * get_data() const
Returns a pointer to the data stored in the record, or NULL if there is no data.
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:37
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:44
An instance of this class is written to the front of a Bam or Txo file to make the file a cached inst...
void ref() const
Explicitly increments the reference count.
A base class for all things that want to be reference-counted.
bool extract_data(TypedWritable *&ptr, ReferenceCount *&ref_ptr)
Fills ptr and ref_ptr with the two different-typed pointers to the same object, the data stored withi...