A library for working with phylogenetic and population genetic data.
v0.27.0
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-2021 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 <lczech@carnegiescience.edu>
23  Department of Plant Biology, Carnegie Institution For Science
24  260 Panama Street, Stanford, CA 94305, USA
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  // Although we are in untils namespace here, we specify the namespace full,
63  // in order to avoid ambiguous overload when compiled with C++17.
64  : pimpl_( genesis::utils::make_unique< Model<T> >( object ))
65  {}
66 
67  SvgObject( SvgObject const& other )
68  : pimpl_( other.pimpl_->clone() )
69  {}
70 
71  SvgObject( SvgObject&& ) = default;
72 
74  {
75  std::swap( pimpl_, other.pimpl_ );
76  return *this;
77  }
78 
79  // SvgObject& operator= ( SvgObject const& ) = default;
80  // SvgObject& operator= ( SvgObject&& ) = default;
81 
82  ~SvgObject() = default;
83 
84  // -------------------------------------------------------------
85  // Members
86  // -------------------------------------------------------------
87 
89  {
90  return pimpl_->bounding_box_();
91  }
92 
93  void write(
94  std::ostream& out,
95  size_t indent = 0,
96  SvgDrawingOptions const& options = SvgDrawingOptions()
97  ) const {
98  pimpl_->write_( out, indent, options );
99  }
100 
101  // -------------------------------------------------------------
102  // Internal Members
103  // -------------------------------------------------------------
104 
105 private:
106 
107  struct Concept
108  {
109  virtual ~Concept() {}
110 
111  virtual SvgBox bounding_box_() const = 0;
112 
113  virtual void write_(
114  std::ostream& out,
115  size_t indent = 0,
116  SvgDrawingOptions const& options = SvgDrawingOptions()
117  ) const = 0;
118 
119  virtual std::unique_ptr< Concept > clone() const = 0;
120  };
121 
122  template< typename T >
123  struct Model : Concept
124  {
125  Model( T const& value )
126  : object_( value )
127  {}
128 
129  SvgBox bounding_box_() const override
130  {
131  return object_.bounding_box();
132  }
133 
134  void write_(
135  std::ostream& out,
136  size_t indent = 0,
137  SvgDrawingOptions const& options = SvgDrawingOptions()
138  ) const override {
139  object_.write( out, indent, options );
140  }
141 
142  std::unique_ptr< Concept > clone() const override
143  {
144  // Although we are in untils namespace here, we specify the namespace full,
145  // in order to avoid ambiguous overload when compiled with C++17.
146  return genesis::utils::make_unique< Model<T> >( object_ );
147  }
148 
149  T object_;
150  };
151 
152  std::unique_ptr< Concept > pimpl_;
153 
154 };
155 
156 } // namespace utils
157 } // namespace genesis
158 
159 #endif // include guard
genesis::placement::swap
void swap(Sample &lhs, Sample &rhs)
Definition: sample.cpp:104
genesis::utils::SvgObject::SvgObject
SvgObject(SvgObject const &other)
Definition: object.hpp:67
helper.hpp
genesis::utils::indent
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:522
genesis::utils::SvgObject::SvgObject
SvgObject(T const &object)
Definition: object.hpp:61
genesis::utils::SvgObject::write
void write(std::ostream &out, size_t indent=0, SvgDrawingOptions const &options=SvgDrawingOptions()) const
Definition: object.hpp:93
genesis::utils::SvgBox
Definition: utils/formats/svg/helper.hpp:116
std.hpp
Provides some valuable additions to STD.
genesis::utils::SvgObject::bounding_box
SvgBox bounding_box() const
Definition: object.hpp:88
genesis::utils::SvgObject::~SvgObject
~SvgObject()=default
genesis
Container namespace for all symbols of genesis in order to keep them separate when used as a library.
Definition: placement/formats/edge_color.cpp:42
genesis::utils::SvgObject::operator=
SvgObject & operator=(SvgObject other)
Definition: object.hpp:73
genesis::utils::make_unique
std::unique_ptr< T > make_unique(Args &&... args)
Returns a std::unique_ptr for a given type.
Definition: std.hpp:82
genesis::utils::SvgDrawingOptions
Definition: utils/formats/svg/helper.hpp:202
genesis::utils::SvgObject
Definition: object.hpp:46