00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #ifndef __BAM_WRITER_
00016 #define __BAM_WRITER_
00017
00018 #include "pandabase.h"
00019 #include "pnotify.h"
00020 #include "bamEnums.h"
00021 #include "typedWritable.h"
00022 #include "datagramSink.h"
00023 #include "pdeque.h"
00024 #include "pset.h"
00025 #include "pmap.h"
00026 #include "vector_int.h"
00027 #include "pipelineCyclerBase.h"
00028
00029
00030
00031 #define WRITE_PTA(Manager, dest, Write_func, array) \
00032 if (!Manager->register_pta(dest, array.p())) \
00033 { \
00034 Write_func(Manager, dest, array); \
00035 } \
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073 class EXPCL_PANDA_PUTIL BamWriter : public BamEnums {
00074 PUBLISHED:
00075 BamWriter(DatagramSink *target = NULL, const Filename &name = "");
00076 ~BamWriter();
00077
00078 void set_target(DatagramSink *target);
00079
00080 bool init();
00081 INLINE const Filename &get_filename() const;
00082 bool write_object(const TypedWritable *obj);
00083 bool has_object(const TypedWritable *obj) const;
00084 void flush();
00085
00086 INLINE BamEndian get_file_endian() const;
00087
00088 INLINE BamTextureMode get_file_texture_mode() const;
00089 INLINE void set_file_texture_mode(BamTextureMode file_texture_mode);
00090
00091 public:
00092
00093
00094 void consider_update(const TypedWritable *obj);
00095
00096 void write_pointer(Datagram &packet, const TypedWritable *dest);
00097 void write_cdata(Datagram &packet, const PipelineCyclerBase &cycler);
00098 void write_cdata(Datagram &packet, const PipelineCyclerBase &cycler,
00099 void *extra_data);
00100 bool register_pta(Datagram &packet, const void *ptr);
00101 void write_handle(Datagram &packet, TypeHandle type);
00102
00103 private:
00104 void object_destructs(TypedWritable *object);
00105
00106 void write_object_id(Datagram &dg, int object_id);
00107 void write_pta_id(Datagram &dg, int pta_id);
00108 int enqueue_object(const TypedWritable *object);
00109 bool flush_queue();
00110
00111
00112 Filename _filename;
00113
00114 BamEndian _file_endian;
00115 BamTextureMode _file_texture_mode;
00116
00117
00118 pset<int, int_hash> _types_written;
00119
00120
00121
00122
00123 class StoreState {
00124 public:
00125 int _object_id;
00126 UpdateSeq _written_seq;
00127 UpdateSeq _modified;
00128
00129 StoreState(int object_id) : _object_id(object_id) {}
00130 };
00131 typedef phash_map<const TypedWritable *, StoreState, pointer_hash> StateMap;
00132 StateMap _state_map;
00133
00134
00135
00136
00137
00138
00139 UpdateSeq _writing_seq;
00140
00141
00142
00143
00144 BamObjectCode _next_boc;
00145
00146
00147 int _next_object_id;
00148 bool _long_object_id;
00149
00150
00151
00152 typedef pdeque<const TypedWritable *> ObjectQueue;
00153 ObjectQueue _object_queue;
00154
00155
00156
00157
00158 typedef vector_int FreedObjectIds;
00159 FreedObjectIds _freed_object_ids;
00160
00161
00162
00163 typedef phash_map<const void *, int, pointer_hash> PTAMap;
00164 PTAMap _pta_map;
00165 int _next_pta_id;
00166 bool _long_pta_id;
00167
00168
00169 DatagramSink *_target;
00170 bool _needs_init;
00171
00172 friend class TypedWritable;
00173 };
00174
00175 #include "bamWriter.I"
00176
00177 #endif
00178