Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

panda/src/putil/bamReader.h

Go to the documentation of this file.
00001 // Filename: bamReader.h
00002 // Created by:  jason (12Jun00)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
00008 //
00009 // All use of this software is subject to the terms of the Panda 3d
00010 // Software license.  You should have received a copy of this license
00011 // along with this source code; you will also find a current copy of
00012 // the license at http://www.panda3d.org/license.txt .
00013 //
00014 // To contact the maintainers of this program write to
00015 // panda3d@yahoogroups.com .
00016 //
00017 ////////////////////////////////////////////////////////////////////
00018 
00019 #ifndef __BAM_READER_
00020 #define __BAM_READER_
00021 
00022 #include "pandabase.h"
00023 #include "notify.h"
00024 
00025 #include "typedWritable.h"
00026 #include "datagramGenerator.h"
00027 #include "datagramIterator.h"
00028 #include "bamReaderParam.h"
00029 #include "factory.h"
00030 #include "vector_int.h"
00031 #include "pset.h"
00032 #include "dcast.h"
00033 
00034 #include <algorithm>
00035 
00036 struct PipelineCyclerBase;
00037 
00038 
00039 // A handy macro for reading PointerToArrays.
00040 #define READ_PTA(Manager, source, Read_func, array)   \
00041 {                                                     \
00042   void *t;                                            \
00043   if ((t = Manager->get_pta(source)) == (void*)NULL)  \
00044   {                                                   \
00045     array = Read_func(source);                        \
00046     Manager->register_pta(array.get_void_ptr());      \
00047   }                                                   \
00048   else                                                \
00049   {                                                   \
00050     array.set_void_ptr(t);                            \
00051   }                                                   \
00052 }
00053 
00054 ////////////////////////////////////////////////////////////////////
00055 //       Class : BamReader
00056 // Description : This is the fundamental interface for extracting
00057 //               binary objects from a Bam file, as generated by a
00058 //               BamWriter.
00059 //
00060 //               A Bam file can be thought of as a linear collection
00061 //               of objects.  Each object is an instance of a class
00062 //               that inherits, directly or indirectly, from
00063 //               TypedWritable.  The objects may include pointers to
00064 //               other objects within the Bam file; the BamReader
00065 //               automatically manages these (with help from code
00066 //               within each class) and restores the pointers
00067 //               correctly.
00068 //
00069 //               This is the abstract interface and does not
00070 //               specifically deal with disk files, but rather with a
00071 //               DatagramGenerator of some kind, which is simply a
00072 //               linear source of Datagrams.  It is probably from a
00073 //               disk file, but it might conceivably be streamed
00074 //               directly from a network or some such nonsense.
00075 //
00076 //               Bam files are most often used to store scene graphs
00077 //               or subgraphs, and by convention they are given
00078 //               filenames ending in the extension ".bam" when they
00079 //               are used for this purpose.  However, a Bam file may
00080 //               store any arbitrary list of TypedWritable objects;
00081 //               in this more general usage, they are given filenames
00082 //               ending in ".boo" to differentiate them from the more
00083 //               common scene graph files.
00084 //
00085 //               See also BamFile, which defines a higher-level
00086 //               interface to read and write Bam files on disk.
00087 ////////////////////////////////////////////////////////////////////
00088 class EXPCL_PANDA BamReader {
00089 public:
00090   typedef Factory<TypedWritable> WritableFactory;
00091   static BamReader *const Null;
00092   static WritableFactory *const NullFactory;
00093 
00094   // The primary interface for a caller.
00095 
00096   BamReader(DatagramGenerator *generator);
00097   ~BamReader();
00098 
00099   bool init();
00100   TypedWritable *read_object();
00101   INLINE bool is_eof() const;
00102   bool resolve();
00103 
00104   INLINE int get_file_major_ver() const;
00105   INLINE int get_file_minor_ver() const;
00106 
00107   INLINE int get_current_major_ver() const;
00108   INLINE int get_current_minor_ver() const;
00109 
00110 public:
00111   // Functions to support classes that read themselves from the Bam.
00112 
00113   void read_pointer(DatagramIterator &scan);
00114   void read_pointers(DatagramIterator &scan, int count);
00115   void skip_pointer(DatagramIterator &scan);
00116 
00117   void read_cdata(DatagramIterator &scan, PipelineCyclerBase &cycler);
00118 
00119   void register_finalize(TypedWritable *whom);
00120 
00121   typedef TypedWritable *(*ChangeThisFunc)(TypedWritable *object, BamReader *manager);
00122   void register_change_this(ChangeThisFunc func, TypedWritable *whom);
00123 
00124   void finalize_now(TypedWritable *whom);
00125 
00126   void *get_pta(DatagramIterator &scan);
00127   void register_pta(void *ptr);
00128 
00129   TypeHandle read_handle(DatagramIterator &scan);
00130 
00131 
00132 public:
00133   INLINE static WritableFactory *get_factory();
00134 private:
00135   INLINE static void create_factory();
00136 
00137 private:
00138   int p_read_object();
00139   bool resolve_object_pointers(TypedWritable *object, const vector_int &pointer_ids);
00140   bool resolve_cycler_pointers(PipelineCyclerBase *cycler, const vector_int &pointer_ids);
00141   void finalize();
00142 
00143 private:
00144   static WritableFactory *_factory;
00145 
00146   DatagramGenerator *_source;
00147 
00148   // This maps the type index numbers encountered within the Bam file
00149   // to actual TypeHandles.
00150   typedef pmap<int, TypeHandle> IndexMap;
00151   IndexMap _index_map;
00152 
00153   // This maps the object ID numbers encountered within the Bam file
00154   // to the actual pointers of the corresponding generated objects.
00155   class CreatedObj {
00156   public:
00157     TypedWritable *_ptr;
00158     ChangeThisFunc _change_this;
00159   };
00160   typedef pmap<int, CreatedObj> CreatedObjs;
00161   CreatedObjs _created_objs;
00162   // This is the iterator into the above map for the object we are
00163   // currently reading in p_read_object().  It is carefully maintained
00164   // during recursion.  We need this so we can associate
00165   // read_pointer() calls with the proper objects.
00166   CreatedObjs::iterator _now_creating;
00167   // This is the pointer to the current PipelineCycler we are reading,
00168   // if we are within a read_cdata() call.
00169   PipelineCyclerBase *_reading_cycler;
00170 
00171   // This records all the objects that still need their pointers
00172   // completed, along with the object ID's of the pointers they need,
00173   // in the order in which read_pointer() was called, so that we may
00174   // call the appropriate complete_pointers() later.
00175   typedef pmap<int, vector_int> ObjectPointers;
00176   ObjectPointers _object_pointers;
00177 
00178   // Ditto, for the PiplineCycler objects.
00179   typedef pmap<PipelineCyclerBase *, vector_int> CyclerPointers;
00180   CyclerPointers _cycler_pointers;
00181 
00182   // This is the number of extra objects that must still be read (and
00183   // saved in the _created_objs map) before returning from
00184   // read_object().
00185   int _num_extra_objects;
00186 
00187   // This is the set of all objects that registered themselves for
00188   // finalization.
00189   typedef pset<TypedWritable *> Finalize;
00190   Finalize _finalize_list;
00191 
00192   // These are used by get_pta() and register_pta() to unify multiple
00193   // references to the same PointerToArray.
00194   typedef pmap<int, void *> PTAMap;
00195   PTAMap _pta_map;
00196   int _pta_id;
00197 
00198   // This is used internally to record all of the new types created
00199   // on-the-fly to satisfy bam requirements.  We keep track of this
00200   // just so we can suppress warning messages from attempts to create
00201   // objects of these types.
00202   typedef pset<TypeHandle> NewTypes;
00203   static NewTypes _new_types;
00204 
00205   int _file_major, _file_minor;
00206   static const int _cur_major;
00207   static const int _cur_minor;
00208 };
00209 
00210 typedef BamReader::WritableFactory WritableFactory;
00211 
00212 // Useful function for taking apart the Factory Params in the static
00213 // functions that need to be defined in each writable class that will
00214 // be generated by a factory.  Sets the DatagramIterator and the
00215 // BamReader pointers.
00216 INLINE void
00217 parse_params(const FactoryParams &params,
00218              DatagramIterator &scan, BamReader *&manager);
00219 
00220 #include "bamReader.I"
00221 
00222 #endif

Generated on Fri May 2 00:43:29 2003 for Panda by doxygen1.3