A library for working with phylogenetic and population genetic data.
v0.32.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-2022 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 
88  std::string const& id() const
89  {
90  return pimpl_->id_();
91  }
92 
94  {
95  return pimpl_->bounding_box_();
96  }
97 
98  void write(
99  std::ostream& out,
100  size_t indent = 0,
101  SvgDrawingOptions const& options = SvgDrawingOptions()
102  ) const {
103  pimpl_->write_( out, indent, options );
104  }
105 
106  // -------------------------------------------------------------
107  // Internal Members
108  // -------------------------------------------------------------
109 
110 private:
111 
112  struct Concept
113  {
114  virtual ~Concept() {}
115 
116  virtual std::string const& id_() const = 0;
117  virtual SvgBox bounding_box_() const = 0;
118 
119  virtual void write_(
120  std::ostream& out,
121  size_t indent = 0,
122  SvgDrawingOptions const& options = SvgDrawingOptions()
123  ) const = 0;
124 
125  virtual std::unique_ptr< Concept > clone() const = 0;
126  };
127 
128  template< typename T >
129  struct Model : Concept
130  {
131  Model( T const& value )
132  : object_( value )
133  {}
134 
135  std::string const& id_() const override
136  {
137  return object_.id;
138  }
139 
140  SvgBox bounding_box_() const override
141  {
142  return object_.bounding_box();
143  }
144 
145  void write_(
146  std::ostream& out,
147  size_t indent = 0,
148  SvgDrawingOptions const& options = SvgDrawingOptions()
149  ) const override {
150  object_.write( out, indent, options );
151  }
152 
153  std::unique_ptr< Concept > clone() const override
154  {
155  // Although we are in untils namespace here, we specify the namespace full,
156  // in order to avoid ambiguous overload when compiled with C++17.
157  return genesis::utils::make_unique< Model<T> >( object_ );
158  }
159 
160  T object_;
161  };
162 
163  std::unique_ptr< Concept > pimpl_;
164 
165 };
166 
167 } // namespace utils
168 } // namespace genesis
169 
170 #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:719
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:98
genesis::utils::SvgObject::id
std::string const & id() const
Definition: object.hpp:88
genesis::utils::SvgBox
Definition: utils/formats/svg/helper.hpp:127
std.hpp
Provides some valuable additions to STD.
genesis::utils::SvgObject::bounding_box
SvgBox bounding_box() const
Definition: object.hpp:93
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:213
genesis::utils::SvgObject
Definition: object.hpp:46