File: crate.hpp

package info (click to toggle)
libdjinterop 0.24.3%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,880 kB
  • sloc: cpp: 27,419; sql: 4,053; makefile: 3; sh: 2
file content (164 lines) | stat: -rw-r--r-- 5,147 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
    This file is part of libdjinterop.

    libdjinterop is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    libdjinterop is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with libdjinterop.  If not, see <http://www.gnu.org/licenses/>.
 */

#pragma once
#ifndef DJINTEROP_CRATE_HPP
#define DJINTEROP_CRATE_HPP

#include <cstdint>
#include <memory>
#include <optional>
#include <string>
#include <vector>

#include <djinterop/config.hpp>

namespace djinterop
{
class database;
class crate_impl;
class track;

/// A `crate` object is a handle to a crate stored in a database. As long as it
/// lives, the corresponding database connection is kept open.
///
/// `crate` objects can be copied and assigned cheaply, resulting in multiple
/// handles to the same actual crate.
///
/// The read/write operations provided by this class directly access the
/// database.
///
/// A `crate` object becomes invalid if the crate gets deleted by
/// `database::remove_crate()`. After that, you must not call any methods on the
/// `crate` object, except for destructing it, or assigning to it.
class DJINTEROP_PUBLIC crate
{
public:
    /// Copy constructor
    crate(const crate& other) noexcept;

    /// Destructor
    ~crate();

    /// Copy assignment operator
    crate& operator=(const crate& other) noexcept;

    /// Adds a track to the crate.
    // TODO (mr-smidge): Remove method, don't expose format-specific ids.
    void add_track(int64_t track_id) const;

    /// Adds a track to the crate.
    ///
    /// A track can be contained in arbitrarily many (including zero) crates.
    void add_track(track tr) const;

    /// Add a range of tracks to the crate.
    template <typename InputIterator>
    void add_tracks(InputIterator first, InputIterator last)
    {
        for (auto iter = first; iter != last; ++iter)
            add_track(*iter);
    }

    /// Returns the (direct) children of this crate
    std::vector<crate> children() const;

    /// Removes all tracks from the crate
    ///
    /// Note that the tracks stay in the database even if they're contained in
    /// zero crates.
    void clear_tracks() const;

    /// Create a new crate as a child of this one.
    crate create_sub_crate(const std::string& name);

    /// Create a new crate as a child of this one, after the given crate in
    /// order.
    crate create_sub_crate_after(const std::string& name, const crate& after);

    /// Returns the database containing the crate
    database db() const;

    /// Returns the descendants of this crate
    ///
    /// A descendant is a direct or indirect child of this crate.
    // TODO (mr-smidge): Remove method, unnecessary complication.
    std::vector<crate> descendants() const;

    /// Returns the ID of this crate
    ///
    /// The ID is used internally in the database and is unique for crates
    /// contained in the same database.
    int64_t id() const;

    /// Returns `true` iff `*this` is valid as described in the class comment
    // TODO (mr-smidge): Remove method, not necessary.
    bool is_valid() const;

    /// Returns the crate's name
    std::string name() const;

    /// Returns the parent crate, if this crate has one
    ///
    /// If the crate doesn't have a parent, then `djinterop::nullopt` is
    /// returned.
    std::optional<crate> parent() const;

    /// Removes a track from the crate
    ///
    /// Note that the track stays in the database even if it's contained in zero
    /// crates.
    void remove_track(track tr) const;

    /// Sets the crate's name
    void set_name(std::string name) const;

    /// Sets this crate's parent
    ///
    /// If `djinterop::nullopt` is given, then this crate will have no parent.
    /// That is, it becomes a root crate.
    void set_parent(std::optional<crate> parent) const;

    /// Gets the sub-crate of this one with a given name.
    ///
    /// Note that descendants of sub-crates are not found by this method, i.e.
    /// the search does not recurse into the immediate sub-crates of this crate.
    ///
    /// If no such crate is found, then `djinterop::nullopt` is returned.
    std::optional<crate> sub_crate_by_name(const std::string& name) const;

    /// Returns the crate's contained tracks.
    ///
    /// Note that there is no guarantee on the ordering of tracks within the
    /// crate.
    ///
    /// \return Returns a vector of tracks.
    std::vector<track> tracks() const;

    // TODO (haslersn): non public?
    crate(std::shared_ptr<crate_impl> pimpl);

private:
    std::shared_ptr<crate_impl> pimpl_;

    friend class database;
    friend class track;
};

}  // namespace djinterop

#endif  // DJINTEROP_CRATE_HPP