GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/chatobject.cpp Lines: 3 21 14.3 %
Date: 2017-11-29 Branches: 2 16 12.5 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2012-2017  The ManaPlus Developers
4
 *
5
 *  This file is part of The ManaPlus Client.
6
 *
7
 *  This program is free software; you can redistribute it and/or modify
8
 *  it under the terms of the GNU General Public License as published by
9
 *  the Free Software Foundation; either version 2 of the License, or
10
 *  any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU General Public License
18
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
#include "resources/chatobject.h"
22
23
#include "debug.h"
24
25
2
std::map<std::string, ChatObject*> ChatObject::chatNameMap;
26
2
std::map<int, ChatObject*> ChatObject::chatIdMap;
27
28
ChatObject::ChatObject() :
29
    ownerId(BeingId_zero),
30
    chatId(0),
31
    maxUsers(0),
32
    currentUsers(0),
33
    type(0),
34
    title()
35
{
36
}
37
38
ChatObject::~ChatObject()
39
{
40
    std::map<std::string, ChatObject*>::iterator it = chatNameMap.find(title);
41
    if (it != chatNameMap.end() && (*it).second == this)
42
        chatNameMap.erase(it);
43
    std::map<int, ChatObject*>::iterator it2 = chatIdMap.find(chatId);
44
    if (it2 != chatIdMap.end() && (*it2).second == this)
45
        chatIdMap.erase(it2);
46
}
47
48
void ChatObject::update()
49
{
50
    chatNameMap[title] = this;
51
    chatIdMap[chatId] = this;
52
}
53
54
ChatObject *ChatObject::findByName(const std::string &name)
55
{
56
    const std::map<std::string, ChatObject*>::iterator it =
57
        chatNameMap.find(name);
58
    if (it == chatNameMap.end())
59
        return nullptr;
60
    return (*it).second;
61
}
62
63
ChatObject *ChatObject::findById(const int id)
64
{
65
    const std::map<int, ChatObject*>::iterator it = chatIdMap.find(id);
66
    if (it == chatIdMap.end())
67
        return nullptr;
68
    return (*it).second;
69

6
}