Panda3D
 All Classes Functions Variables Enumerations
fltInstanceRef.cxx
1 // Filename: fltInstanceRef.cxx
2 // Created by: drose (30Aug00)
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 #include "fltInstanceRef.h"
16 #include "fltRecordReader.h"
17 #include "fltRecordWriter.h"
18 #include "fltInstanceDefinition.h"
19 #include "fltHeader.h"
20 
21 TypeHandle FltInstanceRef::_type_handle;
22 
23 ////////////////////////////////////////////////////////////////////
24 // Function: FltInstanceRef::Constructor
25 // Access: Public
26 // Description:
27 ////////////////////////////////////////////////////////////////////
28 FltInstanceRef::
29 FltInstanceRef(FltHeader *header) : FltBead(header) {
30  _instance_index = 0;
31 }
32 
33 ////////////////////////////////////////////////////////////////////
34 // Function: FltInstanceRef::get_instance
35 // Access: Public
36 // Description: Returns the instance subtree referenced by this node,
37 // or NULL if the reference is invalid.
38 ////////////////////////////////////////////////////////////////////
40 get_instance() const {
41  return _header->get_instance(_instance_index);
42 }
43 
44 ////////////////////////////////////////////////////////////////////
45 // Function: FltInstanceRef::write
46 // Access: Public
47 // Description: Writes a multiple-line description of the record and
48 // all of its children. This is a human-readable
49 // description, primarily for debugging; to write a flt
50 // file, use FltHeader::write_flt().
51 ////////////////////////////////////////////////////////////////////
53 write(ostream &out, int indent_level) const {
54  indent(out, indent_level) << "instance";
55  FltInstanceDefinition *def = _header->get_instance(_instance_index);
56  if (def != (FltInstanceDefinition *)NULL) {
57  def->write_children(out, indent_level + 2);
58  indent(out, indent_level) << "}\n";
59  } else {
60  out << "\n";
61  }
62 }
63 
64 ////////////////////////////////////////////////////////////////////
65 // Function: FltInstanceRef::extract_record
66 // Access: Protected, Virtual
67 // Description: Fills in the information in this bead based on the
68 // information given in the indicated datagram, whose
69 // opcode has already been read. Returns true on
70 // success, false if the datagram is invalid.
71 ////////////////////////////////////////////////////////////////////
72 bool FltInstanceRef::
73 extract_record(FltRecordReader &reader) {
74  if (!FltBead::extract_record(reader)) {
75  return false;
76  }
77 
78  nassertr(reader.get_opcode() == FO_instance_ref, false);
79  DatagramIterator &iterator = reader.get_iterator();
80 
81  iterator.skip_bytes(2);
82  _instance_index = iterator.get_be_int16();
83 
84  check_remaining_size(iterator);
85  return true;
86 }
87 
88 ////////////////////////////////////////////////////////////////////
89 // Function: FltInstanceRef::write_record_and_children
90 // Access: Protected, Virtual
91 // Description: Writes this record out to the flt file, along with all
92 // of its ancillary records and children records. Returns
93 // FE_ok on success, or something else on error.
94 ////////////////////////////////////////////////////////////////////
95 FltError FltInstanceRef::
96 write_record_and_children(FltRecordWriter &writer) const {
97  // First, make sure our instance definition has already been written.
98  FltError result = writer.write_instance_def(_header, _instance_index);
99  if (result != FE_ok) {
100  return result;
101  }
102 
103  // Then write out our own record.
104  return FltBead::write_record_and_children(writer);
105 }
106 
107 ////////////////////////////////////////////////////////////////////
108 // Function: FltInstanceRef::build_record
109 // Access: Protected, Virtual
110 // Description: Fills up the current record on the FltRecordWriter with
111 // data for this record, but does not advance the
112 // writer. Returns true on success, false if there is
113 // some error.
114 ////////////////////////////////////////////////////////////////////
115 bool FltInstanceRef::
116 build_record(FltRecordWriter &writer) const {
117  if (!FltBead::build_record(writer)) {
118  return false;
119  }
120 
121  writer.set_opcode(FO_instance_ref);
122  Datagram &datagram = writer.update_datagram();
123 
124  datagram.pad_bytes(2);
125  datagram.add_be_int16(_instance_index);
126 
127  return true;
128 }
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...
A base class for any of a broad family of flt records that represent particular beads in the hierarch...
Definition: fltBead.h:33
DatagramIterator & get_iterator()
Returns an iterator suitable for extracting data from the current record.
void pad_bytes(size_t size)
Adds the indicated number of zero bytes to the datagram.
Definition: datagram.cxx:111
This is the first bead in the file, the top of the bead hierarchy, and the primary interface to readi...
Definition: fltHeader.h:48
FltError write_instance_def(FltHeader *header, int instance_index)
Ensures that the given instance definition has already been written to the file.
FltInstanceDefinition * get_instance() const
Returns the instance subtree referenced by this node, or NULL if the reference is invalid...
void skip_bytes(size_t size)
Skips over the indicated number of bytes in the datagram.
virtual void write(ostream &out, int indent_level=0) const
Writes a multiple-line description of the record and all of its children.
void check_remaining_size(const DatagramIterator &di, const string &name=string()) const
Checks that the iterator has no bytes left, as it should at the end of a successfully read record...
Definition: fltRecord.cxx:313
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:85
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:43
This special kind of record marks the top node of an instance subtree.
void add_be_int16(PN_int16 value)
Adds a signed 16-bit big-endian integer to the datagram.
Definition: datagram.I:255