Panda3D
fltInstanceRef.cxx
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 fltInstanceRef.cxx
10  * @author drose
11  * @date 2000-08-30
12  */
13 
14 #include "fltInstanceRef.h"
15 #include "fltRecordReader.h"
16 #include "fltRecordWriter.h"
17 #include "fltInstanceDefinition.h"
18 #include "fltHeader.h"
19 
20 TypeHandle FltInstanceRef::_type_handle;
21 
22 /**
23  *
24  */
25 FltInstanceRef::
26 FltInstanceRef(FltHeader *header) : FltBead(header) {
27  _instance_index = 0;
28 }
29 
30 /**
31  * Returns the instance subtree referenced by this node, or NULL if the
32  * reference is invalid.
33  */
35 get_instance() const {
36  return _header->get_instance(_instance_index);
37 }
38 
39 /**
40  * Writes a multiple-line description of the record and all of its children.
41  * This is a human-readable description, primarily for debugging; to write a
42  * flt file, use FltHeader::write_flt().
43  */
45 write(std::ostream &out, int indent_level) const {
46  indent(out, indent_level) << "instance";
47  FltInstanceDefinition *def = _header->get_instance(_instance_index);
48  if (def != nullptr) {
49  def->write_children(out, indent_level + 2);
50  indent(out, indent_level) << "}\n";
51  } else {
52  out << "\n";
53  }
54 }
55 
56 /**
57  * Fills in the information in this bead based on the information given in the
58  * indicated datagram, whose opcode has already been read. Returns true on
59  * success, false if the datagram is invalid.
60  */
61 bool FltInstanceRef::
62 extract_record(FltRecordReader &reader) {
63  if (!FltBead::extract_record(reader)) {
64  return false;
65  }
66 
67  nassertr(reader.get_opcode() == FO_instance_ref, false);
68  DatagramIterator &iterator = reader.get_iterator();
69 
70  iterator.skip_bytes(2);
71  _instance_index = iterator.get_be_int16();
72 
73  check_remaining_size(iterator);
74  return true;
75 }
76 
77 /**
78  * Writes this record out to the flt file, along with all of its ancillary
79  * records and children records. Returns FE_ok on success, or something else
80  * on error.
81  */
82 FltError FltInstanceRef::
83 write_record_and_children(FltRecordWriter &writer) const {
84  // First, make sure our instance definition has already been written.
85  FltError result = writer.write_instance_def(_header, _instance_index);
86  if (result != FE_ok) {
87  return result;
88  }
89 
90  // Then write out our own record.
91  return FltBead::write_record_and_children(writer);
92 }
93 
94 /**
95  * Fills up the current record on the FltRecordWriter with data for this
96  * record, but does not advance the writer. Returns true on success, false if
97  * there is some error.
98  */
99 bool FltInstanceRef::
100 build_record(FltRecordWriter &writer) const {
101  if (!FltBead::build_record(writer)) {
102  return false;
103  }
104 
105  writer.set_opcode(FO_instance_ref);
106  Datagram &datagram = writer.update_datagram();
107 
108  datagram.pad_bytes(2);
109  datagram.add_be_int16(_instance_index);
110 
111  return true;
112 }
This class writes a sequence of FltRecords to an ostream, handling opcode and size counts properly.
This class turns an istream into a sequence of FltRecords by reading a sequence of Datagrams and extr...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void check_remaining_size(const DatagramIterator &di, const std::string &name=std::string()) const
Checks that the iterator has no bytes left, as it should at the end of a successfully read record.
Definition: fltRecord.cxx:254
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A base class for any of a broad family of flt records that represent particular beads in the hierarch...
Definition: fltBead.h:29
DatagramIterator & get_iterator()
Returns an iterator suitable for extracting data from the current record.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void pad_bytes(size_t size)
Adds the indicated number of zero bytes to the datagram.
Definition: datagram.cxx:99
This is the first bead in the file, the top of the bead hierarchy, and the primary interface to readi...
Definition: fltHeader.h:44
FltError write_instance_def(FltHeader *header, int instance_index)
Ensures that the given instance definition has already been written to the file.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
std::ostream & indent(std::ostream &out, int indent_level)
A handy function for doing text formatting.
Definition: indent.cxx:20
void skip_bytes(size_t size)
Skips over the indicated number of bytes in the datagram.
void add_be_int16(int16_t value)
Adds a signed 16-bit big-endian integer to the datagram.
Definition: datagram.I:145
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
FltInstanceDefinition * get_instance() const
Returns the instance subtree referenced by this node, or NULL if the reference is invalid.
FltOpcode get_opcode() const
Returns the opcode associated with the current record.
A class to retrieve the individual data elements previously stored in a Datagram.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
void set_opcode(FltOpcode opcode)
Sets the opcode associated with the current record.
Datagram & update_datagram()
Returns a modifiable reference to the datagram associated with the current record.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
virtual void write(std::ostream &out, int indent_level=0) const
Writes a multiple-line description of the record and all of its children.
This special kind of record marks the top node of an instance subtree.