Serialization/Schema: deployed range-for and auto

Also removed a bunch of unnecessary typedefs:
* the value_type typedefs weren't since you can do map::value_type (granted, that gives you
  a const version, but they weren't used anywhere anyway)
* iterator typedefs weren't needed since we have range-for now. No need to construct a pair
  of iterators and then use an increment-for loop to iterate over the container.
This commit is contained in:
Charles Dang 2018-03-05 17:06:12 +11:00
parent 596edaac00
commit 8309ba63c1
3 changed files with 48 additions and 75 deletions

View file

@ -216,17 +216,16 @@ void schema_validator::close_tag()
void schema_validator::validate(const config& cfg, const std::string& name, int start_line, const std::string& file)
{
// close previous errors and print them to output.
message_map::iterator cache_it = cache_.top().begin();
for(; cache_it != cache_.top().end(); ++cache_it) {
for(message_list::iterator i = cache_it->second.begin(); i != cache_it->second.end(); ++i) {
print(*i);
for(auto& m : cache_.top()) {
for(auto& list : m.second) {
print(list);
}
}
cache_.pop();
// clear cache
cache_it = cache_.top().find(&cfg);
auto cache_it = cache_.top().find(&cfg);
if(cache_it != cache_.top().end()) {
cache_it->second.clear();
}
@ -234,29 +233,26 @@ void schema_validator::validate(const config& cfg, const std::string& name, int
// Please note that validating unknown tag keys the result will be false
// Checking all elements counters.
if(!stack_.empty() && stack_.top() && config_read_) {
class_tag::all_const_tag_iterators p = stack_.top()->tags();
for(const auto& tag : stack_.top()->tags()) {
int cnt = counter_.top()[tag.first].cnt;
for(class_tag::const_tag_iterator tag = p.first; tag != p.second; ++tag) {
int cnt = counter_.top()[tag->first].cnt;
if(tag->second.get_min() > cnt) {
if(tag.second.get_min() > cnt) {
cache_.top()[&cfg].emplace_back(
MISSING_TAG, file, start_line, tag->second.get_min(), tag->first, "", name);
MISSING_TAG, file, start_line, tag.second.get_min(), tag.first, "", name);
continue;
}
if(tag->second.get_max() < cnt) {
if(tag.second.get_max() < cnt) {
cache_.top()[&cfg].emplace_back(
EXTRA_TAG, file, start_line, tag->second.get_max(), tag->first, "", name);
EXTRA_TAG, file, start_line, tag.second.get_max(), tag.first, "", name);
}
}
// Checking if all mandatory keys are present
class_tag::all_const_key_iterators k = stack_.top()->keys();
for(class_tag::const_key_iterator key = k.first; key != k.second; ++key) {
if(key->second.is_mandatory()) {
if(cfg.get(key->first) == nullptr) {
cache_.top()[&cfg].emplace_back(MISSING_KEY, file, start_line, 0, name, key->first);
for(const auto& key : stack_.top()->keys()) {
if(key.second.is_mandatory()) {
if(cfg.get(key.first) == nullptr) {
cache_.top()[&cfg].emplace_back(MISSING_KEY, file, start_line, 0, name, key.first);
}
}
}
@ -270,7 +266,7 @@ void schema_validator::validate_key(
// checking existing keys
const class_key* key = stack_.top()->find_key(name);
if(key) {
std::map<std::string, boost::regex>::iterator itt = types_.find(key->get_type());
auto itt = types_.find(key->get_type());
if(itt != types_.end()) {
boost::smatch sub;

View file

@ -117,7 +117,7 @@ void class_tag::add_link(const std::string& link)
const class_key* class_tag::find_key(const std::string& name) const
{
key_map::const_iterator it_keys = keys_.find(name);
const auto it_keys = keys_.find(name);
if(it_keys != keys_.end()) {
return &(it_keys->second);
}
@ -127,7 +127,7 @@ const class_key* class_tag::find_key(const std::string& name) const
const std::string* class_tag::find_link(const std::string& name) const
{
link_map::const_iterator it_links = links_.find(name);
const auto it_links = links_.find(name);
if(it_links != links_.end()) {
return &(it_links->second);
}
@ -152,7 +152,7 @@ const class_tag* class_tag::find_tag(const std::string& fullpath, const class_ta
name = fullpath;
}
tag_map::const_iterator it_tags = tags_.find(name);
const auto it_tags = tags_.find(name);
if(it_tags != tags_.end()) {
if(next_path.empty()) {
return &(it_tags->second);
@ -161,7 +161,7 @@ const class_tag* class_tag::find_tag(const std::string& fullpath, const class_ta
}
}
link_map::const_iterator it_links = links_.find(name);
const auto it_links = links_.find(name);
if(it_links != links_.end()) {
return root.find_tag(it_links->second + "/" + next_path, root);
}
@ -171,15 +171,15 @@ const class_tag* class_tag::find_tag(const std::string& fullpath, const class_ta
void class_tag::expand_all(class_tag& root)
{
for(tag_map::iterator i = tags_.begin(); i != tags_.end(); ++i) {
i->second.expand(root);
i->second.expand_all(root);
for(auto& tag : tags_) {
tag.second.expand(root);
tag.second.expand_all(root);
}
}
void class_tag::remove_keys_by_type(const std::string& type)
{
key_iterator i = keys_.begin();
auto i = keys_.begin();
while(i != keys_.end()) {
if(i->second.get_type() == type) {
keys_.erase(i++);
@ -188,8 +188,8 @@ void class_tag::remove_keys_by_type(const std::string& type)
}
}
for(tag_iterator t = tags_.begin(); t != tags_.end(); ++t) {
t->second.remove_keys_by_type(type);
for(auto& tag : tags_) {
tag.second.remove_keys_by_type(type);
}
}
@ -234,21 +234,21 @@ void class_tag::printl(std::ostream& os, int level, int step)
os << s << " super=\"" << super_ << "\"\n";
}
for(tag_map::iterator i = tags_.begin(); i != tags_.end(); ++i) {
i->second.printl(os, level + step, step);
for(auto& tag : tags_) {
tag.second.printl(os, level + step, step);
}
for(link_map::iterator i = links_.begin(); i != links_.end(); ++i) {
for(auto& link : links_) {
os << s << ""
<< "[link]\n"
<< s << ""
<< " name=\"" << i->second << "\"\n"
<< " name=\"" << link.second << "\"\n"
<< s << ""
<< "[/link]\n";
}
for(key_map::iterator i = keys_.begin(); i != keys_.end(); ++i) {
i->second.print(os, level + step);
for(auto& key : keys_) {
key.second.print(os, level + step);
}
os << s << "[/tag]\n";
@ -271,7 +271,7 @@ class_tag* class_tag::find_tag(const std::string& fullpath, class_tag& root)
name = fullpath;
}
tag_map::iterator it_tags = tags_.find(name);
auto it_tags = tags_.find(name);
if(it_tags != tags_.end()) {
if(next_path.empty()) {
return &(it_tags->second);
@ -280,7 +280,7 @@ class_tag* class_tag::find_tag(const std::string& fullpath, class_tag& root)
}
}
link_map::iterator it_links = links_.find(name);
auto it_links = links_.find(name);
if(it_links != links_.end()) {
return root.find_tag(it_links->second + "/" + next_path, root);
}
@ -308,7 +308,7 @@ class_tag& class_tag::operator=(const class_tag& t)
void class_tag::add_tag(const std::string& path, const class_tag& tag, class_tag& root)
{
if(path.empty() || path == "/") {
tag_map::iterator it = tags_.find(tag.name_);
auto it = tags_.find(tag.name_);
if(it == tags_.end()) {
tags_.emplace(tag.name_, tag);
@ -328,12 +328,12 @@ void class_tag::add_tag(const std::string& path, const class_tag& tag, class_tag
std::string name = path.substr(0, pos);
std::string next_path = path.substr(pos + 1, path.length());
link_map::const_iterator it_links = links_.find(name);
auto it_links = links_.find(name);
if(it_links != links_.end()) {
root.add_tag(it_links->second + "/" + next_path, tag, root);
}
tag_map::iterator it_tags = tags_.find(name);
auto it_tags = tags_.find(name);
if(it_tags == tags_.end()) {
class_tag subtag;
subtag.set_name(name);
@ -350,9 +350,9 @@ void class_tag::append_super(const class_tag& tag, const std::string& path)
add_keys(tag.keys_);
add_links(tag.links_);
for(tag_map::const_iterator i = tag.tags_.begin(); i != tag.tags_.end(); ++i) {
links_.erase(i->first);
add_link(path + "/" + i->first);
for(const auto& t : tag.tags_) {
links_.erase(t.first);
add_link(path + "/" + t.first);
}
}

View file

@ -140,32 +140,9 @@ private:
class class_tag
{
public:
typedef std::map<std::string, class_tag> tag_map;
typedef std::pair<std::string, class_tag> tag_map_value;
typedef std::map<std::string, class_key> key_map;
typedef std::pair<std::string, class_key> key_map_value;
typedef std::map<std::string, std::string> link_map;
typedef std::pair<std::string, std::string> link_map_value;
typedef key_map::iterator key_iterator;
typedef std::pair<key_iterator, key_iterator> all_key_iterators;
typedef key_map::const_iterator const_key_iterator;
typedef std::pair<const_key_iterator, const_key_iterator> all_const_key_iterators;
typedef tag_map::iterator tag_iterator;
typedef std::pair<tag_iterator, tag_iterator> all_tag_iterators;
typedef tag_map::const_iterator const_tag_iterator;
typedef std::pair<const_tag_iterator, const_tag_iterator> all_const_tag_iterators;
typedef link_map::iterator link_iterator;
typedef std::pair<link_iterator, link_iterator> all_link_iterators;
typedef link_map::const_iterator const_link_iterator;
typedef std::pair<const_link_iterator, const_link_iterator> all_const_link_iterators;
using tag_map = std::map<std::string, class_tag>;
using key_map = std::map<std::string, class_key>;
using link_map = std::map<std::string, std::string>;
class_tag()
: name_("")
@ -317,19 +294,19 @@ public:
/** Calls the expansion on each child. */
void expand_all(class_tag& root);
all_const_tag_iterators tags() const
const tag_map& tags() const
{
return all_const_tag_iterators(tags_.begin(), tags_.end());
return tags_;
}
all_const_key_iterators keys() const
const key_map& keys() const
{
return all_const_key_iterators(keys_.begin(), keys_.end());
return keys_;
}
all_const_link_iterators links() const
const link_map& links() const
{
return all_const_link_iterators(links_.begin(), links_.end());
return links_;
}
void remove_key_by_name(const std::string& name)