summaryrefslogtreecommitdiffstats
path: root/src/Common/UserLists/UserList.h
blob: 4e782bf32bf2a40893ab76e5d318ada5fe76ec6c (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
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
/*
 * UserList.h
 *
 * Copyright (C) 2009 Matthias Schiffer <matthias@gamezock.de>
 *
 * This program 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.
 *
 * This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef MAD_COMMON_USERLISTS_USERLIST_H_
#define MAD_COMMON_USERLISTS_USERLIST_H_

#include "../export.h"

#include "UserListEntry.h"

#include <list>
#include <map>
#include <set>

namespace Mad {
namespace Common {
namespace UserLists {

class MAD_COMMON_EXPORT UserList {
  private:
    template <typename Type, typename IteratorType>
    class iterator_base {
      public:
        friend class UserList;

        typedef Type value_type;

        typedef value_type &reference;
        typedef value_type *pointer;

        typedef long difference_type;

      private:
        IteratorType it;

        iterator_base(IteratorType it0) : it(it0) {}

      public:
        iterator_base() {}

        reference operator*() const {
          return *it;
        }

        iterator_base operator++(int) {
          return iterator(it++);
        }

        iterator_base& operator++() {
          ++it;
          return *this;
        }

        iterator_base operator--(int) {
          return iterator(it--);
        }

        iterator_base& operator--() {
          --it;
          return *this;
        }

        bool operator==(const iterator_base &it2) {
          return it2.it == it;
        }

        bool operator!=(const iterator_base &it2) {
          return it2.it != it;
        }

        pointer operator->() const {
          return &*it;
        }
    };

    std::set<Core::String> names;
    std::list<UserListEntry> list;

    std::map<Core::String, unsigned> detailCounts;

    void registerEntry(const UserListEntry &entry);
    void unregisterEntry(const UserListEntry &entry);

    std::list<UserListEntry>::iterator findEntry(const Core::String &name);
    std::list<UserListEntry>::const_iterator findEntry(const Core::String &name) const;

  public:
    typedef iterator_base<const UserListEntry, std::list<UserListEntry>::iterator> iterator;
    typedef iterator_base<const UserListEntry, std::list<UserListEntry>::const_iterator> const_iterator;

    iterator find(const Core::String &name);
    const_iterator find(const Core::String &name) const;

    iterator begin() {
      return iterator(list.begin());
    }

    const_iterator begin() const {
      return const_iterator(list.begin());
    }

    iterator end() {
      return list.end();
    }

    const_iterator end() const {
      return const_iterator(list.end());
    }

    bool isEmpty() const {
      return list.empty();
    }

    size_t getLength() const {
      return list.size();
    }

    std::set<Core::String> getDetails() const {
      std::set<Core::String> ret;

      for(std::map<Core::String, unsigned>::const_iterator it = detailCounts.begin(); it != detailCounts.end(); ++it)
        ret.insert(it->first);

      return ret;
    }

    unsigned getDetailUsage(const Core::String &detail) const {
      std::map<Core::String, unsigned>::const_iterator it = detailCounts.find(detail);
      if(it == detailCounts.end())
        return 0;
      else
        return it->second;
    }

    bool addUser(const UserListEntry &entry);
    bool insertUser(const UserListEntry &entry, iterator after);
    bool removeUser(const Core::String &name);
    bool updateUser(const UserListEntry &entry);

    UserList() {}
};

}
}
}

#endif /* MAD_COMMON_USERLISTS_USERLIST_H_ */