minor code review

This commit is contained in:
Heather Anderson 2020-08-02 14:38:55 -07:00
parent 4f5f46a623
commit 0c7aab1556
2 changed files with 27 additions and 28 deletions

View file

@ -22,19 +22,19 @@
static const QString RESTRICTED_FLAG_PROPERTY = "RestrictFileAccess"; static const QString RESTRICTED_FLAG_PROPERTY = "RestrictFileAccess";
QReadWriteLock ContextAwareProfile::gl_contextMapProtect; QReadWriteLock ContextAwareProfile::_global_contextMapProtect;
ContextAwareProfile::ContextMap ContextAwareProfile::gl_contextMap; ContextAwareProfile::ContextMap ContextAwareProfile::_global_contextMap;
ContextAwareProfile::ContextAwareProfile(QQmlContext* context) : ContextAwareProfileParent(context), _context(context) { ContextAwareProfile::ContextAwareProfile(QQmlContext* context) : ContextAwareProfileParent(context), _context(context) {
assert(context); assert(context);
{ // register our object for future updates { // register our object for future updates
QWriteLocker guard(&gl_contextMapProtect); QWriteLocker guard(&_global_contextMapProtect);
ContextMap::iterator setLookup = gl_contextMap.find(_context); ContextMap::iterator setLookup = _global_contextMap.find(_context);
if (setLookup == gl_contextMap.end()) { if (setLookup == _global_contextMap.end()) {
setLookup = gl_contextMap.insert(_context, ContextAwareProfileSet()); setLookup = _global_contextMap.insert(_context, ContextAwareProfileSet());
} }
assert(setLookup != gl_contextMap.end()); assert(setLookup != _global_contextMap.end());
ContextAwareProfileSet& profileSet = setLookup.value(); ContextAwareProfileSet& profileSet = setLookup.value();
assert(profileSet.find(this) == profileSet.end()); assert(profileSet.find(this) == profileSet.end());
profileSet.insert(this); profileSet.insert(this);
@ -45,15 +45,15 @@ ContextAwareProfile::ContextAwareProfile(QQmlContext* context) : ContextAwarePro
ContextAwareProfile::~ContextAwareProfile() { ContextAwareProfile::~ContextAwareProfile() {
{ // deregister our object { // deregister our object
QWriteLocker guard(&gl_contextMapProtect); QWriteLocker guard(&_global_contextMapProtect);
ContextMap::iterator setLookup = gl_contextMap.find(_context); ContextMap::iterator setLookup = _global_contextMap.find(_context);
assert(setLookup != gl_contextMap.end()); assert(setLookup != _global_contextMap.end());
if (setLookup != gl_contextMap.end()) { if (setLookup != _global_contextMap.end()) {
ContextAwareProfileSet& profileSet = setLookup.value(); ContextAwareProfileSet& profileSet = setLookup.value();
assert(profileSet.find(this) != profileSet.end()); assert(profileSet.find(this) != profileSet.end());
profileSet.remove(this); profileSet.remove(this);
if (profileSet.isEmpty()) { if (profileSet.isEmpty()) {
gl_contextMap.erase(setLookup); _global_contextMap.erase(setLookup);
} }
} }
} }
@ -65,15 +65,13 @@ void ContextAwareProfile::restrictContext(QQmlContext* context, bool restrict) {
context->setContextProperty(RESTRICTED_FLAG_PROPERTY, restrict); context->setContextProperty(RESTRICTED_FLAG_PROPERTY, restrict);
// broadcast the new value to any registered ContextAwareProfile objects // broadcast the new value to any registered ContextAwareProfile objects
{ // deregister our object QReadLocker guard(&_global_contextMapProtect);
QReadLocker guard(&gl_contextMapProtect); ContextMap::const_iterator setLookup = _global_contextMap.find(context);
ContextMap::const_iterator setLookup = gl_contextMap.find(context); if (setLookup != _global_contextMap.end()) {
if (setLookup != gl_contextMap.end()) { const ContextAwareProfileSet& profileSet = setLookup.value();
const ContextAwareProfileSet& profileSet = setLookup.value(); for (ContextAwareProfileSet::const_iterator profileIterator = profileSet.begin();
for (ContextAwareProfileSet::const_iterator profileIterator = profileSet.begin(); profileIterator != profileSet.end(); profileIterator++) {
profileIterator != profileSet.end(); profileIterator++) { (*profileIterator)->onIsRestrictedChanged(restrict);
(*profileIterator)->onIsRestrictedChanged(restrict);
}
} }
} }
} }

View file

@ -12,7 +12,7 @@
#define hifi_ContextAwareProfile_h #define hifi_ContextAwareProfile_h
#include <atomic> #include <atomic>
#include <QtCore/QMap> #include <QtCore/QHash>
#include <QtCore/QReadWriteLock> #include <QtCore/QReadWriteLock>
#include <QtCore/QSet> #include <QtCore/QSet>
#include <QtCore/QSharedPointer> #include <QtCore/QSharedPointer>
@ -50,17 +50,18 @@ protected:
ContextAwareProfile(QQmlContext* parent); ContextAwareProfile(QQmlContext* parent);
~ContextAwareProfile(); ~ContextAwareProfile();
void onIsRestrictedChanged(bool newValue);
private:
typedef QSet<ContextAwareProfile*> ContextAwareProfileSet;
typedef QHash<QQmlContext*, ContextAwareProfileSet> ContextMap;
QQmlContext* _context{ nullptr }; QQmlContext* _context{ nullptr };
std::atomic<bool> _isRestricted{ false }; std::atomic<bool> _isRestricted{ false };
private: static QReadWriteLock _global_contextMapProtect;
typedef QSet<ContextAwareProfile*> ContextAwareProfileSet; static ContextMap _global_contextMap;
typedef QMap<QQmlContext*, ContextAwareProfileSet> ContextMap;
static QReadWriteLock gl_contextMapProtect; void onIsRestrictedChanged(bool newValue);
static ContextMap gl_contextMap;
}; };
#endif // hifi_FileTypeProfile_h #endif // hifi_FileTypeProfile_h