TPIE

2362a60
tempname.h
Go to the documentation of this file.
1 // -*- mode: c++; tab-width: 4; indent-tabs-mode: t; c-file-style: "stroustrup"; -*-
2 // vi:set ts=4 sts=4 sw=4 noet :
3 // Copyright 2008, 2012, The TPIE development team
4 //
5 // This file is part of TPIE.
6 //
7 // TPIE is free software: you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License as published by the
9 // Free Software Foundation, either version 3 of the License, or (at your
10 // option) any later version.
11 //
12 // TPIE is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 // License for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with TPIE. If not, see <http://www.gnu.org/licenses/>
19 
23 
24 #ifndef _TPIE_TEMPNAM_H
25 #define _TPIE_TEMPNAM_H
26 
27 // Get definitions for working with Unix and Windows
28 #include <tpie/portability.h>
29 #include <tpie/stats.h>
30 #include <stdexcept>
31 #include <boost/intrusive_ptr.hpp>
32 #include <string>
33  // The name of the environment variable pointing to a tmp directory.
34 #define TMPDIR_ENV "TMPDIR"
35 
36 // The name of the environment variable to consult for default device
37 // descriptions.
38 #define AMI_SINGLE_DEVICE_ENV "AMI_SINGLE_DEVICE"
39 
40 namespace tpie {
41 
42  struct tempfile_error: public std::runtime_error {
43  explicit tempfile_error(const std::string & what): std::runtime_error(what) {}
44  };
45 
50  class tempname {
51  public:
69  static std::string tpie_name(const std::string& post_base = "",
70  const std::string& dir = "",
71  const std::string& ext = "");
72 
79  static std::string tpie_dir_name(const std::string& post_base = "",
80  const std::string& dir = "");
81 
85  static std::string get_system_path();
86 
93  static bool try_directory(const std::string& path, const std::string& subdir="");
94 
101  static void set_default_path(const std::string& path, const std::string& subdir="");
102 
107  static void set_default_base_name(const std::string& name);
108 
113  static void set_default_extension(const std::string& ext);
114 
120  static const std::string& get_default_path();
121 
127  static const std::string& get_default_base_name();
128 
134  static const std::string& get_default_extension();
135 
136 
153  static std::string get_actual_path();
154  };
155 
156  void finish_tempfile();
157 
158  namespace bits {
159 
161  public:
162  temp_file_inner();
163  temp_file_inner(const temp_file_inner & o) = delete;
164  temp_file_inner(temp_file_inner && o) = delete;
165  temp_file_inner & operator=(const temp_file_inner & o) = delete;
166  temp_file_inner & operator=(temp_file_inner && o) = delete;
167 
168  temp_file_inner(const std::string & path, bool persist);
169  ~temp_file_inner();
170 
171  const std::string & path();
172  void update_recorded_size(stream_size_type size);
173 
174  bool is_persistent() const {
175  return m_persist;
176  }
177 
178  void set_persistent(bool p) {
179  m_persist = p;
180  }
181 
182  friend void intrusive_ptr_add_ref(temp_file_inner * p);
183  friend void intrusive_ptr_release(temp_file_inner * p);
184 
185  private:
186  std::string m_path;
187  bool m_persist;
188  stream_size_type m_recordedSize;
189  memory_size_type m_count;
190  };
191 
192  void intrusive_ptr_add_ref(temp_file_inner * p);
193  void intrusive_ptr_release(temp_file_inner * p);
194 
195  } // namespace bits
196 
202  class temp_file {
203  public:
208  m_inner = new bits::temp_file_inner();
209  }
210 
214  temp_file(const std::string & path, bool persist=false) {
215  m_inner = new bits::temp_file_inner(path, persist);
216  }
217 
222  bool is_persistent() const {
223  return m_inner->is_persistent();
224  }
225 
230  void set_persistent(bool p) {
231  m_inner->set_persistent(p);
232  }
233 
237  void set_path(const std::string & path, bool persist=false) {
238  m_inner = new bits::temp_file_inner(path, persist);
239  }
240 
244  const std::string & path() {
245  return m_inner->path();
246  }
247 
251  void free() {
252  m_inner = new bits::temp_file_inner();
253  }
254 
255  void update_recorded_size(stream_size_type size) {
256  m_inner->update_recorded_size(size);
257  }
258  private:
259  boost::intrusive_ptr<bits::temp_file_inner> m_inner;
260  };
261 
262 }
263 
264 #endif // _TPIE_TEMPNAM_H
temp_file(const std::string &path, bool persist=false)
Create a temp_file associated with a specific file.
Definition: tempname.h:214
static std::string tpie_name(const std::string &post_base="", const std::string &dir="", const std::string &ext="")
Generate path for a new temporary file.
temp_file()
Create a temp_file associated with a temporary file.
Definition: tempname.h:207
void free()
Clears the current object reference.
Definition: tempname.h:251
This file contains a few deprecated definitions for legacy code.
void set_persistent(bool p)
Set persistence.
Definition: tempname.h:230
static std::string tpie_dir_name(const std::string &post_base="", const std::string &dir="")
Generate path for a new temporary directory.
I/O statistics.
static void set_default_base_name(const std::string &name)
Set default base name for temporary files.
void set_path(const std::string &path, bool persist=false)
Associate with a specific file.
Definition: tempname.h:237
const std::string & path()
Get the path of the associated file.
Definition: tempname.h:244
Class representing a reference to a temporary file.
Definition: tempname.h:202
Static methods for generating temporary file names and finding temporary file directories.
Definition: tempname.h:50
static const std::string & get_default_base_name()
Get default base name for temporary files if one is set using set_default_base_name.
static std::string get_system_path()
Get the default path for temporary files on the system.
static std::string get_actual_path()
Return The actual path used for temporary files taking environment variables into account...
bool is_persistent() const
Definition: tempname.h:222
static const std::string & get_default_path()
Get default path for directory containing temporary files if one is set using set_default_path.
static void set_default_extension(const std::string &ext)
Set default extension for temporary files.
static const std::string & get_default_extension()
Get default extension for temporary files if one is set using set_default_extension.
static void set_default_path(const std::string &path, const std::string &subdir="")
Sets the default temporary path.
static bool try_directory(const std::string &path, const std::string &subdir="")
Tests whether a temporary path is usable for tpie.