]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/base.cpp
Assorted changes here, Extend() is not templated so you can pass it any pointer type...
[user/henk/code/inspircd.git] / src / base.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 #include "inspircd_config.h"
18 #include "base.h"
19 #include <time.h>
20 #include <map>
21 #include <deque>
22 #include <string>
23 #include "inspircd.h"
24 #include "modules.h"
25 #include "helperfuncs.h"
26
27 const int bitfields[]           =       {1,2,4,8,16,32,64,128};
28 const int inverted_bitfields[]  =       {~1,~2,~4,~8,~16,~32,~64,~128};
29
30 extern time_t TIME;
31
32 /* This is now a template in base.h
33  *
34  * bool Extensible::Extend(const std::string &key, char* p)
35  * {
36  *      // only add an item if it doesnt already exist
37  *      if (this->Extension_Items.find(key) == this->Extension_Items.end())
38  *      {
39  *              this->Extension_Items[key] = p;
40  *              log(DEBUG,"Extending object with item %s",key.c_str());
41  *              return true;
42  *      }
43  *      // item already exists, return false
44  *      return false;
45  * }
46  */
47
48 bool Extensible::Shrink(const std::string &key)
49 {
50         /* map::size_type map::erase( const key_type& key );
51          * returns the number of elements removed, std::map
52          * is single-associative so this should only be 0 or 1
53          */
54         if(this->Extension_Items.erase(key))
55         {
56                 log(DEBUG, "Shrinking object with item %s",key.c_str());
57                 return true;
58         }
59         else
60         {
61                 log(DEBUG, "Tried to shrink object with item %s but no items removed", key.c_str());            
62                 return false;
63         }
64 }
65
66 char* Extensible::GetExt(const std::string &key)
67 {
68         /* This was calling ExtensibleStore::find() twice,
69          * once to see if there was a value, and again to
70          * get that value if it was there. Now we store
71          * the iterator so we only have to search for it once.
72          */
73         ExtensibleStore::iterator iter = this->Extension_Items.find(key);
74         
75         if(iter != this->Extension_Items.end())
76         {
77                 return iter->second;
78         }
79         else
80         {
81                 return NULL;
82         }
83 }
84
85 void Extensible::GetExtList(std::deque<std::string> &list)
86 {
87         for (ExtensibleStore::iterator u = Extension_Items.begin(); u != Extension_Items.end(); u++)
88         {
89                 list.push_back(u->first);
90         }
91 }
92
93 void BoolSet::Set(int number)
94 {
95         this->bits |= bitfields[number];
96 }
97
98 void BoolSet::Unset(int number)
99 {
100         this->bits &= inverted_bitfields[number];
101 }
102
103 void BoolSet::Invert(int number)
104 {
105         this->bits ^= bitfields[number];
106 }
107
108 bool BoolSet::Get(int number)
109 {
110         return ((this->bits | bitfields[number]) > 0);
111 }
112
113 bool BoolSet::operator==(BoolSet other)
114 {
115         return (this->bits == other.bits);
116 }
117
118 BoolSet BoolSet::operator|(BoolSet other)
119 {
120         BoolSet x(this->bits | other.bits);
121         return x;
122 }
123
124 BoolSet BoolSet::operator&(BoolSet other)
125 {
126         BoolSet x(this->bits & other.bits);
127         return x;
128 }
129
130 BoolSet::BoolSet()
131 {
132         this->bits = 0;
133 }
134
135 BoolSet::BoolSet(char bitmask)
136 {
137         this->bits = bitmask;
138 }
139
140 bool BoolSet::operator=(BoolSet other)
141 {
142         this->bits = other.bits;
143         return true;
144 }