TPIE

11a2c2d
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/tpie_export.h>
29 #include <tpie/types.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 TPIE_EXPORT 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 
92  static bool try_directory(const std::string& path);
93 
100  static void set_default_path(const std::string& path, const std::string& subdir="");
101 
106  static void set_default_base_name(const std::string& name);
107 
112  static void set_default_extension(const std::string& ext);
113 
119  static const std::string& get_default_path();
120 
126  static const std::string& get_default_base_name();
127 
133  static const std::string& get_default_extension();
134 
135 
152  static std::string get_actual_path();
153  };
154 
155  void finish_tempfile();
156 
157  namespace bits {
158 
159  class TPIE_EXPORT temp_file_inner {
160  public:
161  temp_file_inner();
162  temp_file_inner(const temp_file_inner & o) = delete;
163  temp_file_inner(temp_file_inner && o) = delete;
164  temp_file_inner & operator=(const temp_file_inner & o) = delete;
165  temp_file_inner & operator=(temp_file_inner && o) = delete;
166 
167  temp_file_inner(const std::string & path, bool persist);
168  ~temp_file_inner();
169 
170  const std::string & path();
171  void update_recorded_size(stream_size_type size);
172 
173  bool is_persistent() const {
174  return m_persist;
175  }
176 
177  void set_persistent(bool p) {
178  m_persist = p;
179  }
180 
181  TPIE_EXPORT friend void intrusive_ptr_add_ref(temp_file_inner *p);
182  TPIE_EXPORT friend void intrusive_ptr_release(temp_file_inner *p);
183 
184  private:
185  std::string m_path;
186  bool m_persist;
187  stream_size_type m_recordedSize;
188  memory_size_type m_count;
189  };
190 
191  TPIE_EXPORT void intrusive_ptr_add_ref(temp_file_inner * p);
192  TPIE_EXPORT void intrusive_ptr_release(temp_file_inner * p);
193 
194  } // namespace bits
195 
201  class temp_file {
202  public:
207  m_inner = new bits::temp_file_inner();
208  }
209 
213  temp_file(const std::string & path, bool persist=false) {
214  m_inner = new bits::temp_file_inner(path, persist);
215  }
216 
221  bool is_persistent() const {
222  return m_inner->is_persistent();
223  }
224 
229  void set_persistent(bool p) {
230  m_inner->set_persistent(p);
231  }
232 
236  void set_path(const std::string & path, bool persist=false) {
237  m_inner = new bits::temp_file_inner(path, persist);
238  }
239 
243  const std::string & path() {
244  return m_inner->path();
245  }
246 
250  void free() {
251  m_inner = new bits::temp_file_inner();
252  }
253 
254  void update_recorded_size(stream_size_type size) {
255  m_inner->update_recorded_size(size);
256  }
257  private:
258  boost::intrusive_ptr<bits::temp_file_inner> m_inner;
259  };
260 
261 }
262 
263 #endif // _TPIE_TEMPNAM_H
tpie::tempfile_error
Definition: tempname.h:42
tpie::temp_file::temp_file
temp_file(const std::string &path, bool persist=false)
Create a temp_file associated with a specific file.
Definition: tempname.h:213
types.h
tpie::bits::temp_file_inner
Definition: tempname.h:159
tpie::temp_file::path
const std::string & path()
Get the path of the associated file.
Definition: tempname.h:243
tpie::temp_file::free
void free()
Clears the current object reference.
Definition: tempname.h:250
tpie::temp_file::set_path
void set_path(const std::string &path, bool persist=false)
Associate with a specific file.
Definition: tempname.h:236
tpie::temp_file::set_persistent
void set_persistent(bool p)
Set persistence.
Definition: tempname.h:229
tpie::tempname
Static methods for generating temporary file names and finding temporary file directories.
Definition: tempname.h:50
tpie::temp_file::is_persistent
bool is_persistent() const
Definition: tempname.h:221
tpie::temp_file
Class representing a reference to a temporary file.
Definition: tempname.h:201
tpie
Definition: access_type.h:26
tpie::temp_file::temp_file
temp_file()
Create a temp_file associated with a temporary file.
Definition: tempname.h:206