source: qutecom-coip/libs/owutil/util/src/IStateDPContext.cpp @ 125:d648f4cb122f

Last change on this file since 125:d648f4cb122f was 125:d648f4cb122f, checked in by laurent, 3 years ago

wengo => qutecom

File size: 2.2 KB
Line 
1/*
2 * QuteCom, a voice over Internet phone
3 * Copyright (C) 2010 Mbdsys
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19
20#include <util/IStateDPContext.h>
21
22#include <util/IStateDPState.h>
23#include <util/Logger.h>
24#include <util/SafeDelete.h>
25#include <util/String.h>
26
27IStateDPContext::IStateDPContext()
28        : NO_STATE_ID(-1) {
29        _currentState = NULL;
30        _stateDPMutex = new QMutex();
31}
32
33IStateDPContext::~IStateDPContext() {
34        OWSAFE_DELETE(_stateDPMutex);
35
36        for (StateMap::iterator it = _stateMap.begin();
37                it != _stateMap.end();
38                ++it) {
39                delete (*it).second;
40        }
41}
42
43void IStateDPContext::addState(int stateId, IStateDPState * state) {
44        if (state) {
45                StateMap::iterator it = _stateMap.find(stateId);
46                if (it != _stateMap.end()) {
47                        LOG_FATAL("id already in map. Cannot add this state.");
48                        return;
49                }
50
51                _stateMap[stateId] = state;
52        }
53}
54
55int IStateDPContext::getStateId() const {
56        int result = NO_STATE_ID;
57
58        if (_currentState) {
59                for (StateMap::const_iterator it = _stateMap.begin();
60                        it != _stateMap.end();
61                        ++it) {
62                        if ((*it).second == _currentState) {
63                                result = (*it).first;
64                                break;
65                        }
66                }
67        }
68
69        return result;
70}
71
72void IStateDPContext::setState(int stateId) {
73        QMutexLocker lock(_stateDPMutex);
74
75        StateMap::iterator it = _stateMap.find(stateId);
76        if (it == _stateMap.end()) {
77                LOG_FATAL("id not in map. Cannot set this state.");
78                return;
79        }
80
81        LOG_DEBUG("switch state to " + String::fromNumber(stateId));
82        _currentState = (*it).second;
83}
84
85void IStateDPContext::execute() {
86        _stateDPMutex->lock();
87        IStateDPState * state = _currentState;
88        _stateDPMutex->unlock();
89
90        if (state) {
91                state->execute();
92        }
93}
Note: See TracBrowser for help on using the repository browser.