Panda3D
 All Classes Functions Variables Enumerations
fltInstanceRef.cxx
00001 // Filename: fltInstanceRef.cxx
00002 // Created by:  drose (30Aug00)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #include "fltInstanceRef.h"
00016 #include "fltRecordReader.h"
00017 #include "fltRecordWriter.h"
00018 #include "fltInstanceDefinition.h"
00019 #include "fltHeader.h"
00020 
00021 TypeHandle FltInstanceRef::_type_handle;
00022 
00023 ////////////////////////////////////////////////////////////////////
00024 //     Function: FltInstanceRef::Constructor
00025 //       Access: Public
00026 //  Description:
00027 ////////////////////////////////////////////////////////////////////
00028 FltInstanceRef::
00029 FltInstanceRef(FltHeader *header) : FltBead(header) {
00030   _instance_index = 0;
00031 }
00032 
00033 ////////////////////////////////////////////////////////////////////
00034 //     Function: FltInstanceRef::get_instance
00035 //       Access: Public
00036 //  Description: Returns the instance subtree referenced by this node,
00037 //               or NULL if the reference is invalid.
00038 ////////////////////////////////////////////////////////////////////
00039 FltInstanceDefinition *FltInstanceRef::
00040 get_instance() const {
00041   return _header->get_instance(_instance_index);
00042 }
00043 
00044 ////////////////////////////////////////////////////////////////////
00045 //     Function: FltInstanceRef::write
00046 //       Access: Public
00047 //  Description: Writes a multiple-line description of the record and
00048 //               all of its children.  This is a human-readable
00049 //               description, primarily for debugging; to write a flt
00050 //               file, use FltHeader::write_flt().
00051 ////////////////////////////////////////////////////////////////////
00052 void FltInstanceRef::
00053 write(ostream &out, int indent_level) const {
00054   indent(out, indent_level) << "instance";
00055   FltInstanceDefinition *def = _header->get_instance(_instance_index);
00056   if (def != (FltInstanceDefinition *)NULL) {
00057     def->write_children(out, indent_level + 2);
00058     indent(out, indent_level) << "}\n";
00059   } else {
00060     out << "\n";
00061   }
00062 }
00063 
00064 ////////////////////////////////////////////////////////////////////
00065 //     Function: FltInstanceRef::extract_record
00066 //       Access: Protected, Virtual
00067 //  Description: Fills in the information in this bead based on the
00068 //               information given in the indicated datagram, whose
00069 //               opcode has already been read.  Returns true on
00070 //               success, false if the datagram is invalid.
00071 ////////////////////////////////////////////////////////////////////
00072 bool FltInstanceRef::
00073 extract_record(FltRecordReader &reader) {
00074   if (!FltBead::extract_record(reader)) {
00075     return false;
00076   }
00077 
00078   nassertr(reader.get_opcode() == FO_instance_ref, false);
00079   DatagramIterator &iterator = reader.get_iterator();
00080 
00081   iterator.skip_bytes(2);
00082   _instance_index = iterator.get_be_int16();
00083 
00084   check_remaining_size(iterator);
00085   return true;
00086 }
00087 
00088 ////////////////////////////////////////////////////////////////////
00089 //     Function: FltInstanceRef::write_record_and_children
00090 //       Access: Protected, Virtual
00091 //  Description: Writes this record out to the flt file, along with all
00092 //               of its ancillary records and children records.  Returns
00093 //               FE_ok on success, or something else on error.
00094 ////////////////////////////////////////////////////////////////////
00095 FltError FltInstanceRef::
00096 write_record_and_children(FltRecordWriter &writer) const {
00097   // First, make sure our instance definition has already been written.
00098   FltError result = writer.write_instance_def(_header, _instance_index);
00099   if (result != FE_ok) {
00100     return result;
00101   }
00102 
00103   // Then write out our own record.
00104   return FltBead::write_record_and_children(writer);
00105 }
00106 
00107 ////////////////////////////////////////////////////////////////////
00108 //     Function: FltInstanceRef::build_record
00109 //       Access: Protected, Virtual
00110 //  Description: Fills up the current record on the FltRecordWriter with
00111 //               data for this record, but does not advance the
00112 //               writer.  Returns true on success, false if there is
00113 //               some error.
00114 ////////////////////////////////////////////////////////////////////
00115 bool FltInstanceRef::
00116 build_record(FltRecordWriter &writer) const {
00117   if (!FltBead::build_record(writer)) {
00118     return false;
00119   }
00120 
00121   writer.set_opcode(FO_instance_ref);
00122   Datagram &datagram = writer.update_datagram();
00123 
00124   datagram.pad_bytes(2);
00125   datagram.add_be_int16(_instance_index);
00126 
00127   return true;
00128 }
 All Classes Functions Variables Enumerations