Flow123d  release_2.2.0-914-gf1a3a4f
reader_cache.cc
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License version 3 as published by the
7  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  *
14  * @file reader_cache.cc
15  * @brief
16  */
17 
18 #include "io/reader_cache.hh"
19 #include "io/msh_gmshreader.h"
20 #include "io/msh_vtkreader.hh"
21 #include "io/msh_pvdreader.hh"
22 #include "input/accessors.hh"
23 
24 
25 /***********************************************************************************************
26  * Implementation of ReaderCache
27  */
28 
30  static ReaderCache *instance = new ReaderCache;
31  return instance;
32 }
33 
34 std::shared_ptr<BaseMeshReader> ReaderCache::get_reader(const FilePath &file_path) {
35  return ReaderCache::get_reader_data(file_path)->second.reader_;
36 }
37 
38 std::shared_ptr<Mesh> ReaderCache::get_mesh(const FilePath &file_path) {
39  auto it = ReaderCache::get_reader_data(file_path);
40  // Create and fill mesh if doesn't exist
41  if ( (*it).second.mesh_ == nullptr ) {
42  (*it).second.mesh_ = std::make_shared<Mesh>( Input::Record() );
43  (*it).second.reader_->read_raw_mesh( (*it).second.mesh_.get() );
44  (*it).second.reader_->check_compatible_mesh( *((*it).second.mesh_) );
45 
46  }
47  return (*it).second.mesh_;
48 }
49 
50 ReaderCache::ReaderTable::iterator ReaderCache::get_reader_data(const FilePath &file_path) {
51  ReaderTable::iterator it = ReaderCache::instance()->reader_table_.find( string(file_path) );
52  if (it == ReaderCache::instance()->reader_table_.end()) {
53  ReaderData reader_data;
54  if ( file_path.extension() == ".msh" ) {
55  reader_data.reader_ = std::make_shared<GmshMeshReader>(file_path);
56  } else if ( file_path.extension() == ".vtu" ) {
57  reader_data.reader_ = std::make_shared<VtkMeshReader>(file_path);
58  } else if ( file_path.extension() == ".pvd" ) {
59  reader_data.reader_ = std::make_shared<PvdMeshReader>(file_path);
60  } else {
61  THROW(BaseMeshReader::ExcWrongExtension()
62  << BaseMeshReader::EI_FileExtension(file_path.extension()) << BaseMeshReader::EI_MeshFile((string)file_path) );
63  }
64  ReaderCache::instance()->reader_table_.insert( std::pair<string, ReaderData>(string(file_path), reader_data) );
65  it = ReaderCache::instance()->reader_table_.find( string(file_path) );
66  }
67  return it;
68 }
69 
ReaderCache()
Constructor.
Definition: reader_cache.hh:57
static ReaderCache * instance()
Returns singleton instance.
Definition: reader_cache.cc:29
Accessor to the data with type Type::Record.
Definition: accessors.hh:292
std::shared_ptr< BaseMeshReader > reader_
Definition: reader_cache.hh:36
static ReaderTable::iterator get_reader_data(const FilePath &file_path)
Returns instance of given FilePath. If reader doesn&#39;t exist, creates new ReaderData object...
Definition: reader_cache.cc:50
string extension() const
Definition: file_path.cc:198
Dedicated class for storing path to input and output files.
Definition: file_path.hh:54
ReaderTable reader_table_
Table of readers.
Definition: reader_cache.hh:63
static std::shared_ptr< Mesh > get_mesh(const FilePath &file_path)
Definition: reader_cache.cc:38
static std::shared_ptr< BaseMeshReader > get_reader(const FilePath &file_path)
Definition: reader_cache.cc:34
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:53