1 #ifndef WS_PROJECT_DEF_H_INCLUDED
2 #define WS_PROJECT_DEF_H_INCLUDED
28 #include <boost/property_tree/xml_parser.hpp>
30 #include <QFileSystemWatcher>
41 template <
class FileType, const std::
string& index_file>
43 const string& prefs_file,
const shared_ptr<IWorkspace>& w)
44 : Preferences(path +
'/' + prefs_file),
47 m_filesystem_watcher(path, bind(&Project<FileType, index_file>::Refresh, this))
50 QDir qd(QString::fromStdString(
m_path));
53 QFileInfoList l = qd.entryInfoList(qfilters, QDir::Files);
55 string filename = it.fileName().toStdString();
56 auto file = FileType::Constructor(filename,
m_path +
'/' + filename);
63 read_xml(
m_path +
'/' + index_file, p, trim_whitespace);
67 catch (xml_parser_error& e) {}
68 catch (ptree_bad_path &) {}
72 template <
class FileType, const std::
string& index_file>
75 m_path(move(other.m_path)),
76 m_files(move(other.m_files)),
77 m_filesystem_watcher(move(other.m_filesystem_watcher))
81 template <
class FileType, const std::
string& index_file>
84 auto file = FileType::Constructor(name, m_path +
'/' + name);
86 FileMap::value_type v(name, file);
87 auto result = m_files.insert(v).first;
89 {Event::ProjectChanged::LeafAdded, name});
96 template <
class FileType, const std::
string& index_file>
99 return (--m_files.end())->second;
102 template <
class FileType, const std::
string& index_file>
105 return (--m_files.end())->second;
108 template <
class FileType, const std::
string& index_file>
109 IProject::FileIterator
112 return m_files.begin();
115 template <
class FileType, const std::
string& index_file>
116 IProject::ConstFileIterator
119 return m_files.begin();
122 template <
class FileType, const std::
string& index_file>
127 m_session->StopSimulation();
129 m_session_name.clear();
132 template <
class FileType, const std::
string& index_file>
133 IProject::FileIterator
136 return m_files.end();
139 template <
class FileType, const std::
string& index_file>
140 IProject::ConstFileIterator
143 return m_files.end();
146 template <
class FileType, const std::
string& index_file>
147 IProject::FileIterator
150 return m_files.find(name);
153 template <
class FileType, const std::
string& index_file>
154 IProject::ConstFileIterator
157 auto it = m_files.begin();
158 for (; it != m_files.end(); it++) {
159 if (it->first == name)
165 template <
class FileType, const std::
string& index_file>
168 return m_files.begin()->second;
171 template <
class FileType, const std::
string& index_file>
174 return m_files.begin()->second;
177 template <
class FileType, const std::
string& index_file>
180 auto it = m_files.find(name);
181 if (it == m_files.end()) {
183 ss <<
"No file \"" << name <<
"\" in project \"" << m_path <<
"\"";
189 template <
class FileType, const std::
string& index_file>
192 auto it = m_files.find(name);
193 if (it == m_files.end()) {
195 ss <<
"No file \"" << name <<
"\" in project \"" << m_path <<
"\"";
201 template <
class FileType, const std::
string& index_file>
207 template <
class FileType, const std::
string& index_file>
213 template <
class FileType, const std::
string& index_file>
217 throw Exception(
"Called GetSession() while project was not opened.");
221 template <
class FileType, const std::
string& index_file>
224 return m_session_name;
227 template <
class FileType, const std::
string& index_file>
230 auto result_optional = m_user_data.get_child_optional(user);
231 if (result_optional) {
232 return result_optional.get();
234 auto& result = m_user_data.put_child(user, ptree());
239 template <
class FileType, const std::
string& index_file>
242 return m_files.find(name) != end();
245 template <
class FileType, const std::
string& index_file>
248 return (
bool) m_session;
251 template <
class FileType, const std::
string& index_file>
254 return m_filesystem_watcher.IsActive();
257 template <
class FileType, const std::
string& index_file>
263 template <
class FileType, const std::
string& index_file>
269 template <
class FileType, const std::
string& index_file>
274 m_session = it->second->CreateSession(shared_ptr<IProject>(
this, [](
IProject*){}), m_workspace);
275 m_session_name = it->first;
278 template <
class FileType, const std::
string& index_file>
282 QDir qd(QString::fromStdString(m_path));
284 QFileInfoList l = qd.entryInfoList(filters, QDir::Files);
287 vector<string> to_delete(m_files.size());
288 for (
auto& file : m_files) {
291 if (it.fileName().toStdString() == file.first) {
297 to_delete.push_back(file.first);
301 for (
auto& filename : to_delete) {
302 m_files.erase(filename);
304 {Event::ProjectChanged::LeafRemoved, filename});
309 string filename = it.fileName().toStdString();
311 if (m_files.find(filename) == m_files.end()) {
312 auto file = FileType::Constructor(filename, m_path +
'/' + filename);
314 m_files[filename] = file;
316 {Event::ProjectChanged::LeafAdded, filename});
322 template <
class FileType, const std::
string& index_file>
325 auto it = m_files.find(name);
326 if (it == m_files.end()) {
328 ss <<
"No file \"" << name <<
"\" in project \"" << m_path <<
"\"";
334 template <
class FileType, const std::
string& index_file>
338 string name = it->first;
339 QFile::remove(QString::fromStdString(it->second->GetPath()));
342 {Event::ProjectChanged::LeafRemoved, name});
345 template <
class FileType, const std::
string& index_file>
349 throw Exception(
"Called Session() while project was not opened.");
351 return *m_session.get();
354 template <
class FileType, const std::
string& index_file>
357 m_filesystem_watcher.SetActive(a);
360 template <
class FileType, const std::
string& index_file>
363 m_user_data.put_child(user, p);
367 template <
class FileType, const std::
string& index_file>
371 pt_project.put_child(
"project.user_data", m_user_data);
372 write_xml(m_path +
'/' + index_file, pt_project, std::locale(), XmlWriterSettings::GetTab());
378 #endif // end_of_inclde_guard
Implementation of IPreferences.
virtual FileIterator Add(const std::string &name)
Interface for project-like behavior.
Namespace for miscellaneous utilities.
virtual bool IsWatchingDirectory() const
virtual Session::ISession & Session() const
virtual void Remove(const std::string &name)
virtual void SetWatchingDirectory(bool)
virtual std::shared_ptr< IFile > Get(const std::string &name)
Abstraction of project in workspace on file system.
boost::property_tree::ptree m_user_data
User data. Mutable because a GetUserData() with non-existing argument will create an empty user data ...
virtual std::string GetSessionFileName() const
Extremely simple Exception root class.
virtual bool IsLeaf(const std::string &name) const
Project(const std::string &path, const std::string &prefs_file, const std::shared_ptr< IWorkspace > &w)
Constructor.
virtual FileIterator begin()
virtual bool IsOpened() const
virtual std::shared_ptr< Session::ISession > GetSession() const
virtual std::shared_ptr< IFile > Back()
virtual FileIterator end()
std::string m_path
Path of project directory.
virtual void SetUserData(const std::string &user, const boost::property_tree::ptree &)
FileMap m_files
Mapping from file name to file objects.
Subject in Observer pattern.
virtual std::shared_ptr< IFile > Front()
virtual const std::string & GetPath() const
Header file for Exception class.
Xml writer settings class.
virtual const std::string & GetIndexFile() const
Namespace for generic graphical shell for simulators.
virtual const boost::property_tree::ptree & GetUserData(const std::string &user) const
Generic session interface.
virtual FileIterator Find(const std::string &name)