Make cajun containers implement the STL container interfaces rather than being retarded special snowflakes

Originally committed to SVN as r5746.
This commit is contained in:
Thomas Goyne 2011-10-17 21:59:35 +00:00
parent 85bfb91f73
commit 2f64a116a7
11 changed files with 136 additions and 235 deletions

View file

@ -75,7 +75,7 @@ Hotkey::Hotkey(const std::string &file, const std::string &default_config)
json::UnknownElement hotkey_root = agi::json_util::file(config_file, default_config); json::UnknownElement hotkey_root = agi::json_util::file(config_file, default_config);
json::Object object = hotkey_root; json::Object object = hotkey_root;
for (json::Object::const_iterator index(object.Begin()); index != object.End(); index++) { for (json::Object::const_iterator index(object.begin()); index != object.end(); index++) {
const json::Object::Member& member = *index; const json::Object::Member& member = *index;
const json::Object& obj = member.element; const json::Object& obj = member.element;
BuildHotkey(member.name, obj); BuildHotkey(member.name, obj);
@ -84,22 +84,20 @@ Hotkey::Hotkey(const std::string &file, const std::string &default_config)
void Hotkey::BuildHotkey(std::string const& context, const json::Object& object) { void Hotkey::BuildHotkey(std::string const& context, const json::Object& object) {
for (json::Object::const_iterator index(object.Begin()); index != object.End(); index++) { for (json::Object::const_iterator index(object.begin()); index != object.end(); index++) {
const json::Object::Member& member = *index; const json::Object::Member& member = *index;
const json::Array& array = member.element; const json::Array& array = member.element;
for (json::Array::const_iterator arr_index(array.Begin()); arr_index != array.End(); arr_index++) { for (json::Array::const_iterator arr_index(array.begin()); arr_index != array.end(); arr_index++) {
Combo combo(context, member.name); Combo combo(context, member.name);
const json::Object& obj = *arr_index; const json::Object& obj = *arr_index;
const json::Array& arr_mod = obj["modifiers"]; const json::Array& arr_mod = obj["modifiers"];
if (arr_mod.Size() > 0) { for (json::Array::const_iterator arr_mod_index(arr_mod.begin()); arr_mod_index != arr_mod.end(); arr_mod_index++) {
for (json::Array::const_iterator arr_mod_index(arr_mod.Begin()); arr_mod_index != arr_mod.End(); arr_mod_index++) { const json::String& key_mod = *arr_mod_index;
const json::String& key_mod = *arr_mod_index; combo.KeyInsert(key_mod.Value());
combo.KeyInsert(key_mod.Value());
} // for arr_mod_index
} }
combo.KeyInsert(static_cast<const json::String&>(obj["key"]).Value()); combo.KeyInsert(static_cast<const json::String&>(obj["key"]).Value());
ComboInsert(combo); ComboInsert(combo);
@ -178,7 +176,7 @@ void Hotkey::Flush() {
json::Array modifiers; json::Array modifiers;
for (int i = 0; i != combo_map.size()-1; i++) { for (int i = 0; i != combo_map.size()-1; i++) {
modifiers.Insert(json::String(combo_map[i])); modifiers.push_back(json::String(combo_map[i]));
} }
json::Object hotkey; json::Object hotkey;
@ -188,7 +186,7 @@ void Hotkey::Flush() {
json::Object& context_obj = root[index->second.Context()]; json::Object& context_obj = root[index->second.Context()];
json::Array& combo_array = context_obj[index->second.CmdName()]; json::Array& combo_array = context_obj[index->second.CmdName()];
combo_array.Insert(hotkey); combo_array.push_back(hotkey);
} }
io::Save file(config_file); io::Save file(config_file);

View file

@ -89,17 +89,17 @@ LogSink::~LogSink() {
entry["line"] = json::Number(sink[i]->line); entry["line"] = json::Number(sink[i]->line);
entry["message"] = json::String(std::string(sink[i]->message, sink[i]->len)); entry["message"] = json::String(std::string(sink[i]->message, sink[i]->len));
array.Insert(entry); array.push_back(entry);
} }
json::Array timeval_open; json::Array timeval_open;
timeval_open.Insert(json::Number(time_start.tv_sec)); timeval_open.push_back(json::Number(time_start.tv_sec));
timeval_open.Insert(json::Number(time_start.tv_usec)); timeval_open.push_back(json::Number(time_start.tv_usec));
root["timeval"]["open"] = timeval_open; root["timeval"]["open"] = timeval_open;
json::Array timeval_close; json::Array timeval_close;
timeval_close.Insert(json::Number(time_close.tv_sec)); timeval_close.push_back(json::Number(time_close.tv_sec));
timeval_close.Insert(json::Number(time_close.tv_usec)); timeval_close.push_back(json::Number(time_close.tv_usec));
root["timeval"]["close"] = timeval_close; root["timeval"]["close"] = timeval_close;

View file

@ -34,7 +34,7 @@ MRUManager::MRUManager(const std::string &config, const std::string &default_con
json::UnknownElement root = json_util::file(config, default_config); json::UnknownElement root = json_util::file(config, default_config);
const json::Object& root_new = (json::Object)root; const json::Object& root_new = (json::Object)root;
json::Object::const_iterator index_object(root_new.Begin()), index_objectEnd(root_new.End()); json::Object::const_iterator index_object(root_new.begin()), index_objectEnd(root_new.end());
for (; index_object != index_objectEnd; ++index_object) { for (; index_object != index_objectEnd; ++index_object) {
const json::Object::Member& member = *index_object; const json::Object::Member& member = *index_object;
@ -98,7 +98,7 @@ void MRUManager::Flush() {
const MRUListMap &map_list = i->second; const MRUListMap &map_list = i->second;
for (MRUListMap::const_iterator i_lst = map_list.begin(); i_lst != map_list.end(); ++i_lst) { for (MRUListMap::const_iterator i_lst = map_list.begin(); i_lst != map_list.end(); ++i_lst) {
array.Insert(json::String(*i_lst)); array.push_back(json::String(*i_lst));
} }
} }
@ -121,7 +121,7 @@ static json::String cast_str(json::UnknownElement const& e) {
/// @param array json::Array of values. /// @param array json::Array of values.
void MRUManager::Load(const std::string &key, const json::Array& array) { void MRUManager::Load(const std::string &key, const json::Array& array) {
try { try {
transform(array.Begin(), array.End(), back_inserter(mru[key]), cast_str); transform(array.begin(), array.end(), back_inserter(mru[key]), cast_str);
} }
catch (json::Exception const&) { catch (json::Exception const&) {
// Out of date MRU file; just discard the data and skip it // Out of date MRU file; just discard the data and skip it

View file

@ -143,7 +143,7 @@ void Options::Flush() {
for (std::vector<std::string>::const_iterator i_str = array_string.begin(); i_str != array_string.end(); ++i_str) { for (std::vector<std::string>::const_iterator i_str = array_string.begin(); i_str != array_string.end(); ++i_str) {
json::Object obj; json::Object obj;
obj["string"] = json::String(*i_str); obj["string"] = json::String(*i_str);
array.Insert(obj); array.push_back(obj);
} }
PutOption(obj_out, i->first, (json::Array)array); PutOption(obj_out, i->first, (json::Array)array);
@ -159,7 +159,7 @@ void Options::Flush() {
for (std::vector<int64_t>::const_iterator i_int = array_int.begin(); i_int != array_int.end(); ++i_int) { for (std::vector<int64_t>::const_iterator i_int = array_int.begin(); i_int != array_int.end(); ++i_int) {
json::Object obj; json::Object obj;
obj["int"] = json::Number((const double)*i_int); obj["int"] = json::Number((const double)*i_int);
array.Insert(obj); array.push_back(obj);
} }
PutOption(obj_out, i->first, (json::Array)array); PutOption(obj_out, i->first, (json::Array)array);
} }
@ -174,7 +174,7 @@ void Options::Flush() {
for (std::vector<double>::const_iterator i_double = array_double.begin(); i_double != array_double.end(); ++i_double) { for (std::vector<double>::const_iterator i_double = array_double.begin(); i_double != array_double.end(); ++i_double) {
json::Object obj; json::Object obj;
obj["double"] = json::Number(*i_double); obj["double"] = json::Number(*i_double);
array.Insert(obj); array.push_back(obj);
} }
PutOption(obj_out, i->first, (json::Array)array); PutOption(obj_out, i->first, (json::Array)array);
} }
@ -193,7 +193,7 @@ void Options::Flush() {
obj["colour"] = json::String(str); obj["colour"] = json::String(str);
array.Insert(obj); array.push_back(obj);
} }
PutOption(obj_out, i->first, (json::Array)array); PutOption(obj_out, i->first, (json::Array)array);
} }
@ -208,7 +208,7 @@ void Options::Flush() {
for (std::vector<bool>::const_iterator i_bool = array_bool.begin(); i_bool != array_bool.end(); ++i_bool) { for (std::vector<bool>::const_iterator i_bool = array_bool.begin(); i_bool != array_bool.end(); ++i_bool) {
json::Object obj; json::Object obj;
obj["bool"] = json::Boolean(*i_bool); obj["bool"] = json::Boolean(*i_bool);
array.Insert(obj); array.push_back(obj);
} }
PutOption(obj_out, i->first, (json::Array)array); PutOption(obj_out, i->first, (json::Array)array);
} }
@ -224,10 +224,10 @@ void Options::Flush() {
bool Options::PutOption(json::Object &obj, const std::string &path, const json::UnknownElement &value) { bool Options::PutOption(json::Object &obj, const std::string &path, const json::UnknownElement &value) {
// Having a '/' denotes it is a leaf. // Having a '/' denotes it is a leaf.
if (path.find('/') == std::string::npos) { if (path.find('/') == std::string::npos) {
json::Object::iterator pos = obj.Find(path); json::Object::iterator pos = obj.find(path);
// Fail if a key of the same name already exists. // Fail if a key of the same name already exists.
if (pos != obj.End()) if (pos != obj.end())
throw OptionErrorDuplicateKey("Key already exists"); throw OptionErrorDuplicateKey("Key already exists");
obj[path] = value; obj[path] = value;
@ -235,11 +235,11 @@ bool Options::PutOption(json::Object &obj, const std::string &path, const json::
} else { } else {
std::string thispart = path.substr(0, path.find("/")); std::string thispart = path.substr(0, path.find("/"));
std::string restpart = path.substr(path.find("/")+1, path.size()); std::string restpart = path.substr(path.find("/")+1, path.size());
json::Object::iterator pos = obj.Find(thispart); json::Object::iterator pos = obj.find(thispart);
// New key, make object. // New key, make object.
if (pos == obj.End()) if (pos == obj.end())
pos = obj.Insert(json::Object::Member(thispart, json::Object())); pos = obj.insert(json::Object::Member(thispart, json::Object()));
PutOptionVisitor visitor(restpart, value); PutOptionVisitor visitor(restpart, value);
pos->element.Accept(visitor); pos->element.Accept(visitor);

View file

@ -37,7 +37,7 @@ ConfigVisitor::ConfigVisitor(OptionValueMap &val, const std::string &member_name
} }
void ConfigVisitor::Visit(const json::Object& object) { void ConfigVisitor::Visit(const json::Object& object) {
json::Object::const_iterator index(object.Begin()), index_end(object.End()); json::Object::const_iterator index(object.begin()), index_end(object.end());
if (!name.empty()) if (!name.empty())
name += "/"; name += "/";
@ -56,12 +56,12 @@ void ConfigVisitor::Visit(const json::Object& object) {
void ConfigVisitor::Visit(const json::Array& array) { void ConfigVisitor::Visit(const json::Array& array) {
OptionValueList *array_list = NULL; OptionValueList *array_list = NULL;
json::Array::const_iterator index(array.Begin()), indexEnd(array.End()); json::Array::const_iterator index(array.begin()), indexEnd(array.end());
for (; index != indexEnd; ++index) { for (; index != indexEnd; ++index) {
const json::Object& index_array = *index; const json::Object& index_array = *index;
json::Object::const_iterator index_object(index_array.Begin()), index_objectEnd(index_array.End()); json::Object::const_iterator index_object(index_array.begin()), index_objectEnd(index_array.end());
for (; index_object != index_objectEnd; ++index_object) { for (; index_object != index_objectEnd; ++index_object) {
const json::Object::Member& member = *index_object; const json::Object::Member& member = *index_object;

View file

@ -13,7 +13,7 @@ Author: Terry Caton
#include <string> #include <string>
#include <stdexcept> #include <stdexcept>
/* /*
TODO: TODO:
* better documentation (doxygen?) * better documentation (doxygen?)
@ -28,8 +28,6 @@ namespace json
///////////////////////////////////////////////// /////////////////////////////////////////////////
// forward declarations (more info further below) // forward declarations (more info further below)
class Visitor; class Visitor;
class ConstVisitor; class ConstVisitor;
@ -48,23 +46,19 @@ class Null;
///////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////
// Exception - base class for all JSON-related runtime errors // Exception - base class for all JSON-related runtime errors
class Exception : public std::runtime_error class Exception : public std::runtime_error
{ {
public: public:
Exception(const std::string& sMessage); Exception(const std::string& sMessage) : std::runtime_error(sMessage) { }
}; };
///////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////
// UnknownElement - provides a typesafe surrogate for any of the JSON- // UnknownElement - provides a typesafe surrogate for any of the JSON-
// sanctioned element types. This class allows the Array and Object // sanctioned element types. This class allows the Array and Object
// class to effectively contain a heterogeneous set of child elements. // class to effectively contain a heterogeneous set of child elements.
// The cast operators provide convenient implicit downcasting, while // The cast operators provide convenient implicit downcasting, while
// preserving dynamic type safety by throwing an exception during a // preserving dynamic type safety by throwing an exception during a
// a bad cast. // a bad cast.
// The object & array element index operators (operators [std::string] // The object & array element index operators (operators [std::string]
// and [size_t]) provide convenient, quick access to child elements. // and [size_t]) provide convenient, quick access to child elements.
// They are a logical extension of the cast operators. These child // They are a logical extension of the cast operators. These child
@ -128,7 +122,7 @@ private:
class CastVisitor; class CastVisitor;
class ConstCastVisitor; class ConstCastVisitor;
template <typename ElementTypeT> template <typename ElementTypeT>
class CastVisitor_T; class CastVisitor_T;
@ -146,50 +140,54 @@ private:
///////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////
// Array - mimics std::deque<UnknownElement>. The array contents are effectively // Array - mimics std::deque<UnknownElement>. The array contents are effectively
// heterogeneous thanks to the ElementUnknown class. push_back has been replaced // heterogeneous thanks to the ElementUnknown class.
// by more generic insert functions.
class Array class Array
{ {
public: public:
typedef std::deque<UnknownElement> Elements; typedef std::deque<UnknownElement> elements;
typedef Elements::iterator iterator; typedef elements::iterator iterator;
typedef Elements::const_iterator const_iterator; typedef elements::const_iterator const_iterator;
iterator Begin(); iterator begin() { return m_Elements.begin(); }
iterator End(); iterator end() { return m_Elements.end(); }
const_iterator Begin() const; const_iterator begin() const { return m_Elements.begin(); }
const_iterator End() const; const_iterator end() const { return m_Elements.end(); }
iterator Insert(const UnknownElement& element, iterator itWhere);
iterator Insert(const UnknownElement& element);
iterator Erase(iterator itWhere);
void Resize(size_t newSize);
void Clear();
size_t Size() const; iterator insert(iterator it, const UnknownElement& element) { m_Elements.insert(it, element); }
bool Empty() const; void push_back(const UnknownElement& element) { m_Elements.push_back(element); }
iterator erase(iterator it) { return m_Elements.erase(it); }
void resize(size_t newsize) { m_Elements.resize(newsize); }
void clear() { m_Elements.clear(); }
size_t size() const { return m_Elements.size(); }
bool empty() const { return m_Elements.empty(); }
UnknownElement& front() { return m_Elements.front(); }
const UnknownElement& front() const { return m_Elements.front(); }
UnknownElement& back() { return m_Elements.back(); }
const UnknownElement& back() const { return m_Elements.back(); }
UnknownElement& operator[] (size_t index); UnknownElement& operator[] (size_t index);
const UnknownElement& operator[] (size_t index) const; const UnknownElement& operator[] (size_t index) const;
bool operator == (const Array& array) const; bool operator == (const Array& array) const { return m_Elements == array.m_Elements; }
private: private:
Elements m_Elements; elements m_Elements;
}; };
///////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////
// Object - mimics std::map<std::string, UnknownElement>. The member value // Object - mimics std::map<std::string, UnknownElement>. The member value
// contents are effectively heterogeneous thanks to the UnknownElement class // contents are effectively heterogeneous thanks to the UnknownElement class
class Object class Object
{ {
public: public:
struct Member { struct Member {
Member(const std::string& nameIn = std::string(), const UnknownElement& elementIn = UnknownElement()); Member(const std::string& nameIn = std::string(), const UnknownElement& elementIn = UnknownElement())
: name(nameIn), element(elementIn) { }
bool operator == (const Member& member) const; bool operator == (const Member& member) const;
@ -201,23 +199,22 @@ public:
typedef Members::iterator iterator; typedef Members::iterator iterator;
typedef Members::const_iterator const_iterator; typedef Members::const_iterator const_iterator;
bool operator == (const Object& object) const; bool operator == (const Object& object) const { return m_Members == object.m_Members; }
iterator Begin(); iterator begin() { return m_Members.begin(); }
iterator End(); iterator end() { return m_Members.end(); }
const_iterator Begin() const; const_iterator begin() const { return m_Members.begin(); }
const_iterator End() const; const_iterator end() const { return m_Members.end(); }
size_t Size() const; size_t size() const { return m_Members.size(); }
bool Empty() const; bool empty() const { return m_Members.empty(); }
iterator Find(const std::string& name); iterator find(const std::string& name);
const_iterator Find(const std::string& name) const; const_iterator find(const std::string& name) const;
iterator Insert(const Member& member); iterator insert(const Member& member);
iterator Insert(const Member& member, iterator itWhere); iterator erase(iterator it) { return m_Members.erase(it); }
iterator Erase(iterator itWhere); void clear() { m_Members.clear(); }
void Clear();
UnknownElement& operator [](const std::string& name); UnknownElement& operator [](const std::string& name);
const UnknownElement& operator [](const std::string& name) const; const UnknownElement& operator [](const std::string& name) const;
@ -266,7 +263,7 @@ public:
}; };
} // End namespace } // end namespace
#include "elements.inl" #include "elements.inl"

View file

@ -12,7 +12,7 @@ Author: Terry Caton
#include <algorithm> #include <algorithm>
#include <map> #include <map>
/* /*
TODO: TODO:
* better documentation * better documentation
@ -22,11 +22,6 @@ TODO:
namespace json namespace json
{ {
inline Exception::Exception(const std::string& sMessage) :
std::runtime_error(sMessage) {}
///////////////////////// /////////////////////////
// UnknownElement members // UnknownElement members
@ -133,7 +128,7 @@ inline UnknownElement::operator Boolean& () { return ConvertTo<Boolean>(); }
inline UnknownElement::operator String& () { return ConvertTo<String>(); } inline UnknownElement::operator String& () { return ConvertTo<String>(); }
inline UnknownElement::operator Null& () { return ConvertTo<Null>(); } inline UnknownElement::operator Null& () { return ConvertTo<Null>(); }
inline UnknownElement& UnknownElement::operator = (const UnknownElement& unknown) inline UnknownElement& UnknownElement::operator = (const UnknownElement& unknown)
{ {
delete m_pImp; delete m_pImp;
m_pImp = unknown.m_pImp->Clone(); m_pImp = unknown.m_pImp->Clone();
@ -143,29 +138,25 @@ inline UnknownElement& UnknownElement::operator = (const UnknownElement& unknown
inline UnknownElement& UnknownElement::operator[] (const std::string& key) inline UnknownElement& UnknownElement::operator[] (const std::string& key)
{ {
// the people want an object. make us one if we aren't already // the people want an object. make us one if we aren't already
Object& object = ConvertTo<Object>(); return ConvertTo<Object>()[key];
return object[key];
} }
inline const UnknownElement& UnknownElement::operator[] (const std::string& key) const inline const UnknownElement& UnknownElement::operator[] (const std::string& key) const
{ {
// throws if we aren't an object // throws if we aren't an object
const Object& object = CastTo<Object>(); return CastTo<Object>()[key];
return object[key];
} }
inline UnknownElement& UnknownElement::operator[] (size_t index) inline UnknownElement& UnknownElement::operator[] (size_t index)
{ {
// the people want an array. make us one if we aren't already // the people want an array. make us one if we aren't already
Array& array = ConvertTo<Array>(); return ConvertTo<Array>()[index];
return array[index];
} }
inline const UnknownElement& UnknownElement::operator[] (size_t index) const inline const UnknownElement& UnknownElement::operator[] (size_t index) const
{ {
// throws if we aren't an array // throws if we aren't an array
const Array& array = CastTo<Array>(); return CastTo<Array>()[index];
return array[index];
} }
@ -182,7 +173,7 @@ const ElementTypeT& UnknownElement::CastTo() const
template <typename ElementTypeT> template <typename ElementTypeT>
ElementTypeT& UnknownElement::ConvertTo() ElementTypeT& UnknownElement::ConvertTo()
{ {
CastVisitor_T<ElementTypeT> castVisitor; CastVisitor_T<ElementTypeT> castVisitor;
m_pImp->Accept(castVisitor); m_pImp->Accept(castVisitor);
@ -206,160 +197,80 @@ inline bool UnknownElement::operator == (const UnknownElement& element) const
return m_pImp->Compare(*element.m_pImp); return m_pImp->Compare(*element.m_pImp);
} }
////////////////// //////////////////
// Object members // Object members
inline bool Object::Member::operator == (const Member& member) const
inline Object::Member::Member(const std::string& nameIn, const UnknownElement& elementIn) :
name(nameIn), element(elementIn) {}
inline bool Object::Member::operator == (const Member& member) const
{ {
return name == member.name && return name == member.name && element == member.element;
element == member.element;
} }
class Object::Finder : public std::unary_function<Object::Member, bool> class Object::Finder : public std::unary_function<Object::Member, bool>
{ {
std::string m_name;
public: public:
Finder(const std::string& name) : m_name(name) {} Finder(const std::string& name) : m_name(name) {}
bool operator () (const Object::Member& member) { bool operator () (const Object::Member& member) {
return member.name == m_name; return member.name == m_name;
} }
private:
std::string m_name;
}; };
inline Object::iterator Object::find(const std::string& name)
inline Object::iterator Object::Begin() { return m_Members.begin(); }
inline Object::iterator Object::End() { return m_Members.end(); }
inline Object::const_iterator Object::Begin() const { return m_Members.begin(); }
inline Object::const_iterator Object::End() const { return m_Members.end(); }
inline size_t Object::Size() const { return m_Members.size(); }
inline bool Object::Empty() const { return m_Members.empty(); }
inline Object::iterator Object::Find(const std::string& name)
{ {
return std::find_if(m_Members.begin(), m_Members.end(), Finder(name)); return std::find_if(m_Members.begin(), m_Members.end(), Finder(name));
} }
inline Object::const_iterator Object::Find(const std::string& name) const inline Object::const_iterator Object::find(const std::string& name) const
{ {
return std::find_if(m_Members.begin(), m_Members.end(), Finder(name)); return std::find_if(m_Members.begin(), m_Members.end(), Finder(name));
} }
inline Object::iterator Object::Insert(const Member& member) inline Object::iterator Object::insert(const Member& member)
{ {
return Insert(member, End()); iterator it = find(member.name);
}
inline Object::iterator Object::Insert(const Member& member, iterator itWhere)
{
iterator it = Find(member.name);
if (it != m_Members.end()) if (it != m_Members.end())
throw Exception("Object member already exists: " + member.name); throw Exception("Object member already exists: " + member.name);
it = m_Members.insert(itWhere, member); m_Members.push_back(member);
return it; return --it;
}
inline Object::iterator Object::Erase(iterator itWhere)
{
return m_Members.erase(itWhere);
} }
inline UnknownElement& Object::operator [](const std::string& name) inline UnknownElement& Object::operator [](const std::string& name)
{ {
iterator it = find(name);
iterator it = Find(name);
if (it == m_Members.end()) if (it == m_Members.end())
{ {
Member member(name); it = insert(Member(name));
it = Insert(member, End());
} }
return it->element; return it->element;
} }
inline const UnknownElement& Object::operator [](const std::string& name) const inline const UnknownElement& Object::operator [](const std::string& name) const
{ {
const_iterator it = Find(name); const_iterator it = find(name);
if (it == End()) if (it == end())
throw Exception("Object member not found: " + name); throw Exception("Object member not found: " + name);
return it->element; return it->element;
} }
inline void Object::Clear()
{
m_Members.clear();
}
inline bool Object::operator == (const Object& object) const
{
return m_Members == object.m_Members;
}
///////////////// /////////////////
// Array members // Array members
inline Array::iterator Array::Begin() { return m_Elements.begin(); }
inline Array::iterator Array::End() { return m_Elements.end(); }
inline Array::const_iterator Array::Begin() const { return m_Elements.begin(); }
inline Array::const_iterator Array::End() const { return m_Elements.end(); }
inline Array::iterator Array::Insert(const UnknownElement& element, iterator itWhere)
{
return m_Elements.insert(itWhere, element);
}
inline Array::iterator Array::Insert(const UnknownElement& element)
{
return Insert(element, End());
}
inline Array::iterator Array::Erase(iterator itWhere)
{
return m_Elements.erase(itWhere);
}
inline void Array::Resize(size_t newSize)
{
m_Elements.resize(newSize);
}
inline size_t Array::Size() const { return m_Elements.size(); }
inline bool Array::Empty() const { return m_Elements.empty(); }
inline UnknownElement& Array::operator[] (size_t index) inline UnknownElement& Array::operator[] (size_t index)
{ {
size_t nMinSize = index + 1; // zero indexed size_t nMinsize = index + 1; // zero indexed
if (m_Elements.size() < nMinSize) if (m_Elements.size() < nMinsize)
m_Elements.resize(nMinSize); m_Elements.resize(nMinsize);
return m_Elements[index]; return m_Elements[index];
} }
inline const UnknownElement& Array::operator[] (size_t index) const inline const UnknownElement& Array::operator[] (size_t index) const
{ {
if (index >= m_Elements.size()) if (index >= m_Elements.size())
throw Exception("Array out of bounds"); throw Exception("Array out of bounds");
return m_Elements[index]; return m_Elements[index];
} }
inline void Array::Clear() {
m_Elements.clear();
}
inline bool Array::operator == (const Array& array) const
{
return m_Elements == array.m_Elements;
}
//////////////////////// ////////////////////////
// TrivialType_T members // TrivialType_T members
@ -370,25 +281,25 @@ TrivialType_T<DataTypeT>::TrivialType_T(const DataTypeT& t) :
template <typename DataTypeT> template <typename DataTypeT>
TrivialType_T<DataTypeT>::operator DataTypeT&() TrivialType_T<DataTypeT>::operator DataTypeT&()
{ {
return Value(); return Value();
} }
template <typename DataTypeT> template <typename DataTypeT>
TrivialType_T<DataTypeT>::operator const DataTypeT&() const TrivialType_T<DataTypeT>::operator const DataTypeT&() const
{ {
return Value(); return Value();
} }
template <typename DataTypeT> template <typename DataTypeT>
DataTypeT& TrivialType_T<DataTypeT>::Value() DataTypeT& TrivialType_T<DataTypeT>::Value()
{ {
return m_tValue; return m_tValue;
} }
template <typename DataTypeT> template <typename DataTypeT>
const DataTypeT& TrivialType_T<DataTypeT>::Value() const const DataTypeT& TrivialType_T<DataTypeT>::Value() const
{ {
return m_tValue; return m_tValue;
} }
template <typename DataTypeT> template <typename DataTypeT>
@ -397,8 +308,6 @@ bool TrivialType_T<DataTypeT>::operator == (const TrivialType_T<DataTypeT>& triv
return m_tValue == trivial.m_tValue; return m_tValue == trivial.m_tValue;
} }
////////////////// //////////////////
// Null members // Null members
@ -407,6 +316,4 @@ inline bool Null::operator == (const Null& trivial) const
return true; return true;
} }
} // end namespace
} // End namespace

View file

@ -417,13 +417,12 @@ inline void Reader::Parse(Object& object, Reader::TokenStream& tokenStream)
// try adding it to the object (this could throw) // try adding it to the object (this could throw)
try try
{ {
object.Insert(member); object.insert(member);
} }
catch (Exception&) catch (Exception&)
{ {
// must be a duplicate name // must be a duplicate name
std::string sMessage = "Duplicate object member token: " + member.name; throw ParseException("Duplicate object member token: " + member.name, tokenName.locBegin, tokenName.locEnd);
throw ParseException(sMessage, tokenName.locBegin, tokenName.locEnd);
} }
bContinue = (tokenStream.EOS() == false && bContinue = (tokenStream.EOS() == false &&
@ -445,8 +444,8 @@ inline void Reader::Parse(Array& array, Reader::TokenStream& tokenStream)
while (bContinue) while (bContinue)
{ {
// ...what's next? could be anything // ...what's next? could be anything
Array::iterator itElement = array.Insert(UnknownElement()); array.push_back(UnknownElement());
UnknownElement& element = *itElement; UnknownElement& element = array.back();
Parse(element, tokenStream); Parse(element, tokenStream);
bContinue = (tokenStream.EOS() == false && bContinue = (tokenStream.EOS() == false &&

View file

@ -46,21 +46,21 @@ void Writer::Write_i(const ElementTypeT& element, std::ostream& ostr)
inline void Writer::Write_i(const Array& array) inline void Writer::Write_i(const Array& array)
{ {
if (array.Empty()) if (array.empty())
m_ostr << "[]"; m_ostr << "[]";
else else
{ {
m_ostr << '[' << std::endl; m_ostr << '[' << std::endl;
++m_nTabDepth; ++m_nTabDepth;
Array::const_iterator it(array.Begin()), Array::const_iterator it(array.begin()),
itEnd(array.End()); itend(array.end());
while (it != itEnd) { while (it != itend) {
m_ostr << std::string(m_nTabDepth, '\t'); m_ostr << std::string(m_nTabDepth, '\t');
Write_i(*it); Write_i(*it);
if (++it != itEnd) if (++it != itend)
m_ostr << ','; m_ostr << ',';
m_ostr << std::endl; m_ostr << std::endl;
} }
@ -72,20 +72,20 @@ inline void Writer::Write_i(const Array& array)
inline void Writer::Write_i(const Object& object) inline void Writer::Write_i(const Object& object)
{ {
if (object.Empty()) if (object.empty())
m_ostr << "{}"; m_ostr << "{}";
else else
{ {
m_ostr << '{' << std::endl; m_ostr << '{' << std::endl;
++m_nTabDepth; ++m_nTabDepth;
Object::const_iterator it(object.Begin()), Object::const_iterator it(object.begin()),
itEnd(object.End()); itend(object.end());
while (it != itEnd) { while (it != itend) {
m_ostr << std::string(m_nTabDepth, '\t') << '"' << it->name << "\" : "; m_ostr << std::string(m_nTabDepth, '\t') << '"' << it->name << "\" : ";
Write_i(it->element); Write_i(it->element);
if (++it != itEnd) if (++it != itend)
m_ostr << ','; m_ostr << ',';
m_ostr << std::endl; m_ostr << std::endl;
} }
@ -111,8 +111,8 @@ inline void Writer::Write_i(const String& stringElement)
const std::string& s = stringElement.Value(); const std::string& s = stringElement.Value();
std::string::const_iterator it(s.begin()), std::string::const_iterator it(s.begin()),
itEnd(s.end()); itend(s.end());
for (; it != itEnd; ++it) for (; it != itend; ++it)
{ {
switch (*it) switch (*it)
{ {
@ -150,4 +150,4 @@ inline void Writer::Visit(const Null& null) { Write_i(null); }
} // End namespace } // end namespace

View file

@ -229,8 +229,8 @@ struct CommandMenuBar : public wxMenuBar {
/// @param[out] value Output value to write to /// @param[out] value Output value to write to
/// @return Was the requested index found /// @return Was the requested index found
bool read_entry(json::Object const& obj, const char *name, std::string *value) { bool read_entry(json::Object const& obj, const char *name, std::string *value) {
json::Object::const_iterator it = obj.Find(name); json::Object::const_iterator it = obj.find(name);
if (it == obj.End()) return false; if (it == obj.end()) return false;
*value = static_cast<json::String const&>(it->element); *value = static_cast<json::String const&>(it->element);
return true; return true;
} }
@ -241,7 +241,7 @@ typedef json::Object menu_map;
/// Get the root object of the menu configuration /// Get the root object of the menu configuration
menu_map const& get_menus_root() { menu_map const& get_menus_root() {
static menu_map root; static menu_map root;
if (!root.Empty()) return root; if (!root.empty()) return root;
try { try {
root = agi::json_util::file(StandardPaths::DecodePath("?user/menu.json").utf8_str().data(), GET_DEFAULT_CONFIG(default_menu)); root = agi::json_util::file(StandardPaths::DecodePath("?user/menu.json").utf8_str().data(), GET_DEFAULT_CONFIG(default_menu));
@ -263,8 +263,8 @@ menu_map const& get_menus_root() {
menu_items const& get_menu(std::string const& name) { menu_items const& get_menu(std::string const& name) {
menu_map const& root = get_menus_root(); menu_map const& root = get_menus_root();
menu_map::const_iterator it = root.Find(name); menu_map::const_iterator it = root.find(name);
if (it == root.End()) throw menu::UnknownMenu("Menu named " + name + " not found"); if (it == root.end()) throw menu::UnknownMenu("Menu named " + name + " not found");
return it->element; return it->element;
} }
@ -276,7 +276,7 @@ wxMenu *build_menu(std::string const& name, agi::Context *c, CommandManager *cm,
/// @param ele json object to process /// @param ele json object to process
/// @param cm Command manager for this menu /// @param cm Command manager for this menu
void process_menu_item(wxMenu *parent, agi::Context *c, json::Object const& ele, CommandManager *cm) { void process_menu_item(wxMenu *parent, agi::Context *c, json::Object const& ele, CommandManager *cm) {
if (ele.Empty()) { if (ele.empty()) {
parent->AppendSeparator(); parent->AppendSeparator();
return; return;
} }
@ -333,7 +333,7 @@ wxMenu *build_menu(std::string const& name, agi::Context *c, CommandManager *cm,
menu_items const& items = get_menu(name); menu_items const& items = get_menu(name);
if (!menu) menu = new wxMenu; if (!menu) menu = new wxMenu;
for_each(items.Begin(), items.End(), bind(process_menu_item, menu, c, _1, cm)); for_each(items.begin(), items.end(), bind(process_menu_item, menu, c, _1, cm));
return menu; return menu;
} }
@ -389,7 +389,7 @@ namespace menu {
menu_items const& items = get_menu(name); menu_items const& items = get_menu(name);
CommandMenuBar *menu = new CommandMenuBar(c); CommandMenuBar *menu = new CommandMenuBar(c);
for (menu_items::const_iterator it = items.Begin(); it != items.End(); ++it) { for (menu_items::const_iterator it = items.begin(); it != items.end(); ++it) {
std::string submenu, disp; std::string submenu, disp;
read_entry(*it, "submenu", &submenu); read_entry(*it, "submenu", &submenu);
read_entry(*it, "text", &disp); read_entry(*it, "text", &disp);

View file

@ -42,7 +42,7 @@
namespace { namespace {
json::Object const& get_root() { json::Object const& get_root() {
static json::Object root; static json::Object root;
if (root.Empty()) { if (root.empty()) {
root = agi::json_util::parse(new std::istringstream(GET_DEFAULT_CONFIG(default_toolbar))); root = agi::json_util::parse(new std::istringstream(GET_DEFAULT_CONFIG(default_toolbar)));
} }
return root; return root;
@ -92,8 +92,8 @@ namespace {
/// Populate the toolbar with buttons /// Populate the toolbar with buttons
void Populate() { void Populate() {
json::Object const& root = get_root(); json::Object const& root = get_root();
json::Object::const_iterator it = root.Find(name); json::Object::const_iterator it = root.find(name);
if (it == root.End()) { if (it == root.end()) {
// Toolbar names are all hardcoded so this should never happen // Toolbar names are all hardcoded so this should never happen
throw agi::InternalError("Toolbar named " + name + " not found.", 0); throw agi::InternalError("Toolbar named " + name + " not found.", 0);
} }
@ -101,10 +101,10 @@ namespace {
int icon_size = OPT_GET("App/Toolbar Icon Size")->GetInt(); int icon_size = OPT_GET("App/Toolbar Icon Size")->GetInt();
json::Array arr = it->element; json::Array arr = it->element;
commands.reserve(arr.Size()); commands.reserve(arr.size());
bool needs_onidle = false; bool needs_onidle = false;
for (json::Array::const_iterator it = arr.Begin(); it != arr.End(); ++it) { for (json::Array::const_iterator it = arr.begin(); it != arr.end(); ++it) {
json::String const& command_name = *it; json::String const& command_name = *it;
if (command_name.Value().empty()) { if (command_name.Value().empty()) {