summaryrefslogtreecommitdiff
path: root/louloulibs/xmpp/roster.hpp
blob: 0aebca5b9d61180600802926092f9d85d30682c9 (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
#ifndef ROSTER_HPP_INCLUDED
#define ROSTER_HPP_INCLUDED

#include <algorithm>
#include <string>
#include <vector>

class RosterItem
{
public:
  RosterItem(const std::string& jid, const std::string& name,
             std::vector<std::string>& groups);
  RosterItem(const std::string& jid, const std::string& name);
  RosterItem() = default;
  ~RosterItem() = default;
  RosterItem(const RosterItem&) = default;
  RosterItem(RosterItem&&) = default;
  RosterItem& operator=(const RosterItem&) = default;
  RosterItem& operator=(RosterItem&&) = default;

  std::string jid;
  std::string name;
  std::vector<std::string> groups;

private:
};

/**
 * Keep track of the last known stat of a JID's roster
 */
class Roster
{
public:
  Roster() = default;
  ~Roster() = default;

  void clear();

  template <typename... ArgsType>
  RosterItem* add_item(ArgsType&&... args)
  {
    this->items.emplace_back(std::forward<ArgsType>(args)...);
    auto it = this->items.end() - 1;
    return &*it;
  }
  RosterItem* get_item(const std::string& jid)
  {
    auto it = std::find_if(this->items.begin(), this->items.end(),
                           [this, &jid](const auto& item)
                           {
                             return item.jid == jid;
                           });
    if (it != this->items.end())
      return &*it;
    return nullptr;
  }
  const std::vector<RosterItem>& get_items() const
  {
    return this->items;
  }

private:
  std::vector<RosterItem> items;

  Roster(const Roster&) = delete;
  Roster(Roster&&) = delete;
  Roster& operator=(const Roster&) = delete;
  Roster& operator=(Roster&&) = delete;
};

#endif /* ROSTER_HPP_INCLUDED */