[2771] Applied MaNGOS coding style (see trunk/bcpp.cfg).
[mangos-git.git] / src / game / HateMatrix.h
blobb831eda06d7a8c52706dd21b980f29d15a597647
1 /*
2 * Copyright (C) 2005,2006 MaNGOS <http://www.mangosproject.org/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #ifndef MANGOS_HATEMATRIX_H
20 #define MANGOS_HATEMATRIX_H
22 #include "Utilities/HashMap.h"
23 #include <cassert>
25 class Unit;
27 struct MANGOS_DLL_DECL HateMatrix
29 typedef hash_map<Unit *, uint32> HateMatrixMapType;
31 inline uint32 operator[](Unit *unit) const
33 HateMatrixMapType::const_iterator iter = i_hateValues.find(unit);
34 return (iter == i_hateValues.end() ? 0 : iter->second);
37 inline uint32& operator[](Unit *unit)
39 HateMatrixMapType::iterator iter = i_hateValues.find(unit);
40 if( iter == i_hateValues.end() )
42 std::pair<HateMatrixMapType::iterator, bool> p = i_hateValues.insert( HateMatrixMapType::value_type(unit, 0) );
43 assert(p.second);
44 iter = p.first;
47 return iter->second;
50 inline void ClearMatrix(void) { i_hateValues.clear(); }
52 inline void RemoveValue(Unit *unit)
54 HateMatrixMapType::iterator iter = i_hateValues.find(unit);
55 if( iter != i_hateValues.end() )
56 i_hateValues.erase( iter );
59 inline void AddValue(Unit *unit, uint32 val)
61 (*this)[unit] += val;
64 private:
65 HateMatrixMapType i_hateValues;
68 struct HateBinder
70 static uint32 si_noHateValue;
71 uint32 &i_hateValue;
72 Unit *i_unit;
73 HateBinder(uint32 &val, Unit *u) : i_hateValue(val), i_unit(u) {}
74 HateBinder() : i_hateValue(si_noHateValue), i_unit(NULL) {}
75 HateBinder(const HateBinder &obj) : i_hateValue(obj.i_hateValue), i_unit(obj.i_unit) {}
77 HateBinder& operator=(const HateBinder &obj)
79 this->~HateBinder();
80 new (this) HateBinder(obj);
81 return *this;
84 #endif