STIR  6.2.0
InputFileFormat.h
Go to the documentation of this file.
1 //
2 //
3 #ifndef __stir_IO_InputFileFormat_h__
4 #define __stir_IO_InputFileFormat_h__
5 /*
6  Copyright (C) 2006- 2011, Hammersmith Imanet Ltd
7  Copyright (C) 2021, University College London
8  This file is part of STIR.
9  SPDX-License-Identifier: Apache-2.0
10 
11  See STIR/LICENSE.txt for details
12 */
21 #include "stir/IO/FileSignature.h"
22 #include "stir/utilities.h"
23 #include "stir/unique_ptr.h"
24 #include <fstream>
25 #include <string>
26 
27 START_NAMESPACE_STIR
28 
30 
38 template <class DataT>
40 {
41 public:
42  typedef DataT data_type;
43  virtual ~InputFileFormat() {}
44 
45  virtual bool can_read(const FileSignature& signature, std::istream& input) const
46  {
47  return this->actual_can_read(signature, input);
48  }
49  virtual bool can_read(const FileSignature& signature, const std::string& filename) const
50  {
51  std::ifstream input;
52  open_read_binary(input, filename);
53  return this->actual_can_read(signature, input);
54  }
55 
57 
61  virtual unique_ptr<DataT> read_from_file(std::istream& input) const = 0;
62 
64 
68  virtual unique_ptr<DataT> read_from_file(const std::string& filename) const
69  {
70  std::ifstream input;
71  open_read_binary(input, filename);
72  return read_from_file(input);
73  }
74 
75  virtual const std::string get_name() const = 0;
76 
77 protected:
78  virtual bool actual_can_read(const FileSignature& signature, std::istream& input) const = 0;
79 };
80 
81 END_NAMESPACE_STIR
82 
83 #endif
A class to read/store the file signature.
Definition: FileSignature.h:34
This file declares various utility functions.
FILE *& open_read_binary(FILE *&fptr, const string &name)
opens a FILE for reading binary data. Calls error() when it does not succeed.
Definition: utilities.cxx:79
virtual unique_ptr< DataT > read_from_file(const std::string &filename) const
read data from a filename
Definition: InputFileFormat.h:68
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
Declaration of class stir::FileSignature.
Base-class for file-formats for reading.
Definition: InputFileFormat.h:39
Import of std::unique_ptr into the stir namespace, together with work-arounds for other compilers...