A library for working with phylogenetic and population genetic data.
v0.32.0
definitions.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_UTILS_FORMATS_SVG_DEFINITIONS_H_
2 #define GENESIS_UTILS_FORMATS_SVG_DEFINITIONS_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 #include <string>
39 
40 namespace genesis {
41 namespace utils {
42 
43 // =================================================================================================
44 // Svg Definitions
45 // =================================================================================================
46 
48 {
49 public:
50 
51  // -------------------------------------------------------------
52  // Typedefs and Enums
53  // -------------------------------------------------------------
54 
56 
57  // -------------------------------------------------------------
58  // Constructors and Rule of Five
59  // -------------------------------------------------------------
60 
61  template< typename T >
62  SvgDefinitions( T const& object )
63  : pimpl_( make_unique< Model<T> >( object ))
64  {}
65 
67  : pimpl_( other.pimpl_->clone() )
68  {}
69 
70  SvgDefinitions( SvgDefinitions&& ) = default;
71 
73  {
74  std::swap( pimpl_, other.pimpl_ );
75  return *this;
76  }
77 
78  // SvgDefinitions& operator= ( SvgDefinitions const& ) = default;
79  // SvgDefinitions& operator= ( SvgDefinitions&& ) = default;
80 
81  ~SvgDefinitions() = default;
82 
83  // -------------------------------------------------------------
84  // Members
85  // -------------------------------------------------------------
86 
87  void write(
88  std::ostream& out,
89  size_t indent = 0
90  ) const {
91  pimpl_->write_( out, indent );
92  }
93 
94  // -------------------------------------------------------------
95  // Internal Members
96  // -------------------------------------------------------------
97 
98 private:
99 
100  struct Concept
101  {
102  virtual ~Concept() {}
103 
104  virtual void write_(
105  std::ostream& out,
106  size_t indent = 0
107  ) const = 0;
108 
109  virtual std::unique_ptr< Concept > clone() const = 0;
110  };
111 
112  template< typename T >
113  struct Model : Concept
114  {
115  Model( T const& value )
116  : object_( value )
117  {}
118 
119  void write_(
120  std::ostream& out,
121  size_t indent = 0
122  ) const override {
123  object_.write( out, indent );
124  }
125 
126  std::unique_ptr< Concept > clone() const override
127  {
128  // Although we are in untils namespace here, we specify the namespace full,
129  // in order to avoid ambiguous overload when compiled with C++17.
130  return genesis::utils::make_unique< Model<T> >( object_ );
131  }
132 
133  T object_;
134  };
135 
136  std::unique_ptr< Concept > pimpl_;
137 
138 };
139 
140 } // namespace utils
141 } // namespace genesis
142 
143 #endif // include guard
genesis::placement::swap
void swap(Sample &lhs, Sample &rhs)
Definition: sample.cpp:104
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
std.hpp
Provides some valuable additions to STD.
genesis::utils::SvgDefinitions::operator=
SvgDefinitions & operator=(SvgDefinitions other)
Definition: definitions.hpp:72
genesis::utils::SvgDefinitions::~SvgDefinitions
~SvgDefinitions()=default
genesis::utils::SvgDefinitions
Definition: definitions.hpp:47
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::SvgDefinitions::write
void write(std::ostream &out, size_t indent=0) const
Definition: definitions.hpp:87
genesis::utils::SvgDefinitions::SvgDefinitions
SvgDefinitions(SvgDefinitions const &other)
Definition: definitions.hpp:66
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::SvgDefinitions::SvgDefinitions
SvgDefinitions(T const &object)
Definition: definitions.hpp:62