You've already forked godot
mirror of
https://github.com/godotengine/godot.git
synced 2025-11-04 12:00:25 +00:00
Make FileAccess and DirAccess classes reference counted.
This commit is contained in:
@@ -31,11 +31,12 @@
|
||||
#ifndef DIR_ACCESS_H
|
||||
#define DIR_ACCESS_H
|
||||
|
||||
#include "core/object/ref_counted.h"
|
||||
#include "core/string/ustring.h"
|
||||
#include "core/typedefs.h"
|
||||
|
||||
//@ TODO, excellent candidate for THREAD_SAFE MACRO, should go through all these and add THREAD_SAFE where it applies
|
||||
class DirAccess {
|
||||
class DirAccess : public RefCounted {
|
||||
public:
|
||||
enum AccessType {
|
||||
ACCESS_RESOURCES,
|
||||
@@ -44,13 +45,13 @@ public:
|
||||
ACCESS_MAX
|
||||
};
|
||||
|
||||
typedef DirAccess *(*CreateFunc)();
|
||||
typedef Ref<DirAccess> (*CreateFunc)();
|
||||
|
||||
private:
|
||||
AccessType _access_type = ACCESS_FILESYSTEM;
|
||||
static CreateFunc create_func[ACCESS_MAX]; ///< set this to instance a filesystem object
|
||||
|
||||
Error _copy_dir(DirAccess *p_target_da, String p_to, int p_chmod_flags, bool p_copy_links);
|
||||
Error _copy_dir(Ref<DirAccess> &p_target_da, String p_to, int p_chmod_flags, bool p_copy_links);
|
||||
|
||||
protected:
|
||||
String _get_root_path() const;
|
||||
@@ -59,7 +60,7 @@ protected:
|
||||
String fix_path(String p_path) const;
|
||||
|
||||
template <class T>
|
||||
static DirAccess *_create_builtin() {
|
||||
static Ref<DirAccess> _create_builtin() {
|
||||
return memnew(T);
|
||||
}
|
||||
|
||||
@@ -77,7 +78,7 @@ public:
|
||||
virtual bool drives_are_shortcuts();
|
||||
|
||||
virtual Error change_dir(String p_dir) = 0; ///< can be relative or absolute, return false on success
|
||||
virtual String get_current_dir(bool p_include_drive = true) = 0; ///< return current dir location
|
||||
virtual String get_current_dir(bool p_include_drive = true) const = 0; ///< return current dir location
|
||||
virtual Error make_dir(String p_dir) = 0;
|
||||
virtual Error make_dir_recursive(String p_dir);
|
||||
virtual Error erase_contents_recursive(); //super dangerous, use with care!
|
||||
@@ -101,51 +102,29 @@ public:
|
||||
// Meant for editor code when we want to quickly remove a file without custom
|
||||
// handling (e.g. removing a cache file).
|
||||
static void remove_file_or_error(String p_path) {
|
||||
DirAccess *da = create(ACCESS_FILESYSTEM);
|
||||
Ref<DirAccess> da = create(ACCESS_FILESYSTEM);
|
||||
if (da->file_exists(p_path)) {
|
||||
if (da->remove(p_path) != OK) {
|
||||
ERR_FAIL_MSG("Cannot remove file or directory: " + p_path);
|
||||
}
|
||||
}
|
||||
memdelete(da);
|
||||
}
|
||||
|
||||
virtual String get_filesystem_type() const = 0;
|
||||
static String get_full_path(const String &p_path, AccessType p_access);
|
||||
static DirAccess *create_for_path(const String &p_path);
|
||||
static Ref<DirAccess> create_for_path(const String &p_path);
|
||||
|
||||
static DirAccess *create(AccessType p_access);
|
||||
static Ref<DirAccess> create(AccessType p_access);
|
||||
|
||||
template <class T>
|
||||
static void make_default(AccessType p_access) {
|
||||
create_func[p_access] = _create_builtin<T>;
|
||||
}
|
||||
|
||||
static DirAccess *open(const String &p_path, Error *r_error = nullptr);
|
||||
static Ref<DirAccess> open(const String &p_path, Error *r_error = nullptr);
|
||||
|
||||
DirAccess() {}
|
||||
virtual ~DirAccess() {}
|
||||
};
|
||||
|
||||
struct DirAccessRef {
|
||||
_FORCE_INLINE_ DirAccess *operator->() {
|
||||
return f;
|
||||
}
|
||||
|
||||
operator bool() const { return f != nullptr; }
|
||||
|
||||
DirAccess *f = nullptr;
|
||||
|
||||
DirAccessRef(DirAccess *fa) { f = fa; }
|
||||
DirAccessRef(DirAccessRef &&other) {
|
||||
f = other.f;
|
||||
other.f = nullptr;
|
||||
}
|
||||
~DirAccessRef() {
|
||||
if (f) {
|
||||
memdelete(f);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#endif // DIR_ACCESS_H
|
||||
|
||||
Reference in New Issue
Block a user