00001
00002
00003
00004
00005
00006
00007
00008 #ifndef IMPDISPLAY_WRITER_H
00009 #define IMPDISPLAY_WRITER_H
00010
00011 #include "display_config.h"
00012 #include "geometry.h"
00013 #include "internal/writers.h"
00014 #include "GeometryProcessor.h"
00015
00016 #include <IMP/file.h>
00017 #include <IMP/PairContainer.h>
00018 #include <IMP/SingletonContainer.h>
00019 #include <IMP/RefCounted.h>
00020
00021 IMPDISPLAY_BEGIN_NAMESPACE
00022
00023
00024
00025
00026
00027
00028
00029
00030 class IMPDISPLAYEXPORT Writer: public GeometryProcessor, public Object
00031 {
00032 TextOutput out_;
00033 bool on_open_called_;
00034 protected:
00035
00036 std::ostream &get_stream() {
00037 if (! on_open_called_) {
00038
00039 on_open_called_=true;
00040 on_open();
00041 }
00042 return out_;
00043 }
00044
00045 public:
00046
00047 Writer(TextOutput fn, std::string name);
00048 Writer(std::string name);
00049
00050
00051
00052
00053
00054 bool get_stream_is_open() {
00055 return out_;
00056 }
00057
00058
00059
00060 virtual void set_file_name(std::string name) {
00061 set_output(TextOutput(name));
00062 }
00063
00064 virtual void set_output(TextOutput f) {
00065 if (get_stream_is_open()) on_close();
00066 out_= f;
00067 if (get_stream_is_open()) {
00068 set_was_used(true);
00069 on_open();
00070 }
00071 }
00072
00073
00074 void close() {
00075 if (get_stream_is_open()) on_close();
00076 out_= TextOutput();
00077 }
00078
00079
00080 virtual ~Writer();
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092 void add_geometry(Geometry* g);
00093
00094 void add_geometry(const Geometries &g) {
00095 for (unsigned int i=0; i< g.size(); ++i) {
00096 IMP_CHECK_OBJECT(g[i]);
00097 IMP::internal::OwnerPointer<Geometry> gp(g[i]);
00098 add_geometry(gp);
00099 }
00100 }
00101
00102
00103 protected:
00104
00105 virtual void on_close()=0;
00106
00107 virtual void on_open()=0;
00108 };
00109
00110
00111 IMPDISPLAYEXPORT Writer *create_writer(std::string filename);
00112
00113 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
00114 #define IMP_REGISTER_WRITER(Name, suffix) \
00115 namespace { \
00116 internal::WriterFactoryRegistrar registrar(suffix, \
00117 new internal::WriterFactoryHelper<Name>()); \
00118 }
00119 #endif
00120
00121 IMPDISPLAY_END_NAMESPACE
00122
00123 #endif