summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam <Adam@anope.org>2016-07-28 22:45:37 -0400
committerAdam <Adam@anope.org>2016-07-28 22:45:37 -0400
commit104dcb745de20ba6f4b4ab73a53841c6d52c715f (patch)
treed78f7f879da4e6ba0c97010643dc1ac132917af0
parentaeb8f29438358a07cc62f167456d6f04a8c84668 (diff)
Get rid of undefined behavior in configuration blocks by creating them as necessary, add templated set function
-rw-r--r--include/config.h9
-rw-r--r--include/serialize.h2
-rw-r--r--src/config.cpp41
3 files changed, 26 insertions, 26 deletions
diff --git a/include/config.h b/include/config.h
index 84f47ecb3..94ddb7e91 100644
--- a/include/config.h
+++ b/include/config.h
@@ -35,7 +35,8 @@ namespace Configuration
Block(const Anope::string &);
const Anope::string &GetName() const;
int CountBlock(const Anope::string &name);
- Block* GetBlock(const Anope::string &name, int num = 0);
+ Block* GetBlock(const Anope::string &name);
+ Block* GetBlock(const Anope::string &name, int num);
template<typename T> inline T Get(const Anope::string &tag)
{
@@ -56,7 +57,11 @@ namespace Configuration
return T();
}
- bool Set(const Anope::string &tag, const Anope::string &value);
+ template<typename T> void Set(const Anope::string &tag, const T &value)
+ {
+ Set(tag, stringify(value));
+ }
+
const item_map* GetItems() const;
};
diff --git a/include/serialize.h b/include/serialize.h
index 3ee9ce376..949866dec 100644
--- a/include/serialize.h
+++ b/include/serialize.h
@@ -566,7 +566,7 @@ class Serialize::Field : public CommonFieldBase<TypeImpl, T>
{
}
- T GetField(TypeImpl *s)
+ T GetField(TypeImpl *s) override
{
T* t = this->Get_(s);
diff --git a/src/config.cpp b/src/config.cpp
index ad73f8e59..b3de2864d 100644
--- a/src/config.cpp
+++ b/src/config.cpp
@@ -38,47 +38,37 @@ const Anope::string &Block::GetName() const
int Block::CountBlock(const Anope::string &bname)
{
- if (!this)
- return 0;
-
return blocks.count(bname);
}
-Block* Block::GetBlock(const Anope::string &bname, int num)
+Block* Block::GetBlock(const Anope::string &bname)
{
- if (!this)
- return NULL;
+ auto it = blocks.find(bname);
- std::pair<block_map::iterator, block_map::iterator> it = blocks.equal_range(bname);
+ if (it != blocks.end())
+ return &it->second;
- for (int i = 0; it.first != it.second; ++it.first, ++i)
- if (i == num)
- return &it.first->second;
- return NULL;
+ blocks.emplace(bname, bname);
+ return GetBlock(bname);
}
-bool Block::Set(const Anope::string &tag, const Anope::string &value)
+Block* Block::GetBlock(const Anope::string &bname, int num)
{
- if (!this)
- return false;
+ std::pair<block_map::iterator, block_map::iterator> it = blocks.equal_range(bname);
- items[tag] = value;
- return true;
+ for (int i = 0; it.first != it.second; ++it.first, ++i)
+ if (i == num)
+ return &it.first->second;
+ return nullptr;
}
const Block::item_map* Block::GetItems() const
{
- if (this)
- return &items;
- else
- return NULL;
+ return &items;
}
template<> Anope::string Block::Get(const Anope::string &tag, const Anope::string& def) const
{
- if (!this)
- return def;
-
Anope::map<Anope::string>::const_iterator it = items.find(tag);
if (it != items.end())
return it->second;
@@ -118,6 +108,11 @@ template<> unsigned int Block::Get(const Anope::string &tag, const Anope::string
return l;
}
+template<> void Block::Set(const Anope::string &tag, const Anope::string &value)
+{
+ items[tag] = value;
+}
+
static void ValidateNotEmpty(const Anope::string &block, const Anope::string &name, const Anope::string &value)
{
if (value.empty())