VPTissue Reference Manual
FileViewer.h
Go to the documentation of this file.
1 #ifndef VIEW_FILE_VIEWER_H_INCLUDED
2 #define VIEW_FILE_VIEWER_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 "../../../cpp_simptshell/viewers/common/FileViewerPreferences.h"
23 #include "../../../cpp_simptshell/viewers/common/PreferencesObserver.h"
25 #include <string>
26 #include <sstream>
27 
28 namespace SimPT_Shell {
29 
34 template <typename FileType>
36 {
37 public:
38  FileViewer(const std::shared_ptr<SimShell::Ws::MergedPreferences>&);
39 
40  FileViewer(const std::shared_ptr<SimShell::Ws::MergedPreferences>&, std::string const & dir_path);
41 
43  virtual ~FileViewer() {}
44 
46  template <typename EventType>
47  void Update(const EventType& e);
48 
49 private:
51 
52 private:
54  std::string MakeFilepath(const std::string& file_name) const;
55 
56 private:
57  std::shared_ptr<prefs_type> m_preferences;
58  std::string m_dir_path;
59  FileType m_exporter;
60 };
61 
62 //-----------------------------------------------------------------------------------
63 // Primary implementations
64 //-----------------------------------------------------------------------------------
65 
66 template <typename FileType>
67 FileViewer<FileType>::FileViewer(const std::shared_ptr<SimShell::Ws::MergedPreferences>& p)
68  : m_preferences(prefs_type::Create(p)),
69  m_dir_path(p->GetPath()),
70  m_exporter(MakeFilepath(m_preferences->m_file))
71 {
72 }
73 
74 template <typename FileType>
75 std::string FileViewer<FileType>::MakeFilepath(const std::string& file_name) const
76 {
77  std::string const postfix = FileType::GetFileExtension();
78  std::stringstream file_path;
79  file_path << m_dir_path << '/' << file_name << "." << postfix;
80  return file_path.str();
81 }
82 
83 template <typename FileType>
84 template <typename EventType>
85 void FileViewer<FileType>::Update(const EventType& e)
86 {
87  // Unwrap event data
88  typename EventType::Source es = e.GetSource();
89  const int step = e.GetStep();
90  typename EventType::Type et = e.GetType();
91 
92  const bool check = (et == EventType::Type::Forced)
93  || (et == EventType::Type::Done && m_preferences->m_stride == 0)
94  || (et == EventType::Type::Stepped && ((m_preferences->m_stride != 0) && (step % m_preferences->m_stride == 0)));
95 
96  // Check whether writing is required
97  if (m_exporter.IsOpen() && check) {
98  m_exporter.Write(es);
99  }
100 }
101 
102 } // namespace
103 
104 #endif // end_of_include_guard
Namespace for SimPT shell package.
Definition: Client.cpp:50
Type
Enumerates the wall types.
Definition: WallType.h:27
virtual ~FileViewer()
Destructor must be virtual.
Definition: FileViewer.h:43
void Update(const EventType &e)
Check whether export should happen and then do so.
Definition: FileViewer.h:85
Interface for MergedPreferences.
Viewer that writes to file and needs to maintain some state info w.r.t.
Definition: FileViewer.h:35
Listener for SimShell::Event::MergedPreferencesChanged events.