forqs module reference
Forward simulation of Recombination, Quantitative traits, and Selection
 All Classes Groups Pages
Configurable.hpp
1 //
2 // Configurable.hpp
3 //
4 // Created by Darren Kessner with John Novembre
5 //
6 // Copyright (c) 2013 Regents of the University of California
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are met:
11 //
12 // * Redistributions of source code must retain the above copyright notice,
13 // this list of conditions and the following disclaimer.
14 //
15 // * Redistributions in binary form must reproduce the above copyright notice,
16 // this list of conditions and the following disclaimer in the documentation
17 // and/or other materials provided with the distribution.
18 //
19 // * Neither UCLA nor the names of its contributors may be used to endorse or
20 // promote products derived from this software without specific prior
21 // written permission.
22 //
23 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
27 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 //
34 
35 
36 #ifndef _CONFIGURABLE_HPP_
37 #define _CONFIGURABLE_HPP_
38 
39 
40 #include "Parameters.hpp"
41 #include "shared_ptr.hpp"
42 #include <string>
43 #include <set>
44 #include <stdexcept>
45 
46 
47 struct SimulatorConfig;
48 
49 
51 {
52  public:
53 
54  class Registry;
55 
56  Configurable(const std::string& id) : id_(id) {}
57  const std::string& object_id() const {return id_;}
58 
59  virtual std::string class_name() const = 0;
60  virtual Parameters parameters() const = 0;
61  virtual void configure(const Parameters& parameters, const Registry& registry) = 0;
62  virtual void initialize(const SimulatorConfig& config) {}
63 
64  void write_configuration(std::ostream& os, std::set<std::string>& ids_written) const;
65  virtual void write_child_configurations(std::ostream& os, std::set<std::string>& ids_written) const {}
66 
67  virtual ~Configurable() {}
68 
69  private:
70 
71  std::string id_;
72 };
73 
74 
75 typedef shared_ptr<Configurable> ConfigurablePtr;
76 typedef std::vector<ConfigurablePtr> ConfigurablePtrs;
77 
78 
79 class Configurable::Registry : public std::map<std::string, ConfigurablePtr>
80 {
81  public:
82 
83  template <typename result_type>
84  shared_ptr<result_type> get(const std::string& name) const
85  {
86  if (!count(name))
87  throw std::runtime_error(("[Configurable::Registry] Object id \"" + name + "\" requested, but not found in registry.").c_str());
88 
89  shared_ptr<result_type> result = dynamic_pointer_cast<result_type>(at(name));
90  if (!result.get()) // dynamic_pointer_cast doesn't throw
91  throw std::runtime_error(("[Configurable::Registry] Unable to convert object " + name).c_str());
92 
93  return result;
94  }
95 
96  template <typename result_type>
97  shared_ptr<result_type> get(const std::string& name, std::nothrow_t) const
98  {
99  if (!count(name)) return shared_ptr<result_type>();
100  return dynamic_pointer_cast<result_type>(at(name));
101  }
102 };
103 
104 
105 #endif // _CONFIGURABLE_HPP_
106 
107