STIR 6.4.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
26START_NAMESPACE_STIR
27
30
44template <class DataT, class FileT>
45inline unique_ptr<DataT>
46read_from_file(const FileSignature& signature, FileT file)
47{
48 using hierarchy_base_type = typename DataT::hierarchy_base_type;
50 = InputFileFormatRegistry<hierarchy_base_type>::default_sptr()->find_factory(signature, file);
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
84template <class DataT, class FileT>
85inline unique_ptr<DataT>
86read_from_file(FileT file)
87{
88 const FileSignature signature(file);
89 return read_from_file<DataT>(signature, file);
90}
91
92END_NAMESPACE_STIR
93
94#endif
Declaration of class stir::InputFileFormatRegistry, stir::RegisterInputFileFormat.
A class to read/store the file signature.
Definition FileSignature.h:35
static shared_ptr< self_type > & default_sptr()
A function to return the default registry.
Base-class for file-formats for reading.
Definition InputFileFormat.h:40
virtual unique_ptr< DataT > read_from_file(std::istream &input) const =0
read data from a stream
unique_ptr< DataT > read_from_file(const FileSignature &signature, FileT file)
Function that reads data from file using the default InputFileFormatRegistry, using the provided File...
Definition read_from_file.h:46
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.