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);
00076 ~BamWriter();
00077
00078 void set_target(DatagramSink *target);
00079 INLINE DatagramSink *get_target();
00080
00081 bool init();
00082 INLINE const Filename &get_filename() const;
00083 bool write_object(const TypedWritable *obj);
00084 bool has_object(const TypedWritable *obj) const;
00085 void flush();
00086
00087 INLINE BamEndian get_file_endian() const;
00088 INLINE bool get_file_stdfloat_double() const;
00089
00090 INLINE BamTextureMode get_file_texture_mode() const;
00091 INLINE void set_file_texture_mode(BamTextureMode file_texture_mode);
00092
00093 public:
00094
00095
00096 void consider_update(const TypedWritable *obj);
00097
00098 void write_pointer(Datagram &packet, const TypedWritable *dest);
00099
00100 void write_file_data(SubfileInfo &result, const Filename &filename);
00101 void write_file_data(SubfileInfo &result, const SubfileInfo &source);
00102
00103 void write_cdata(Datagram &packet, const PipelineCyclerBase &cycler);
00104 void write_cdata(Datagram &packet, const PipelineCyclerBase &cycler,
00105 void *extra_data);
00106 bool register_pta(Datagram &packet, const void *ptr);
00107 void write_handle(Datagram &packet, TypeHandle type);
00108
00109 private:
00110 void object_destructs(TypedWritable *object);
00111
00112 void write_object_id(Datagram &dg, int object_id);
00113 void write_pta_id(Datagram &dg, int pta_id);
00114 int enqueue_object(const TypedWritable *object);
00115 bool flush_queue();
00116
00117 BamEndian _file_endian;
00118 bool _file_stdfloat_double;
00119 BamTextureMode _file_texture_mode;
00120
00121
00122 pset<int, int_hash> _types_written;
00123
00124
00125
00126
00127 class StoreState {
00128 public:
00129 int _object_id;
00130 UpdateSeq _written_seq;
00131 UpdateSeq _modified;
00132
00133 StoreState(int object_id) : _object_id(object_id) {}
00134 };
00135 typedef phash_map<const TypedWritable *, StoreState, pointer_hash> StateMap;
00136 StateMap _state_map;
00137
00138
00139
00140
00141
00142
00143 UpdateSeq _writing_seq;
00144
00145
00146
00147
00148 BamObjectCode _next_boc;
00149
00150
00151 int _next_object_id;
00152 bool _long_object_id;
00153
00154
00155
00156 typedef pdeque<const TypedWritable *> ObjectQueue;
00157 ObjectQueue _object_queue;
00158
00159
00160
00161
00162 typedef vector_int FreedObjectIds;
00163 FreedObjectIds _freed_object_ids;
00164
00165
00166
00167 typedef phash_map<const void *, int, pointer_hash> PTAMap;
00168 PTAMap _pta_map;
00169 int _next_pta_id;
00170 bool _long_pta_id;
00171
00172
00173 DatagramSink *_target;
00174 bool _needs_init;
00175
00176 friend class TypedWritable;
00177 };
00178
00179 #include "bamWriter.I"
00180
00181 #endif
00182