gem5  v20.1.0.0
output.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 ARM Limited
3  * All rights reserved
4  *
5  * The license below extends only to copyright in the software and shall
6  * not be construed as granting a license to any other intellectual
7  * property including but not limited to intellectual property relating
8  * to a hardware implementation of the functionality of the software
9  * licensed hereunder. You may use the software subject to the license
10  * terms below provided that you ensure that this notice is replicated
11  * unmodified and in its entirety in all distributions of the software,
12  * modified or unmodified, in source code or in binary form.
13  *
14  * Copyright (c) 2013 Andreas Sandberg
15  * Copyright (c) 2005 The Regents of The University of Michigan
16  * All rights reserved.
17  *
18  * Redistribution and use in source and binary forms, with or without
19  * modification, are permitted provided that the following conditions are
20  * met: redistributions of source code must retain the above copyright
21  * notice, this list of conditions and the following disclaimer;
22  * redistributions in binary form must reproduce the above copyright
23  * notice, this list of conditions and the following disclaimer in the
24  * documentation and/or other materials provided with the distribution;
25  * neither the name of the copyright holders nor the names of its
26  * contributors may be used to endorse or promote products derived from
27  * this software without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40  */
41 
42 #ifndef __BASE_OUTPUT_HH__
43 #define __BASE_OUTPUT_HH__
44 
45 #include <ios>
46 #include <map>
47 #include <string>
48 
49 #include "base/compiler.hh"
50 
51 class OutputDirectory;
52 
54 {
55  public:
56  virtual ~OutputStream();
57 
59  std::ostream *stream() const { return _stream; };
60 
67  virtual bool recreateable() const { return false; }
68 
70  const std::string &name() const { return _name; }
71 
72  protected:
73  friend class OutputDirectory;
74 
76  OutputStream(const std::string &name,
77  std::ostream *stream);
78 
79  /* Prevent copying */
80  OutputStream(const OutputStream &f);
81 
83  virtual void relocate(const OutputDirectory &dir);
84 
86  const std::string _name;
87 
89  std::ostream *const _stream;
90 };
91 
92 template<class StreamType>
94  : public OutputStream
95 {
96  public:
97  typedef StreamType stream_type_t;
98 
99  virtual ~OutputFile();
100 
107  bool recreateable() const override { return _recreateable; }
108 
109  protected:
110  friend class OutputDirectory;
111 
113  const std::string &name,
114  std::ios_base::openmode mode,
115  bool recreateable);
116 
117  /* Prevent copying */
119 
121  void relocate(const OutputDirectory &dir) override;
122 
124  const std::ios_base::openmode _mode;
125 
127  const bool _recreateable;
128 
131 };
132 
135 {
136  private:
138  typedef std::map<std::string, OutputStream *> file_map_t;
139 
141  typedef std::map<std::string, OutputDirectory *> dir_map_t;
142 
145 
148 
150  std::string dir;
151 
153  static const char PATH_SEPARATOR = '/';
154 
157 
158  protected:
167  static OutputStream *checkForStdio(const std::string &name);
168 
169  public:
171  OutputDirectory();
172 
174  OutputDirectory(const std::string &name);
175 
178 
187  std::string resolve(const std::string &name) const;
188 
193  void setDirectory(const std::string &dir);
194 
199  const std::string &directory() const;
200 
219  OutputStream *create(const std::string &name,
220  bool binary = false,
221  bool no_gz = false);
222 
237  OutputStream *open(const std::string &name,
238  std::ios_base::openmode mode,
239  bool recreateable = true,
240  bool no_gz = false);
241 
251  void close(OutputStream *file);
252 
259  OutputStream *find(const std::string &name) const;
260 
261  OutputStream *findOrCreate(const std::string &name, bool binary = false);
262 
269  bool isFile(const std::string &name) const;
270 
274  static inline bool isAbsolute(const std::string &name) {
275  return name[0] == PATH_SEPARATOR;
276  }
277 
283  OutputDirectory *createSubdirectory(const std::string &name);
284 
299  void remove(const std::string &name, bool recursive=false);
300 };
301 
302 extern OutputDirectory simout;
303 
304 #endif // __BASE_OUTPUT_HH__
OutputFile::_recreateable
const bool _recreateable
Can the file be recreated in a new location?
Definition: output.hh:127
simout
OutputDirectory simout
Definition: output.cc:61
OutputDirectory::~OutputDirectory
~OutputDirectory()
Destructor.
Definition: output.cc:125
OutputDirectory
Interface for creating files in a gem5 output directory.
Definition: output.hh:134
OutputStream::_stream
std::ostream *const _stream
Underlying output stream.
Definition: output.hh:89
OutputDirectory::find
OutputStream * find(const std::string &name) const
Finds stream associated with an open file or stdout/stderr.
Definition: output.cc:246
OutputDirectory::OutputDirectory
OutputDirectory()
Constructor.
Definition: output.cc:117
OutputStream::_name
const std::string _name
Name in output directory.
Definition: output.hh:86
OutputDirectory::PATH_SEPARATOR
static const char PATH_SEPARATOR
System-specific path separator character.
Definition: output.hh:153
OutputStream::name
const std::string & name() const
Get the file name in the output directory.
Definition: output.hh:70
OutputDirectory::create
OutputStream * create(const std::string &name, bool binary=false, bool no_gz=false)
Creates a file in this directory (optionally compressed).
Definition: output.cc:209
OutputDirectory::setDirectory
void setDirectory(const std::string &dir)
Sets name of this directory.
Definition: output.cc:163
OutputDirectory::createSubdirectory
OutputDirectory * createSubdirectory(const std::string &name)
Creates a subdirectory within this directory.
Definition: output.cc:282
OutputDirectory::resolve
std::string resolve(const std::string &name) const
Returns relative file names prepended with name of this directory.
Definition: output.cc:203
OutputDirectory::isFile
bool isFile(const std::string &name) const
Determines whether a file name corresponds to a file in this directory.
Definition: output.cc:271
OutputDirectory::isAbsolute
static bool isAbsolute(const std::string &name)
Test if a path is absolute.
Definition: output.hh:274
OutputDirectory::findOrCreate
OutputStream * findOrCreate(const std::string &name, bool binary=false)
Definition: output.cc:261
OutputStream::~OutputStream
virtual ~OutputStream()
Definition: output.cc:69
OutputDirectory::stdout
static OutputStream stdout
Definition: output.hh:155
OutputStream::stream
std::ostream * stream() const
Get the output underlying output stream.
Definition: output.hh:59
OutputDirectory::dir
std::string dir
Name of this directory.
Definition: output.hh:150
OutputFile::_mode
const std::ios_base::openmode _mode
File mode when opened.
Definition: output.hh:124
OutputDirectory::files
file_map_t files
Open file streams within this directory.
Definition: output.hh:144
ArmISA::mode
Bitfield< 4, 0 > mode
Definition: miscregs_types.hh:70
OutputDirectory::file_map_t
std::map< std::string, OutputStream * > file_map_t
File names and associated stream handles.
Definition: output.hh:138
OutputDirectory::close
void close(OutputStream *file)
Closes an output file and free the corresponding OutputFile.
Definition: output.cc:146
compiler.hh
OutputFile::stream_type_t
StreamType stream_type_t
Definition: output.hh:97
name
const std::string & name()
Definition: trace.cc:50
OutputFile
Definition: output.hh:93
OutputDirectory::checkForStdio
static OutputStream * checkForStdio(const std::string &name)
Determines whether given file name corresponds to standard output streams.
Definition: output.cc:134
OutputDirectory::stderr
static OutputStream stderr
Definition: output.hh:156
OutputFile::OutputFile
OutputFile(const OutputDirectory &dir, const std::string &name, std::ios_base::openmode mode, bool recreateable)
Definition: output.cc:79
OutputFile::_fstream
stream_type_t *const _fstream
Pointer to the file stream.
Definition: output.hh:130
OutputFile::~OutputFile
virtual ~OutputFile()
Definition: output.cc:95
OutputStream
Definition: output.hh:53
OutputStream::relocate
virtual void relocate(const OutputDirectory &dir)
Re-create the in a new location if recreateable.
Definition: output.cc:74
OutputDirectory::dirs
dir_map_t dirs
Output sub-directories.
Definition: output.hh:147
OutputStream::recreateable
virtual bool recreateable() const
Can the file be recreated if the output directory is moved?
Definition: output.hh:67
OutputFile::relocate
void relocate(const OutputDirectory &dir) override
Re-create the file in a new location if it is relocatable.
Definition: output.cc:103
OutputDirectory::open
OutputStream * open(const std::string &name, std::ios_base::openmode mode, bool recreateable=true, bool no_gz=false)
Open a file in this directory (optionally compressed).
Definition: output.cc:223
OutputDirectory::remove
void remove(const std::string &name, bool recursive=false)
Removes a specified file or subdirectory.
Definition: output.cc:295
OutputDirectory::dir_map_t
std::map< std::string, OutputDirectory * > dir_map_t
Output subdirectories.
Definition: output.hh:141
OutputDirectory::directory
const std::string & directory() const
Gets name of this directory.
Definition: output.cc:194
OutputFile::recreateable
bool recreateable() const override
Can the file be recreated if the output directory is moved?
Definition: output.hh:107
OutputStream::OutputStream
OutputStream(const std::string &name, std::ostream *stream)
Wrap an existing stream.
Definition: output.cc:64
ArmISA::f
Bitfield< 6 > f
Definition: miscregs_types.hh:64

Generated on Wed Sep 30 2020 14:02:07 for gem5 by doxygen 1.8.17