Directory: add constructor and destructor

This commit is contained in:
Max Kellermann 2013-01-03 01:36:28 +01:00
parent 3e8047e583
commit f5a92d6cc3
4 changed files with 45 additions and 18 deletions

View File

@ -36,8 +36,8 @@ extern "C" {
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
static Directory * inline Directory *
directory_allocate(const char *path) Directory::Allocate(const char *path)
{ {
assert(path != NULL); assert(path != NULL);
@ -46,30 +46,21 @@ directory_allocate(const char *path)
(Directory *)g_malloc0(sizeof(*directory) (Directory *)g_malloc0(sizeof(*directory)
- sizeof(directory->path) - sizeof(directory->path)
+ path_size); + path_size);
INIT_LIST_HEAD(&directory->children); new(directory) Directory(path);
INIT_LIST_HEAD(&directory->songs);
INIT_LIST_HEAD(&directory->playlists);
memcpy(directory->path, path, path_size);
return directory; return directory;
} }
Directory * Directory::Directory(const char *_path)
Directory::NewGeneric(const char *path, Directory *parent)
{ {
assert(path != NULL); INIT_LIST_HEAD(&children);
assert((*path == 0) == (parent == NULL)); INIT_LIST_HEAD(&songs);
INIT_LIST_HEAD(&playlists);
Directory *directory = directory_allocate(path); strcpy(path, _path);
directory->parent = parent;
return directory;
} }
void Directory::~Directory()
Directory::Free()
{ {
playlist_vector_deinit(&playlists); playlist_vector_deinit(&playlists);
@ -80,7 +71,25 @@ Directory::Free()
Directory *child, *n; Directory *child, *n;
directory_for_each_child_safe(child, n, this) directory_for_each_child_safe(child, n, this)
child->Free(); child->Free();
}
Directory *
Directory::NewGeneric(const char *path, Directory *parent)
{
assert(path != NULL);
assert((*path == 0) == (parent == NULL));
Directory *directory = Allocate(path);
directory->parent = parent;
return directory;
}
void
Directory::Free()
{
this->Directory::~Directory();
g_free(this); g_free(this);
} }

View File

@ -90,6 +90,19 @@ struct Directory {
bool have_stat; /* not needed if ino_t == dev_t == 0 is impossible */ bool have_stat; /* not needed if ino_t == dev_t == 0 is impossible */
char path[sizeof(long)]; char path[sizeof(long)];
protected:
Directory(const char *path);
gcc_malloc gcc_nonnull_all
static Directory *Allocate(const char *path);
public:
/**
* Default constructor, needed for #detached_root.
*/
Directory() = default;
~Directory();
/** /**
* Generic constructor for #Directory object. * Generic constructor for #Directory object.
*/ */

View File

@ -20,6 +20,7 @@
#include "config.h" #include "config.h"
#include "TagSave.hxx" #include "TagSave.hxx"
#include "song.h" #include "song.h"
#include "Directory.hxx"
extern "C" { extern "C" {
#include "io_thread.h" #include "io_thread.h"
@ -37,6 +38,8 @@ extern "C" {
#include <unistd.h> #include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
Directory::~Directory() {}
static void static void
my_log_func(const gchar *log_domain, G_GNUC_UNUSED GLogLevelFlags log_level, my_log_func(const gchar *log_domain, G_GNUC_UNUSED GLogLevelFlags log_level,
const gchar *message, G_GNUC_UNUSED gpointer user_data) const gchar *message, G_GNUC_UNUSED gpointer user_data)

View File

@ -7,6 +7,8 @@ extern "C" {
Directory detached_root; Directory detached_root;
Directory::~Directory() {}
struct song * struct song *
song_dup_detached(const struct song *src) song_dup_detached(const struct song *src)
{ {