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