STIR  6.2.0
read_from_file.h
Go to the documentation of this file.
1 //
2 //
3 #ifndef __stir_IO_read_from_file_H__
4 #define __stir_IO_read_from_file_H__
5 /*
6  Copyright (C) 2006 - 2008-10-01, Hammersmith Imanet Ltd
7  Copyright (C) 2011-07-01 - 2013, Kris Thielemans
8  This file is part of STIR.
9 
10  SPDX-License-Identifier: Apache-2.0
11 
12  See STIR/LICENSE.txt for details
13 */
24 #include "stir/unique_ptr.h"
25 
26 START_NAMESPACE_STIR
27 
30 
44 template <class DataT, class FileT>
45 inline unique_ptr<DataT>
46 read_from_file(const FileSignature& signature, FileT file)
47 {
48  using hierarchy_base_type = typename DataT::hierarchy_base_type;
51  auto uptr(factory.read_from_file(file));
52  // There is no dynamic_pointer_cast for unique_ptr
53  // See https://stackoverflow.com/questions/11002641/dynamic-casting-for-unique-ptr why
54  // We use a trick mentioned in that link
55  auto data_ptr = dynamic_cast<DataT*>(uptr.get());
56  if (!data_ptr)
57  {
58  // TODO improve on the following cryptic error message
59  error("data read from file is an incorrect type");
60  }
61  // get rid of the original uptr (but not the object pointed to) and create a new one
62  uptr.release();
63  return unique_ptr<DataT>(data_ptr);
64 }
65 
67 
84 template <class DataT, class FileT>
85 inline unique_ptr<DataT>
86 read_from_file(FileT file)
87 {
88  const FileSignature signature(file);
89  return read_from_file<DataT>(signature, file);
90 }
91 
92 END_NAMESPACE_STIR
93 
94 #endif
virtual unique_ptr< DataT > read_from_file(std::istream &input) const =0
read data from a stream
A class to read/store the file signature.
Definition: FileSignature.h:34
Factory const & find_factory(const FileSignature &signature, std::istream &input) const
Find a factory that can handle a particular stream.
unique_ptr< DataT > read_from_file(FileT file)
Function that reads data from file using the default InputFileFormatRegistry.
Definition: read_from_file.h:86
Declaration of class stir::InputFileFormatRegistry, stir::RegisterInputFileFormat.
Base-class for file-formats for reading.
Definition: InputFileFormat.h:39
void error(const char *const s,...)
Print error with format string a la printf and throw exception.
Definition: error.cxx:42
Import of std::unique_ptr into the stir namespace, together with work-arounds for other compilers...
A class for registering (and finding) all input file formats.
Definition: InputFileFormatRegistry.h:42