~michihenning/storage-framework/increase-timeout

« back to all changes in this revision

Viewing changes to src/qt/client/internal/ItemBase.cpp

  • Committer: Michi Henning
  • Date: 2016-08-10 06:45:41 UTC
  • mfrom: (10.10.15 more-coverage)
  • Revision ID: michi.henning@canonical.com-20160810064541-ty9vzh6dff8jgfpw
Merged lp:~michihenning/storage-framework/more-coverage:
Improved coverage. Better error reporting/handling.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 
19
19
#include <unity/storage/qt/client/internal/ItemBase.h>
20
20
 
 
21
#include <unity/storage/qt/client/Account.h>
21
22
#include <unity/storage/qt/client/Exceptions.h>
 
23
#include <unity/storage/qt/client/Root.h>
22
24
 
23
25
#include <cassert>
24
26
 
46
48
 
47
49
QString ItemBase::native_identity() const
48
50
{
 
51
    throw_if_destroyed("Item::native_identity()");
49
52
    return identity_;
50
53
}
51
54
 
52
55
ItemType ItemBase::type() const
53
56
{
 
57
    throw_if_destroyed("Item::type()");
54
58
    return type_;
55
59
}
56
60
 
57
 
Root* ItemBase::root() const
 
61
shared_ptr<Root> ItemBase::root() const
58
62
{
59
 
    if (auto r = root_.lock())
 
63
    auto root = get_root();
 
64
    if (!root)
60
65
    {
61
 
        return r.get();
 
66
        throw RuntimeDestroyedException("Item::root()");
62
67
    }
63
 
    throw RuntimeDestroyedException("Item::root()");
 
68
    return root;
64
69
}
65
70
 
66
71
void ItemBase::set_root(std::weak_ptr<Root> root)
75
80
    public_instance_ = p;
76
81
}
77
82
 
 
83
shared_ptr<Root> ItemBase::get_root() const noexcept
 
84
{
 
85
    try
 
86
    {
 
87
        auto root = root_.lock();
 
88
        if (root)
 
89
        {
 
90
            root->account();  // Throws if either account or runtime has been destroyed.
 
91
            return root;
 
92
        }
 
93
    }
 
94
    catch (RuntimeDestroyedException const&)
 
95
    {
 
96
    }
 
97
    return nullptr;
 
98
}
 
99
 
 
100
void ItemBase::throw_if_destroyed(QString const& method) const
 
101
{
 
102
    if (deleted_)
 
103
    {
 
104
        QString msg = method + ": \"" + identity_ + "\" was deleted previously";
 
105
        throw DeletedException(msg, identity_);
 
106
    }
 
107
    if (!get_root())
 
108
    {
 
109
        throw RuntimeDestroyedException(method);
 
110
    }
 
111
}
 
112
 
78
113
}  // namespace internal
79
114
}  // namespace client
80
115
}  // namespace qt