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