~mardy/unity-scopes-api/clientid-1554040

« back to all changes in this revision

Viewing changes to src/scopes/internal/CategoryRegistry.cpp

  • Committer: Michi Henning
  • Date: 2015-02-25 05:20:57 UTC
  • mfrom: (163.386.1 devel)
  • mto: (163.386.2 devel)
  • mto: This revision was merged to the branch mainline in revision 324.
  • Revision ID: michi.henning@canonical.com-20150225052057-9p21sge1myzaowcb
Merged devel and resolved conflicts.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 
19
19
#include <unity/scopes/internal/CategoryRegistry.h>
20
20
#include <unity/UnityExceptions.h>
 
21
 
 
22
#include <algorithm>
21
23
#include <sstream>
22
24
 
23
25
namespace unity
32
34
void CategoryRegistry::register_category(Category::SCPtr category)
33
35
{
34
36
    std::lock_guard<decltype(mutex_)> lock(mutex_);
35
 
    if (categories_.find(category->id()) != categories_.end())
 
37
    auto id = category->id();
 
38
    auto it = find_if(categories_.begin(), categories_.end(), [id](const CatPair& p) { return p.first == id; });
 
39
    if (it != categories_.end())
36
40
    {
37
 
        std::ostringstream s;
38
 
        s << "Category " << category->id() << " already defined";
39
 
        throw InvalidArgumentException(s.str());
 
41
        throw InvalidArgumentException("register_category(): duplicate category: " + id);
40
42
    }
41
 
    categories_[category->id()] = category;
 
43
    categories_.push_back(make_pair(category->id(), category));
42
44
}
43
45
 
44
46
Category::SCPtr CategoryRegistry::register_category(VariantMap const& variant_map)
58
60
Category::SCPtr CategoryRegistry::lookup_category(std::string const& id) const
59
61
{
60
62
    std::lock_guard<decltype(mutex_)> lock(mutex_);
61
 
    auto it = categories_.find(id);
 
63
    auto it = find_if(categories_.begin(), categories_.end(), [id](const CatPair& p) { return p.first == id; });
62
64
    if (it != categories_.end())
63
65
    {
64
66
        return it->second;
66
68
    return nullptr;
67
69
}
68
70
 
 
71
VariantArray CategoryRegistry::serialize() const
 
72
{
 
73
    std::lock_guard<decltype(mutex_)> lock(mutex_);
 
74
    VariantArray va;
 
75
    for (auto&& p : categories_)
 
76
    {
 
77
        va.push_back(Variant(p.second->serialize()));
 
78
    }
 
79
    return va;
 
80
}
 
81
 
69
82
} // namespace internal
70
83
 
71
84
} // namespace scopes