Panda3D
virtualFileMountRamdisk.h
1 // Filename: virtualFileMountRamdisk.h
2 // Created by: drose (19Sep11)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #ifndef VIRTUALFILEMOUNTRAMDISK_H
16 #define VIRTUALFILEMOUNTRAMDISK_H
17 
18 #include "pandabase.h"
19 
20 #include "virtualFileMount.h"
21 #include "mutexImpl.h"
22 #include "streamWrapper.h"
23 
24 ////////////////////////////////////////////////////////////////////
25 // Class : VirtualFileMountRamdisk
26 // Description : Simulates an actual directory on disk with in-memory
27 // storage. This is useful mainly for performing high
28 // level functions that expect disk I/O without actually
29 // writing files to disk. Naturally, there are
30 // significant limits to the size of the files that may
31 // be written with this system; and "files" written here
32 // are not automatically persistent between sessions.
33 ////////////////////////////////////////////////////////////////////
34 class EXPCL_PANDAEXPRESS VirtualFileMountRamdisk : public VirtualFileMount {
35 PUBLISHED:
37 
38 public:
39  virtual bool has_file(const Filename &file) const;
40  virtual bool create_file(const Filename &file);
41  virtual bool make_directory(const Filename &file);
42  virtual bool delete_file(const Filename &file);
43  virtual bool rename_file(const Filename &orig_filename, const Filename &new_filename);
44  virtual bool copy_file(const Filename &orig_filename, const Filename &new_filename);
45  virtual bool is_directory(const Filename &file) const;
46  virtual bool is_regular_file(const Filename &file) const;
47  virtual bool is_writable(const Filename &file) const;
48 
49  virtual istream *open_read_file(const Filename &file) const;
50  virtual ostream *open_write_file(const Filename &file, bool truncate);
51  virtual ostream *open_append_file(const Filename &file);
52  virtual iostream *open_read_write_file(const Filename &file, bool truncate);
53  virtual iostream *open_read_append_file(const Filename &file);
54 
55  virtual streamsize get_file_size(const Filename &file, istream *stream) const;
56  virtual streamsize get_file_size(const Filename &file) const;
57  virtual time_t get_timestamp(const Filename &file) const;
58 
59  virtual bool scan_directory(vector_string &contents,
60  const Filename &dir) const;
61 
62  virtual bool atomic_compare_and_exchange_contents(const Filename &file, string &orig_contents, const string &old_contents, const string &new_contents);
63  virtual bool atomic_read_contents(const Filename &file, string &contents) const;
64 
65  virtual void output(ostream &out) const;
66 
67 private:
68  class FileBase;
69  class File;
70  class Directory;
71 
72  class FileBase : public TypedReferenceCount {
73  public:
74  INLINE FileBase(const string &basename);
75  virtual ~FileBase();
76  INLINE bool operator < (const FileBase &other) const;
77 
78  virtual bool is_directory() const;
79 
80  string _basename;
81  time_t _timestamp;
82 
83  public:
84  virtual TypeHandle get_type() const {
85  return get_class_type();
86  }
87  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
88  static TypeHandle get_class_type() {
89  return _type_handle;
90  }
91  static void init_type() {
92  TypedReferenceCount::init_type();
93  register_type(_type_handle, "VirtualFileMountRamdisk::FileBase",
94  TypedReferenceCount::get_class_type());
95  }
96 
97  private:
98  static TypeHandle _type_handle;
99  };
100 
101  class File : public FileBase {
102  public:
103  INLINE File(const string &basename);
104 
105  stringstream _data;
106  StreamWrapper _wrapper;
107 
108  public:
109  virtual TypeHandle get_type() const {
110  return get_class_type();
111  }
112  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
113  static TypeHandle get_class_type() {
114  return _type_handle;
115  }
116  static void init_type() {
117  FileBase::init_type();
118  register_type(_type_handle, "VirtualFileMountRamdisk::File",
119  FileBase::get_class_type());
120  }
121 
122  private:
123  static TypeHandle _type_handle;
124  };
125 
127 
128  class Directory : public FileBase {
129  public:
130  INLINE Directory(const string &basename);
131 
132  virtual bool is_directory() const;
133 
134  PT(FileBase) do_find_file(const string &filename) const;
135  PT(File) do_create_file(const string &filename);
136  PT(Directory) do_make_directory(const string &filename);
137  PT(FileBase) do_delete_file(const string &filename);
138  bool do_scan_directory(vector_string &contents) const;
139 
140  Files _files;
141 
142  public:
143  virtual TypeHandle get_type() const {
144  return get_class_type();
145  }
146  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
147  static TypeHandle get_class_type() {
148  return _type_handle;
149  }
150  static void init_type() {
151  FileBase::init_type();
152  register_type(_type_handle, "VirtualFileMountRamdisk::Directory",
153  FileBase::get_class_type());
154  }
155 
156  private:
157  static TypeHandle _type_handle;
158  };
159 
160  Directory _root;
161  mutable MutexImpl _lock;
162 
163 public:
164  virtual TypeHandle get_type() const {
165  return get_class_type();
166  }
167  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
168  static TypeHandle get_class_type() {
169  return _type_handle;
170  }
171  static void init_type() {
172  VirtualFileMount::init_type();
173  register_type(_type_handle, "VirtualFileMountRamdisk",
174  VirtualFileMount::get_class_type());
175  FileBase::init_type();
176  File::init_type();
177  Directory::init_type();
178  }
179 
180 private:
181  static TypeHandle _type_handle;
182 };
183 
184 #include "virtualFileMountRamdisk.I"
185 
186 #endif
virtual bool delete_file(const Filename &file)
Attempts to delete the indicated file or directory within the mount.
virtual bool atomic_read_contents(const Filename &file, string &contents) const
See Filename::atomic_read_contents().
A base class for things which need to inherit from both TypedObject and from ReferenceCount.
This class provides a locking wrapper around a combination ostream/istream pointer.
virtual bool copy_file(const Filename &orig_filename, const Filename &new_filename)
Attempts to copy the contents of the indicated file to the indicated file.
Simulates an actual directory on disk with in-memory storage.
virtual bool rename_file(const Filename &orig_filename, const Filename &new_filename)
Attempts to rename the contents of the indicated file to the indicated file.
virtual bool is_writable(const Filename &file) const
Returns true if the named file or directory may be written to, false otherwise.
virtual bool create_file(const Filename &file)
Attempts to create the indicated file within the mount, if it does not already exist.
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:44
virtual ostream * open_append_file(const Filename &file)
Works like open_write_file(), but the file is opened in append mode.
virtual iostream * open_read_append_file(const Filename &file)
Works like open_read_write_file(), but the file is opened in append mode.
virtual bool make_directory(const Filename &file)
Attempts to create the indicated file within the mount, if it does not already exist.
The abstract base class for a mount definition used within a VirtualFileSystem.
A fake mutex implementation for single-threaded applications that don&#39;t need any synchronization cont...
This is our own Panda specialization on the default STL set.
Definition: pset.h:52
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
virtual bool atomic_compare_and_exchange_contents(const Filename &file, string &orig_contents, const string &old_contents, const string &new_contents)
See Filename::atomic_compare_and_exchange_contents().
virtual ostream * open_write_file(const Filename &file, bool truncate)
Opens the file for writing.
virtual iostream * open_read_write_file(const Filename &file, bool truncate)
Opens the file for writing.