VPTissue Reference Manual
TextViewer.h
Go to the documentation of this file.
1 #ifndef VIEW_TEXT_VIEWER_H_INCLUDED
2 #define VIEW_TEXT_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 "PreferencesObserver.h"
23 #include "TextViewerPreferences.h"
25 
26 #include <memory>
27 #include <sstream>
28 #include <string>
29 
30 namespace SimPT_Shell {
31 
36 template <typename Exporter, typename GzExporter>
38 {
39 public:
40  TextViewer(const std::shared_ptr<SimShell::Ws::MergedPreferences>& p)
41  : m_preferences(prefs_type::Create(p)), m_dir_path(p->GetPath()) {}
42 
43  TextViewer(const std::shared_ptr<SimShell::Ws::MergedPreferences>& p, const std::string& dir_path)
44  : m_preferences(prefs_type::Create(p)), m_dir_path(dir_path) {}
45 
47  virtual ~TextViewer() {}
48 
50  template <typename EventType>
51  void Update(EventType const& e)
52  {
53  typename EventType::Source es = e.GetSource();
54  const int step = e.GetStep();
55  typename EventType::Type et = e.GetType();
56 
57  const bool check = (et == EventType::Type::Forced)
58  || (et == EventType::Type::Stepped &&
59  (m_preferences->m_stride != 0 && (step % m_preferences->m_stride == 0)))
60  || (et == EventType::Type::Done && m_preferences->m_stride == 0);
61  if (check) {
62  if (m_preferences->m_gzip)
63  GzExporter::Export(es, MakeLabeledFilepath(step));
64  else
65  Exporter::Export(es, MakeLabeledFilepath(step));
66  }
67  }
68 
69 private:
71 
72 private:
74  std::string MakeLabeledFilepath(int simstep) const
75  {
76  const std::string postfix = m_preferences->m_gzip
77  ? GzExporter::GetFileExtension() : Exporter::GetFileExtension();
78  std::stringstream file_path;
79  file_path << m_dir_path << '/' << "leaf_";
80  file_path.fill('0');
81  file_path.width(6);
82  file_path << simstep << "." << postfix;
83  return file_path.str();
84  }
85 
86 private:
87  std::shared_ptr<prefs_type> m_preferences;
88  std::string m_dir_path;
89 };
90 
91 } // namespace
92 
93 #endif // end_of_include_guard
virtual ~TextViewer()
Destructor must be virtual.
Definition: TextViewer.h:47
Namespace for SimPT shell package.
Definition: Client.cpp:50
Type
Enumerates the wall types.
Definition: WallType.h:27
Interface/Implementation for TextViewerPreferences.
Viewer that periodically exports mesh to a text file, opening writing and closing the file again duri...
Definition: TextViewer.h:37
Interface for MergedPreferences.
Preferences observer.
void Update(EventType const &e)
Check whether export should happen and then do so.
Definition: TextViewer.h:51
Listener for SimShell::Event::MergedPreferencesChanged events.