gem5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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  * Authors: Nathan Binkert
42  * Chris Emmons
43  * Andreas Sandberg
44  * Sascha Bischoff
45  */
46 
47 #ifndef __BASE_OUTPUT_HH__
48 #define __BASE_OUTPUT_HH__
49 
50 #include <ios>
51 #include <map>
52 #include <string>
53 
54 #include "base/compiler.hh"
55 
56 class OutputDirectory;
57 
59 {
60  public:
61  virtual ~OutputStream();
62 
64  std::ostream *stream() const { return _stream; };
65 
72  virtual bool recreateable() const { return false; }
73 
75  const std::string &name() const { return _name; }
76 
77  protected:
78  friend class OutputDirectory;
79 
81  OutputStream(const std::string &name,
82  std::ostream *stream);
83 
84  /* Prevent copying */
85  OutputStream(const OutputStream &f);
86 
88  virtual void relocate(const OutputDirectory &dir);
89 
91  const std::string _name;
92 
94  std::ostream *const _stream;
95 };
96 
97 template<class StreamType>
99  : public OutputStream
100 {
101  public:
102  typedef StreamType stream_type_t;
103 
104  virtual ~OutputFile();
105 
112  bool recreateable() const override { return _recreateable; }
113 
114  protected:
115  friend class OutputDirectory;
116 
118  const std::string &name,
119  std::ios_base::openmode mode,
120  bool recreateable);
121 
122  /* Prevent copying */
124 
126  void relocate(const OutputDirectory &dir) override;
127 
129  const std::ios_base::openmode _mode;
130 
132  const bool _recreateable;
133 
136 };
137 
140 {
141  private:
143  typedef std::map<std::string, OutputStream *> file_map_t;
144 
146  typedef std::map<std::string, OutputDirectory *> dir_map_t;
147 
150 
153 
155  std::string dir;
156 
158  static const char PATH_SEPARATOR = '/';
159 
162 
163  protected:
172  static OutputStream *checkForStdio(const std::string &name);
173 
174  public:
176  OutputDirectory();
177 
179  OutputDirectory(const std::string &name);
180 
183 
192  std::string resolve(const std::string &name) const;
193 
198  void setDirectory(const std::string &dir);
199 
204  const std::string &directory() const;
205 
224  OutputStream *create(const std::string &name,
225  bool binary = false,
226  bool no_gz = false);
227 
242  OutputStream *open(const std::string &name,
243  std::ios_base::openmode mode,
244  bool recreateable = true,
245  bool no_gz = false);
246 
256  void close(OutputStream *file);
257 
264  OutputStream *find(const std::string &name) const;
265 
266  OutputStream *findOrCreate(const std::string &name, bool binary = false);
267 
274  bool isFile(const std::string &name) const;
275 
279  static inline bool isAbsolute(const std::string &name) {
280  return name[0] == PATH_SEPARATOR;
281  }
282 
288  OutputDirectory *createSubdirectory(const std::string &name);
289 
304  void remove(const std::string &name, bool recursive=false);
305 };
306 
307 extern OutputDirectory simout;
308 
309 #endif // __BASE_OUTPUT_HH__
bool recreateable() const override
Can the file be recreated if the output directory is moved?
Definition: output.hh:112
virtual bool recreateable() const
Can the file be recreated if the output directory is moved?
Definition: output.hh:72
std::ostream * stream() const
Get the output underlying output stream.
Definition: output.hh:64
OutputDirectory * createSubdirectory(const std::string &name)
Creates a subdirectory within this directory.
Definition: output.cc:279
virtual void relocate(const OutputDirectory &dir)
Re-create the in a new location if recreateable.
Definition: output.cc:78
const std::string & name()
Definition: trace.cc:49
std::map< std::string, OutputDirectory * > dir_map_t
Output subdirectories.
Definition: output.hh:146
OutputStream * create(const std::string &name, bool binary=false, bool no_gz=false)
Creates a file in this directory (optionally compressed).
Definition: output.cc:206
OutputDirectory()
Constructor.
Definition: output.cc:119
virtual ~OutputStream()
Definition: output.cc:73
std::ostream *const _stream
Underlying output stream.
Definition: output.hh:94
OutputDirectory simout
Definition: output.cc:65
void setDirectory(const std::string &dir)
Sets name of this directory.
Definition: output.cc:160
StreamType stream_type_t
Definition: output.hh:102
Bitfield< 4, 0 > mode
Definition: miscregs.hh:1385
void relocate(const OutputDirectory &dir) override
Re-create the file in a new location if it is relocatable.
Definition: output.cc:105
const bool _recreateable
Can the file be recreated in a new location?
Definition: output.hh:132
bool isFile(const std::string &name) const
Determines whether a file name corresponds to a file in this directory.
Definition: output.cc:268
Bitfield< 6 > f
Definition: miscregs.hh:1379
file_map_t files
Open file streams within this directory.
Definition: output.hh:149
const std::string _name
Name in output directory.
Definition: output.hh:91
const std::string & name() const
Get the file name in the output directory.
Definition: output.hh:75
const std::ios_base::openmode _mode
File mode when opened.
Definition: output.hh:129
~OutputDirectory()
Destructor.
Definition: output.cc:127
void close(OutputStream *file)
Closes an output file and free the corresponding OutputFile.
Definition: output.cc:148
OutputStream * find(const std::string &name) const
Finds stream associated with an open file or stdout/stderr.
Definition: output.cc:243
std::string resolve(const std::string &name) const
Returns relative file names prepended with name of this directory.
Definition: output.cc:200
OutputFile(const OutputDirectory &dir, const std::string &name, std::ios_base::openmode mode, bool recreateable)
Definition: output.cc:83
const std::string & directory() const
Gets name of this directory.
Definition: output.cc:191
OutputStream * findOrCreate(const std::string &name, bool binary=false)
Definition: output.cc:258
static OutputStream stdout
Definition: output.hh:160
static OutputStream stderr
Definition: output.hh:161
OutputStream(const std::string &name, std::ostream *stream)
Wrap an existing stream.
Definition: output.cc:68
static OutputStream * checkForStdio(const std::string &name)
Determines whether given file name corresponds to standard output streams.
Definition: output.cc:136
static bool isAbsolute(const std::string &name)
Test if a path is absolute.
Definition: output.hh:279
std::string dir
Name of this directory.
Definition: output.hh:155
static const char PATH_SEPARATOR
System-specific path separator character.
Definition: output.hh:158
dir_map_t dirs
Output sub-directories.
Definition: output.hh:152
std::map< std::string, OutputStream * > file_map_t
File names and associated stream handles.
Definition: output.hh:143
Interface for creating files in a gem5 output directory.
Definition: output.hh:139
stream_type_t *const _fstream
Pointer to the file stream.
Definition: output.hh:135
virtual ~OutputFile()
Definition: output.cc:97
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:220

Generated on Fri Jun 9 2017 13:03:41 for gem5 by doxygen 1.8.6