Skip to content

Commit

Permalink
It is now safe to call begin() and end() on a cfg::Option with an emp…
Browse files Browse the repository at this point in the history
…ty array.
  • Loading branch information
ayebear committed May 1, 2014
1 parent 96b092c commit 83208f3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
1 change: 1 addition & 0 deletions configfile.h
Expand Up @@ -19,6 +19,7 @@ See README.md for more information.
TODO:
Handle escape codes inside of strings.
Handle multiple array elements on the same line.
Support sub-sections by using a tree, similar to Option arrays.
Writing should optionally preserve formatting and comments.
*/
class File
Expand Down
18 changes: 14 additions & 4 deletions configoption.cpp
Expand Up @@ -6,6 +6,8 @@
namespace cfg
{

Option::OptionVector Option::emptyVector;

Option::Option():
str(),
number(0),
Expand Down Expand Up @@ -191,22 +193,30 @@ void Option::clear()

Option::OptionVector::iterator Option::begin()
{
return options->begin();
if (options)
return options->begin();
return emptyVector.begin();
}

Option::OptionVector::iterator Option::end()
{
return options->end();
if (options)
return options->end();
return emptyVector.end();
}

Option::OptionVector::const_iterator Option::begin() const
{
return options->begin();
if (options)
return options->begin();
return emptyVector.begin();
}

Option::OptionVector::const_iterator Option::end() const
{
return options->end();
if (options)
return options->end();
return emptyVector.end();
}

std::string Option::buildArrayString(const std::string& indentStr) const
Expand Down
3 changes: 3 additions & 0 deletions configoption.h
Expand Up @@ -100,6 +100,9 @@ class Option
// Also, this is only created when push() is called for the first time
// Also, this array is separate from the option itself, and nothing is kept in sync
// This means that the first element can be different from the option.

static OptionVector emptyVector;
// This is used for returning iterators when the array isn't allocated
};

template <typename Type>
Expand Down

0 comments on commit 83208f3

Please sign in to comment.