why 3 methods when 2 are better? combine add() and get().

JSON-143
Roker 2020-05-14 10:22:20 +02:00
parent ba264cbf42
commit e03a99763a
2 changed files with 8 additions and 17 deletions

View File

@ -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;
}

View File

@ -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