summaryrefslogtreecommitdiff
path: root/include/c/c_owner_set.h
blob: 1ae6f15c75932d41b3180cc3ba4f0c18edde2f79 (plain)
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
#ifndef C_OWNER_SET_H
#define C_OWNER_SET_H

// This file was ported from
// https://github.com/NSMBW-Community/NSMBW-Decomp/blob/master/include/dol/cLib/c_owner_set.hpp

#include "common.h"
/// @file

/// @brief A set node with a pointer to the owning container. See cOwnerSetMg_c.
/// @note Unofficial name.
class cOwnerSetNd_c {
public:
    void *getOwner() const {
        return mpOwner;
    }
    cOwnerSetNd_c *getNext() const {
        return mpNext;
    }

private:
    void *mpOwner;         ///< The set that contains this node.
    cOwnerSetNd_c *mpNext; ///< The next node in the set.

    friend class cOwnerSetMg_c;
};

/// @brief A set container. See cOwnerSetNd_c.
/// @details The set is implemented as a singly-linked list.
/// @note Unofficial name.
class cOwnerSetMg_c {
public:
    /// @brief Constructs a new set container.
    cOwnerSetMg_c() : mpRoot(nullptr) {}

    /// @brief Destroys the set.
    ~cOwnerSetMg_c() {
        clear();
    };

    /**
     * @brief Adds a node to the set.
     *
     * @param nd The node to add.
     * @param owner The owner of the node.
     */
    void add(cOwnerSetNd_c *nd, void *owner);

    /**
     * @brief Removes a node from the set.
     *
     * @param nd The node to remove.
     * @param owner The owner of the node.
     */
    void remove(cOwnerSetNd_c *nd, void *owner);

private:
    /// @brief Clears out the set and unlinks all child nodes.
    void clear();

    cOwnerSetNd_c *mpRoot; ///< The first element of the set.
};

#endif