Prevent possible c++11 range-loop container detach

Explicit or implicit calls to begin() and end() cause a non-const
container to detach from shared data, ie. to perform a deep-copy to gain
a unique copy of the data.
That can be a expensive although unneeded operation.

In order to assist the developer a copyAsConst function is added.
copyAsConst returns a const copy of the object.

For lvalues just use qAsConst. It's only available on Qt 5.7.0. But we
added also for earlier versions. The developer can always use qAsConst.

Intended uses:

    QString s = ...;
    for (const auto &ch : qAsConst(s))
          process(ch);

    for (const auto &ch : copyAsConst(funcReturningQString()))
        process(ch);
This commit is contained in:
Luís Pereira 2018-02-16 18:31:48 +00:00
parent 98a1c111b9
commit 1a913c502b
7 changed files with 35 additions and 12 deletions

View file

@ -45,6 +45,7 @@
#endif
#include "base/bittorrent/session.h"
#include "base/global.h"
#include "base/net/portforwarder.h"
#include "base/net/proxyconfigurationmanager.h"
#include "base/preferences.h"
@ -200,7 +201,7 @@ void AppController::preferencesAction()
data["bypass_local_auth"] = !pref->isWebUiLocalAuthEnabled();
data["bypass_auth_subnet_whitelist_enabled"] = pref->isWebUiAuthSubnetWhitelistEnabled();
QStringList authSubnetWhitelistStringList;
for (const Utils::Net::Subnet &subnet : pref->getWebUiAuthSubnetWhitelist())
for (const Utils::Net::Subnet &subnet : copyAsConst(pref->getWebUiAuthSubnetWhitelist()))
authSubnetWhitelistStringList << Utils::Net::subnetToString(subnet);
data["bypass_auth_subnet_whitelist"] = authSubnetWhitelistStringList.join("\n");
// Update my dynamic domain name