/src/ntopng/include/Bitmap.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * |
3 | | * (C) 2013-25 - ntop.org |
4 | | * |
5 | | * |
6 | | * This program is free software; you can redistribute it and/or modify |
7 | | * it under the terms of the GNU General Public License as published by |
8 | | * the Free Software Foundation; either version 3 of the License, or |
9 | | * (at your option) any later version. |
10 | | * |
11 | | * This program is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | | * GNU General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU General Public License |
17 | | * along with this program; if not, write to the Free Software Foundation, |
18 | | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
19 | | * |
20 | | */ |
21 | | |
22 | | #ifndef _BITMAP_H_ |
23 | | #define _BITMAP_H_ |
24 | | |
25 | | #include "ntop_includes.h" |
26 | | |
27 | | template <typename T> |
28 | | class Bitmap { |
29 | | private: |
30 | | T bitmap; |
31 | | |
32 | | public: |
33 | | Bitmap() { reset(); } |
34 | | |
35 | | static inline u_int numBits() { return sizeof(bitmap) * 8; }; |
36 | | inline void reset() { bitmap = 0; }; |
37 | 0 | inline void setBit(u_int8_t id) { bitmap |= ((T)1) << id; }; |
38 | 0 | inline void clearBit(u_int8_t id) { bitmap &= ~(((T)1) << id); }; |
39 | 0 | inline bool isSetBit(u_int8_t id) const { |
40 | 0 | return (((bitmap >> id) & 1) ? true : false); |
41 | 0 | }; |
42 | | inline void bitmapOr(const Bitmap b) { bitmap |= b.bitmap; }; |
43 | | inline void set(const Bitmap *b) { bitmap = b->bitmap; }; |
44 | | inline bool equal(const Bitmap *b) const { return bitmap == b->bitmap; }; |
45 | | |
46 | | void lua(lua_State *vm, const char *label) const { |
47 | | lua_newtable(vm); |
48 | | |
49 | | for (u_int i = 0; i < numBits(); i++) { |
50 | | if (isSetBit(i)) { |
51 | | lua_pushboolean(vm, true); /* The boolean indicating this risk is set */ |
52 | | lua_pushinteger( |
53 | | vm, i); /* The integer risk id, used as key of this lua table */ |
54 | | lua_insert(vm, -2); |
55 | | lua_settable(vm, -3); |
56 | | } |
57 | | } |
58 | | |
59 | | lua_pushstring(vm, label); |
60 | | lua_insert(vm, -2); |
61 | | lua_settable(vm, -3); |
62 | | }; |
63 | | }; |
64 | | |
65 | | #endif /* _BITMAP_H_ */ |