A toolkit for working with phylogenetic data.
v0.19.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
object.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_UTILS_FORMATS_SVG_OBJECT_H_
2 #define GENESIS_UTILS_FORMATS_SVG_OBJECT_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2017 Lucas Czech
7 
8  This program is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 
21  Contact:
22  Lucas Czech <lucas.czech@h-its.org>
23  Exelixis Lab, Heidelberg Institute for Theoretical Studies
24  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
25 */
26 
36 
37 #include <iosfwd>
38 
39 namespace genesis {
40 namespace utils {
41 
42 // =================================================================================================
43 // Svg Object
44 // =================================================================================================
45 
46 class SvgObject
47 {
48 public:
49 
50  // -------------------------------------------------------------
51  // Typedefs and Enums
52  // -------------------------------------------------------------
53 
55 
56  // -------------------------------------------------------------
57  // Constructors and Rule of Five
58  // -------------------------------------------------------------
59 
60  template< typename T >
61  SvgObject( T const& object )
62  : pimpl_( make_unique< Model<T> >( object ))
63  {}
64 
65  SvgObject( SvgObject const& other )
66  : pimpl_( other.pimpl_->clone() )
67  {}
68 
69  SvgObject( SvgObject&& ) = default;
70 
72  {
73  std::swap( pimpl_, other.pimpl_ );
74  return *this;
75  }
76 
77  // SvgObject& operator= ( SvgObject const& ) = default;
78  // SvgObject& operator= ( SvgObject&& ) = default;
79 
80  ~SvgObject() = default;
81 
82  // -------------------------------------------------------------
83  // Members
84  // -------------------------------------------------------------
85 
87  {
88  return pimpl_->bounding_box_();
89  }
90 
91  void write(
92  std::ostream& out,
93  size_t indent = 0,
94  SvgDrawingOptions const& options = SvgDrawingOptions()
95  ) const {
96  pimpl_->write_( out, indent, options );
97  }
98 
99  // -------------------------------------------------------------
100  // Internal Members
101  // -------------------------------------------------------------
102 
103 private:
104 
105  struct Concept
106  {
107  virtual ~Concept() {}
108 
109  virtual SvgBox bounding_box_() const = 0;
110 
111  virtual void write_(
112  std::ostream& out,
113  size_t indent = 0,
114  SvgDrawingOptions const& options = SvgDrawingOptions()
115  ) const = 0;
116 
117  virtual std::unique_ptr< Concept > clone() const = 0;
118  };
119 
120  template< typename T >
121  struct Model : Concept
122  {
123  Model( T const& value )
124  : object_( value )
125  {}
126 
127  SvgBox bounding_box_() const
128  {
129  return object_.bounding_box();
130  }
131 
132  void write_(
133  std::ostream& out,
134  size_t indent = 0,
135  SvgDrawingOptions const& options = SvgDrawingOptions()
136  ) const {
137  object_.write( out, indent, options );
138  }
139 
140  std::unique_ptr< Concept > clone() const
141  {
142  return make_unique< Model<T> >( object_ );
143  }
144 
145  T object_;
146  };
147 
148  std::unique_ptr< Concept > pimpl_;
149 
150 };
151 
152 } // namespace utils
153 } // namespace genesis
154 
155 #endif // include guard
SvgObject & operator=(SvgObject other)
Definition: object.hpp:71
void swap(SequenceSet &lhs, SequenceSet &rhs)
void write(std::ostream &out, size_t indent=0, SvgDrawingOptions const &options=SvgDrawingOptions()) const
Definition: object.hpp:91
SvgObject(T const &object)
Definition: object.hpp:61
Provides some valuable additions to STD.
std::string indent(std::string const &text, std::string const &indentation)
Indent each line of text with indentation and return the result.
Definition: string.cpp:231
std::unique_ptr< T > make_unique(Args &&...args)
Returns a std::unique_ptr for a given type.
Definition: std.hpp:68
SvgBox bounding_box() const
Definition: object.hpp:86
SvgObject(SvgObject const &other)
Definition: object.hpp:65