ReFRACtor
printable.h
Go to the documentation of this file.
1 #ifndef PRINTABLE_H
2 #define PRINTABLE_H
3 #include "generic_object.h"
4 #include <sstream>
5 
6 namespace FullPhysics {
7 
8 /****************************************************************/
24 template<class T> class Printable : public virtual GenericObject {
25 public:
26 //-----------------------------------------------------------------------
29 //-----------------------------------------------------------------------
30 
31  std::string print_to_string() const
32  {
33  // This reserve shouldn't really be necessary, but on a Mac
34  // 10.4.11 using gcc 4.0.1, there is some kind of bug where we get
35  // a "Double free" error when printing in Ruby. I never tracked
36  // exactly where this occurred, but it was somewhere in the
37  // iostream library when the buffer of os was resized. We just
38  // reserve enough space up front so this isn't an issue. Since
39  // this only gets called when printing, there shouldn't be much of
40  // a performance hit in doing this.
41  std::string buf("blah");
42  buf.reserve(1000);
43  std::ostringstream os(buf);
44  ((T*) this)->print(os);
45  return os.str();
46  }
47 
48 //-----------------------------------------------------------------------
50 //-----------------------------------------------------------------------
51 
52  friend std::ostream& operator<<(std::ostream& Os, const T& P)
53  { P.print(Os); return Os;}
54 };
55 }
56 #endif
friend std::ostream & operator<<(std::ostream &Os, const T &P)
Print to stream.
Definition: printable.h:52
std::string print_to_string() const
Print to string.
Definition: printable.h:31
This is a Mixin for classes that can be printed.
Definition: printable.h:24
Contains classes to abstract away details in various Spurr Radiative Transfer software.
Definition: doxygen_python.h:1
For use with SWIG, it is useful to have a base class that everything can be cast to.

Copyright © 2017, California Institute of Technology.
ALL RIGHTS RESERVED.
U.S. Government Sponsorship acknowledged.
Generated Fri Aug 24 2018 15:44:11