IMP  2.1.0
The Integrative Modeling Platform
Writer.h
Go to the documentation of this file.
1 /**
2  * \file IMP/display/Writer.h
3  * \brief Base class for writing geometry to a file
4  *
5  * Copyright 2007-2013 IMP Inventors. All rights reserved.
6  */
7 
8 #ifndef IMPDISPLAY_WRITER_H
9 #define IMPDISPLAY_WRITER_H
10 
11 #include <IMP/display/display_config.h>
12 #include "declare_Geometry.h"
13 #include "internal/writers.h"
14 #include "GeometryProcessor.h"
15 #include <IMP/base/Pointer.h>
16 #include <IMP/base/InputAdaptor.h>
17 
18 #include <boost/format.hpp>
19 
20 IMPDISPLAY_BEGIN_NAMESPACE
21 
22 //! Base class for writing geometry to a file.
23 /** A writer object accumulates geometry and then
24  writes a file when the object is destroyed. You must set the name
25  of the file being written to before writing using the
26  IMP::display::Writer::set_file_name() method.
27  */
28 class IMPDISPLAYEXPORT Writer : public GeometryProcessor,
29  public IMP::base::Object {
30  int frame_;
31 
32  public:
33  //! Create a writer opening the file with the passed name
34  Writer(std::string name);
35 
36  //! Write the data and close the file
37  virtual ~Writer();
38 
39  /** \name Frames
40  The writer has a concept of the current frame. Depending on
41  the implementation, each frame might be stored in a separate
42  file or they might all be in one file. If using a writer that
43  stores frames in multiple files, you should include a %1% in the
44  filename which will get replaced by the frame number.
45  @{
46  */
47  void set_frame(unsigned int i);
48  int get_frame() const { return frame_; }
49  /** @} */
50 
51  /** @name Geometry Addition methods
52  These methods can be used to add geometry to the model.
53  If you do not want the geometry objects to be destroyed
54  upon addition, make sure you store an IMP::Pointer
55  to them externally.
56 
57  \throws UsageException if it doesn't know
58  how to write that particular sort of geometry.
59  @{
60  */
61  void add_geometry(Geometry* g);
62 
63  void add_geometry(const Geometries& g) {
64  for (unsigned int i = 0; i < g.size(); ++i) {
65  IMP_CHECK_OBJECT(g[i]);
67  add_geometry(gp);
68  }
69  }
70  /** @} */
71 
72  protected:
73  //! A hook for implementation classes to use to take actions on file close
74  virtual void do_close() = 0;
75  //! A hook for implementation classes to use to take actions on file open
76  virtual void do_open() = 0;
77  //! in case you want to take action on a new frame
78  virtual void do_set_frame() {}
79 };
80 
81 /** A base class for writers which write to text files. By default,
82  separate frames are stored in separate files. To change this,
83  override the do_set_frame() method.
84 
85  \note If you inherit from this class, you must use the
86  IMP_TEXT_WRITER() macro and not provide any additional
87  constructors. Sorry, it is necessary in order to ensure
88  that the derived classes handler for opening a file
89  is successfully called, as it can't be directly
90  called from the TextWriter constructor.
91  */
92 class IMPDISPLAYEXPORT TextWriter : public Writer {
93  std::string file_name_;
94  base::TextOutput out_;
95 
96  protected:
97  void open();
98  //! Get the stream for inhereting classes to write to
99  std::ostream& get_stream() { return out_; }
100 
101  virtual void do_set_frame() IMP_OVERRIDE;
102 
103  public:
104  //! Create a writer opening the file with the passed sink
105  /** Frames are not supported with this constructor when using a format
106  such as CMM or Chimera that writes multiple frames to different
107  files.
108  */
109  TextWriter(base::TextOutput fn);
110  //! Create a write for a file or files with the passed name or pattern
111  /** The name should contain %1% if you want to write different frames
112  to separate files. Otherwise, it will either write all frames to the
113  same file (Pymol) or overwrite the file with each new frame,
114  Chimera, CMM.
115  */
116  TextWriter(std::string name);
117 
118  //! get the name of the current file being writter
119  std::string get_current_file_name() const {
120  if (file_name_.find("%1%") != std::string::npos) {
121  IMP_USAGE_CHECK(get_frame() >= 0, "No frame set");
122  std::ostringstream oss;
123  oss << boost::format(file_name_) % get_frame();
124  return oss.str();
125  } else {
126  return file_name_;
127  }
128  }
129  //! Write the data and close the file
130  virtual ~TextWriter();
131 };
132 
133 /** Create an appropriate writer based on the file suffix. */
134 IMPDISPLAYEXPORT Writer* create_writer(std::string filename);
135 
138 
139 /** An adaptor for functions that should take a writer as an input.
140  It can be implicitly constructed from either a Writer or a string.
141  In the later case it determines what type of writer is needed from
142  the file suffix. */
143 class IMPDISPLAYEXPORT WriterAdaptor : public base::InputAdaptor {
145 
146  public:
147  WriterAdaptor(std::string name) : writer_(create_writer(name)) {}
148  WriterAdaptor(Writer* w) : writer_(w) {}
149 #ifndef SWIG
150  Writer* operator->() const { return writer_; }
151  operator Writer*() const { return writer_; }
152 #endif
153  Writer* get_writer() const { return writer_; }
154  IMP_SHOWABLE_INLINE(WriterAdaptor, out << writer_->get_name());
155  ~WriterAdaptor();
156 };
157 
159 
160 IMPDISPLAY_END_NAMESPACE
161 
162 #endif /* IMPDISPLAY_WRITER_H */
A nullptr-initialized pointer to an IMP Object.
The base class for geometry.
A smart pointer to a ref-counted Object that is a class memeber.
Definition: base/Pointer.h:146
#define IMP_VALUES(Name, PluralName)
Define the type for storing sets of values.
#define IMP_SHOWABLE_INLINE(Name, how_to_show)
Declare the methods needed by an object that can be printed.
Base class for writing geometry to a file.
Definition: Writer.h:28
virtual void do_set_frame()
in case you want to take action on a new frame
Definition: Writer.h:78
Implement geometry for the basic shapes from IMP.algebra.
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
Provide a standard geometry processing framework.
Basic types used by IMP.
#define IMP_CHECK_OBJECT(obj)
Perform some basic validity checks on the object for memory debugging.
Writer * create_writer(std::string filename)
Common base class for heavy weight IMP objects.
#define IMP_OBJECTS(Name, PluralName)
Define the types for storing sets of objects.
Implement GeometryProcessor.
std::ostream & get_stream()
Get the stream for inhereting classes to write to.
Definition: Writer.h:99