why 3 methods when 2 are better? combine add() and get().
parent
ba264cbf42
commit
e03a99763a
|
@ -4,15 +4,15 @@
|
|||
#include <pEp/status_to_string.hh>
|
||||
|
||||
|
||||
// calls "init" for the given thread
|
||||
PEP_SESSION SessionRegistry::add(std::thread::id tid)
|
||||
// creates a PEP_SESSION if none yet exists for the given thread
|
||||
PEP_SESSION SessionRegistry::get(std::thread::id tid)
|
||||
{
|
||||
Lock L(_mtx);
|
||||
|
||||
if(m.count(tid) > 0)
|
||||
auto q = m.find(tid);
|
||||
if(q != m.end())
|
||||
{
|
||||
std::stringstream ss; ss << tid;
|
||||
throw std::runtime_error("There is already a session for thread " + ss.str() + "!");
|
||||
return q->second;
|
||||
}
|
||||
|
||||
PEP_SESSION session = nullptr;
|
||||
|
@ -36,11 +36,3 @@ void SessionRegistry::remove(std::thread::id tid)
|
|||
m.erase(q);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
PEP_SESSION SessionRegistry::get(std::thread::id tid) const
|
||||
{
|
||||
Lock L(_mtx);
|
||||
auto q = m.find(tid);
|
||||
return (q==m.end()) ? nullptr : q->second;
|
||||
}
|
||||
|
|
|
@ -14,10 +14,9 @@ public:
|
|||
, ise{_ise}
|
||||
{}
|
||||
|
||||
// calls "init" for the given thread
|
||||
PEP_SESSION add(std::thread::id tid = std::this_thread::get_id());
|
||||
// calls "init" for the given thread if no PEP_SESSION exists, yet for the given thread
|
||||
PEP_SESSION get(std::thread::id tid = std::this_thread::get_id());
|
||||
void remove(std::thread::id tid = std::this_thread::get_id());
|
||||
PEP_SESSION get(std::thread::id tid = std::this_thread::get_id()) const;
|
||||
|
||||
private:
|
||||
std::map<std::thread::id, PEP_SESSION> m;
|
||||
|
@ -26,7 +25,7 @@ private:
|
|||
|
||||
typedef std::recursive_mutex Mutex;
|
||||
typedef std::unique_lock<Mutex> Lock;
|
||||
mutable Mutex _mtx;
|
||||
Mutex _mtx;
|
||||
};
|
||||
|
||||
#endif // JSON_ADAPTER_SESSION_REGISTRY_HH
|
||||
|
|
Loading…
Reference in New Issue