VPTissue Reference Manual
MergedPreferences.h
Go to the documentation of this file.
1 #ifndef WS_MERGED_PREFERENCES_H_INCLUDED
2 #define WS_MERGED_PREFERENCES_H_INCLUDED
3 /*
4  * Copyright 2011-2016 Universiteit Antwerpen
5  *
6  * Licensed under the EUPL, Version 1.1 or as soon they will be approved by
7  * the European Commission - subsequent versions of the EUPL (the "Licence");
8  * You may not use this work except in compliance with the Licence.
9  * You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl5
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the Licence is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the Licence for the specific language governing
15  * permissions and limitations under the Licence.
16  */
22 #include "util/misc/Subject.h"
23 #include "workspace/IWorkspace.h"
24 #include "workspace/IProject.h"
25 
26 #include <type_traits>
27 #include <boost/property_tree/ptree.hpp>
28 
29 namespace SimShell
30 {
31 namespace Ws
32 {
33 
34 class MergedPreferences;
35 
36 namespace Event {
38  const std::shared_ptr<MergedPreferences> source;
39  };
40 }
41 
50 class MergedPreferences : public SimPT_Sim::Util::Subject<Event::MergedPreferencesChanged, std::weak_ptr<const void>>,
51  public std::enable_shared_from_this<MergedPreferences>
52 {
53 public:
54  static std::shared_ptr<MergedPreferences> Create(const std::shared_ptr<Ws::IWorkspace>&,
55  const std::shared_ptr<Ws::IProject>&);
56 
57  virtual ~MergedPreferences();
58 
61  template <typename T>
62  typename std::enable_if<std::is_same<T, std::string>::value, T>::type
63  Get(const boost::property_tree::ptree::path_type&) const;
64 
67  template <typename T>
68  typename std::enable_if<!std::is_same<T, std::string>::value, T>::type
69  Get(const boost::property_tree::ptree::path_type&) const;
70 
72  template <typename T>
73  typename std::enable_if<std::is_same<T, std::string>::value, T>::type
74  Get(const boost::property_tree::ptree::path_type&, const T&) const;
75 
77  template <typename T>
78  typename std::enable_if<!std::is_same<T, std::string>::value, T>::type
79  Get(const boost::property_tree::ptree::path_type&, const T&) const;
80 
86  template <typename T>
87  void Put(const boost::property_tree::ptree::path_type&, const T&);
88 
90  std::shared_ptr<MergedPreferences> GetChild(const boost::property_tree::ptree::path_type&) const;
91 
93  std::shared_ptr<MergedPreferences> GetGlobal() const;
94 
96  const std::string& GetPath() const;
97 
99  const boost::property_tree::ptree& GetProjectPreferencesTree();
100 
101 private:
102  MergedPreferences(const std::shared_ptr<Ws::IWorkspace>&,
103  const std::shared_ptr<Ws::IProject>&);
104 
105  MergedPreferences(const boost::property_tree::ptree::path_type&, const MergedPreferences&);
106 
107  void ListenPreferencesChanged(const Ws::Event::PreferencesChanged&);
108 
109  boost::property_tree::ptree::path_type m_path;
110  std::shared_ptr<Ws::IWorkspace> m_workspace;
111  std::shared_ptr<Ws::IProject> m_project;
112 };
113 
114 template <typename T>
115 typename std::enable_if<std::is_same<T, std::string>::value, T>::type
116 MergedPreferences::Get(const boost::property_tree::ptree::path_type& p) const
117 {
118  auto str = m_project->GetPreferences().get<std::string>(m_path / p); // may throw ptree_bad_path
119  if (str == "$WORKSPACE$") {
120  return m_workspace->GetPreferences().get<T>(m_path / p); // may throw either ptree_bad_path or ptree_bad_data but probably won't
121  } else {
122  return str;
123  }
124 }
125 
126 template <typename T>
127 typename std::enable_if<!std::is_same<T, std::string>::value, T>::type
128 MergedPreferences::Get(const boost::property_tree::ptree::path_type& p) const
129 {
130  auto str = m_project->GetPreferences().get<std::string>(m_path / p); // may throw ptree_bad_path
131  if (str == "$WORKSPACE$") {
132  return m_workspace->GetPreferences().get<T>(m_path / p); // may throw either ptree_bad_path or ptree_bad_data but probably won't
133  } else {
134  return m_project->GetPreferences().get<T>(m_path / p); // may throw ptree_bad_data
135  }
136 }
137 
138 template <typename T>
139 typename std::enable_if<std::is_same<T, std::string>::value, T>::type
140 MergedPreferences::Get(const boost::property_tree::ptree::path_type& p, const T& v) const
141 {
142  auto str_optional = m_project->GetPreferences().get_optional<std::string>(m_path / p); // won't throw
143  if (str_optional) {
144  auto& str = str_optional.get();
145  if (str == "$WORKSPACE$") {
146  return m_workspace->GetPreferences().get<T>(m_path / p); // may throw either ptree_bad_path or ptree_bad_data but probably won't
147  } else {
148  return str;
149  }
150  } else {
151  return v;
152  }
153 }
154 
155 template <typename T>
156 typename std::enable_if<!std::is_same<T, std::string>::value, T>::type
157 MergedPreferences::Get(const boost::property_tree::ptree::path_type& p, const T& v) const
158 {
159  auto str_optional = m_project->GetPreferences().get_optional<std::string>(m_path / p); // won't throw
160  if (str_optional) {
161  auto& str = str_optional.get();
162  if (str == "$WORKSPACE$") {
163  return m_workspace->GetPreferences().get<T>(m_path / p); // may throw either ptree_bad_path or ptree_bad_data but probably won't
164  } else {
165  return m_project->GetPreferences().get<T>(m_path / p); // may throw ptree_bad_data
166  }
167  } else {
168  return v;
169  }
170 }
171 
172 template <typename T>
173 inline void MergedPreferences::Put(const boost::property_tree::ptree::path_type& p, const T& v)
174 {
175  auto prefs = m_project->GetPreferences(); // copy
176  prefs.put<T>(m_path / p, v);
177  m_project->SetPreferences(prefs);
178 }
179 
180 } // namespace
181 } // namespace
182 
183 #endif // end_of_inclde_guard
Event used to inform some observer that preferences have changed.
const std::string & GetPath() const
Get path of project.
std::shared_ptr< MergedPreferences > GetGlobal() const
Get MergedPreferences instance pointing to global preferences. (undoes GetChild() operations) ...
Wrapper around workspace to have read and write access to merged (workspace + project) preferences...
void Put(const boost::property_tree::ptree::path_type &, const T &)
Put value at given path in project preferences if another value at the same path exists, otherwise, put value in workspace preferences.
Interface for IProject.
Interface/Implementation of Subject.
std::shared_ptr< MergedPreferences > GetChild(const boost::property_tree::ptree::path_type &) const
Get MergedPreferences instance for working with preferences subtree.
Interface for IWorkspace.
const boost::property_tree::ptree & GetProjectPreferencesTree()
Get project preferences.
Subject in Observer pattern.
Definition: Subject.h:34
Namespace for generic graphical shell for simulators.
Definition: SimSession.h:32
std::enable_if< std::is_same< T, std::string >::value, T >::type Get(const boost::property_tree::ptree::path_type &) const
see the online C++11 documentation