gem5  v22.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 namespace gem5
52 {
53 
54 class OutputDirectory;
55 
57 {
58  public:
59  virtual ~OutputStream();
60 
62  std::ostream *stream() const { return _stream; };
63 
70  virtual bool recreateable() const { return false; }
71 
73  const std::string &name() const { return _name; }
74 
75  protected:
76  friend class OutputDirectory;
77 
79  OutputStream(const std::string &name,
80  std::ostream *stream);
81 
82  /* Prevent copying */
84 
86  virtual void relocate(const OutputDirectory &dir);
87 
89  const std::string _name;
90 
92  std::ostream *const _stream;
93 };
94 
95 template<class StreamType>
97  : public OutputStream
98 {
99  public:
100  typedef StreamType stream_type_t;
101 
102  virtual ~OutputFile();
103 
110  bool recreateable() const override { return _recreateable; }
111 
112  protected:
113  friend class OutputDirectory;
114 
115  OutputFile(const OutputDirectory &dir,
116  const std::string &name,
117  std::ios_base::openmode mode,
118  bool recreateable);
119 
120  /* Prevent copying */
122 
124  void relocate(const OutputDirectory &dir) override;
125 
127  const std::ios_base::openmode _mode;
128 
130  const bool _recreateable;
131 
134 };
135 
138 {
139  private:
141  typedef std::map<std::string, OutputStream *> file_map_t;
142 
144  typedef std::map<std::string, OutputDirectory *> dir_map_t;
145 
148 
151 
153  std::string dir;
154 
156  static const char PATH_SEPARATOR = '/';
157 
160 
161  protected:
170  static OutputStream *checkForStdio(const std::string &name);
171 
172  public:
174  OutputDirectory();
175 
177  OutputDirectory(const std::string &name);
178 
181 
190  std::string resolve(const std::string &name) const;
191 
196  void setDirectory(const std::string &dir);
197 
202  const std::string &directory() const;
203 
222  OutputStream *create(const std::string &name,
223  bool binary = false,
224  bool no_gz = false);
225 
240  OutputStream *open(const std::string &name,
241  std::ios_base::openmode mode,
242  bool recreateable = true,
243  bool no_gz = false);
244 
254  void close(OutputStream *file);
255 
262  OutputStream *find(const std::string &name) const;
263 
264  OutputStream *findOrCreate(const std::string &name, bool binary = false);
265 
272  bool isFile(const std::string &name) const;
273 
277  static inline bool isAbsolute(const std::string &name) {
278  return name[0] == PATH_SEPARATOR;
279  }
280 
286  OutputDirectory *createSubdirectory(const std::string &name);
287 
302  void remove(const std::string &name, bool recursive=false);
303 };
304 
305 extern OutputDirectory simout;
306 
307 } // namespace gem5
308 
309 #endif // __BASE_OUTPUT_HH__
Interface for creating files in a gem5 output directory.
Definition: output.hh:138
void close(OutputStream *file)
Closes an output file and free the corresponding OutputFile.
Definition: output.cc:147
~OutputDirectory()
Destructor.
Definition: output.cc:126
std::string resolve(const std::string &name) const
Returns relative file names prepended with name of this directory.
Definition: output.cc:204
void remove(const std::string &name, bool recursive=false)
Removes a specified file or subdirectory.
Definition: output.cc:296
bool isFile(const std::string &name) const
Determines whether a file name corresponds to a file in this directory.
Definition: output.cc:272
static bool isAbsolute(const std::string &name)
Test if a path is absolute.
Definition: output.hh:277
std::string dir
Name of this directory.
Definition: output.hh:153
static OutputStream * checkForStdio(const std::string &name)
Determines whether given file name corresponds to standard output streams.
Definition: output.cc:135
OutputDirectory()
Constructor.
Definition: output.cc:118
OutputDirectory * createSubdirectory(const std::string &name)
Creates a subdirectory within this directory.
Definition: output.cc:283
static const char PATH_SEPARATOR
System-specific path separator character.
Definition: output.hh:156
static OutputStream stdout
Definition: output.hh:158
std::map< std::string, OutputStream * > file_map_t
File names and associated stream handles.
Definition: output.hh:141
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:224
const std::string & directory() const
Gets name of this directory.
Definition: output.cc:195
OutputStream * create(const std::string &name, bool binary=false, bool no_gz=false)
Creates a file in this directory (optionally compressed).
Definition: output.cc:210
dir_map_t dirs
Output sub-directories.
Definition: output.hh:150
void setDirectory(const std::string &dir)
Sets name of this directory.
Definition: output.cc:164
OutputStream * findOrCreate(const std::string &name, bool binary=false)
Definition: output.cc:262
OutputStream * find(const std::string &name) const
Finds stream associated with an open file or stdout/stderr.
Definition: output.cc:247
std::map< std::string, OutputDirectory * > dir_map_t
Output subdirectories.
Definition: output.hh:144
static OutputStream stderr
Definition: output.hh:159
file_map_t files
Open file streams within this directory.
Definition: output.hh:147
void relocate(const OutputDirectory &dir) override
Re-create the file in a new location if it is relocatable.
Definition: output.cc:104
OutputFile(const OutputDirectory &dir, const std::string &name, std::ios_base::openmode mode, bool recreateable)
Definition: output.cc:80
bool recreateable() const override
Can the file be recreated if the output directory is moved?
Definition: output.hh:110
stream_type_t *const _fstream
Pointer to the file stream.
Definition: output.hh:133
const std::ios_base::openmode _mode
File mode when opened.
Definition: output.hh:127
OutputFile(const OutputFile< StreamType > &f)
StreamType stream_type_t
Definition: output.hh:100
virtual ~OutputFile()
Definition: output.cc:96
const bool _recreateable
Can the file be recreated in a new location?
Definition: output.hh:130
std::ostream *const _stream
Underlying output stream.
Definition: output.hh:92
virtual void relocate(const OutputDirectory &dir)
Re-create the in a new location if recreateable.
Definition: output.cc:75
const std::string & name() const
Get the file name in the output directory.
Definition: output.hh:73
virtual ~OutputStream()
Definition: output.cc:70
std::ostream * stream() const
Get the output underlying output stream.
Definition: output.hh:62
const std::string _name
Name in output directory.
Definition: output.hh:89
OutputStream(const std::string &name, std::ostream *stream)
Wrap an existing stream.
Definition: output.cc:65
virtual bool recreateable() const
Can the file be recreated if the output directory is moved?
Definition: output.hh:70
OutputStream(const OutputStream &f)
Bitfield< 4, 0 > mode
Definition: misc_types.hh:74
Bitfield< 56 > f
Definition: pagetable.hh:53
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
OutputDirectory simout
Definition: output.cc:62
const std::string & name()
Definition: trace.cc:49

Generated on Wed Dec 21 2022 10:22:29 for gem5 by doxygen 1.9.1