Panda3D
Loading...
Searching...
No Matches
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 */
19INLINE PT(BamCacheRecord) BamCacheRecord::
20make_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 */
28INLINE bool BamCacheRecord::
29operator == (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 */
41INLINE const Filename &BamCacheRecord::
42get_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 */
51INLINE const Filename &BamCacheRecord::
52get_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 */
61INLINE time_t BamCacheRecord::
62get_source_timestamp() const {
63 return _source_timestamp;
64}
65
66/**
67 * Returns the time at which this particular record was recorded or updated.
68 */
69INLINE time_t BamCacheRecord::
70get_recorded_time() const {
71 return _recorded_time;
72}
73
74/**
75 * Returns the number of source files that contribute to the cache.
76 */
77INLINE int BamCacheRecord::
78get_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 */
86INLINE const Filename &BamCacheRecord::
87get_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 */
96INLINE bool BamCacheRecord::
97has_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 */
105INLINE void BamCacheRecord::
106clear_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 */
120get_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 */
134extract_data(TypedWritable *&ptr, ReferenceCount *&ref_ptr) {
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 */
152INLINE void BamCacheRecord::
153set_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 */
168INLINE void BamCacheRecord::
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 */
177INLINE 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 */
186INLINE void BamCacheRecord::
187set_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 */
195INLINE bool BamCacheRecord::SortByAccessTime::
196operator () (const BamCacheRecord *a, const BamCacheRecord *b) const {
197 return (a->_record_access_time < b->_record_access_time);
198}
An instance of this class is written to the front of a Bam or Txo file to make the file a cached inst...
get_data
Returns a pointer to the data stored in the record, or NULL if there is no data.
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...
set_data
Stores a new data object on the record.
The name of a file, such as a texture file or an Egg file.
Definition filename.h:44
A base class for all things that want to be reference-counted.
void ref() const
Explicitly increments the reference count.
A base class for things which need to inherit from both TypedWritable and from ReferenceCount.
Base class for objects that can be written to and read from Bam files.
virtual ReferenceCount * as_reference_count()
Returns the pointer cast to a ReferenceCount pointer, if it is in fact of that type.
void unref_delete(RefCountType *ptr)
This global helper function will unref the given ReferenceCount object, and if the reference count re...