}
}
-std::vector<std::unique_ptr<Zone>>::iterator find(std::vector<std::unique_ptr<Zone>>& zones,
- const std::string& id)
-{
- auto equalId = [&id](const std::unique_ptr<Zone>& zone) {
- return zone->getId() == id;
- };
- return std::find_if(zones.begin(), zones.end(), equalId);
-}
-
Zone& get(std::vector<std::unique_ptr<Zone>>::iterator iter)
{
return **iter;
}
-Zone& at(std::vector<std::unique_ptr<Zone>>& zones, const std::string& id)
-{
- auto iter = find(zones, id);
- if (iter == zones.end()) {
- throw std::out_of_range("id not found");
- }
- return get(iter);
-}
-
bool zoneIsRunning(const std::unique_ptr<Zone>& zone) {
return zone->isRunning();
}
LOGD("ZonesManager object destroyed");
}
+ZonesManager::Zones::iterator ZonesManager::findZone(const std::string& id)
+{
+ return std::find_if(mZones.begin(), mZones.end(), [&id](const std::unique_ptr<Zone>& zone) {
+ return zone->getId() == id;
+ });
+}
+
+Zone& ZonesManager::getZone(const std::string& id)
+{
+ auto iter = findZone(id);
+ if (iter == mZones.end()) {
+ throw std::out_of_range("id not found");
+ }
+ return get(iter);
+}
+
void ZonesManager::saveDynamicConfig()
{
config::saveToKVStore(mConfig.dbPath, mDynamicConfig, DB_PREFIX);
LOGT("Keep empty defaultId");
return;
}
- if (find(mZones, mDynamicConfig.defaultId) != mZones.end()) {
+ if (findZone(mDynamicConfig.defaultId) != mZones.end()) {
LOGT("Keep " << mDynamicConfig.defaultId << " as defaultId");
return;
}
Lock lock(mMutex);
- if (find(mZones, id) != mZones.end()) {
+ if (findZone(id) != mZones.end()) {
throw ZoneOperationException("Zone already exists");
}
mZones.push_back(std::move(zone));
{
Lock lock(mMutex);
- auto iter = find(mZones, zoneId);
+ auto iter = findZone(zoneId);
if (iter == mZones.end()) {
LOGE("Failed to destroy zone " << zoneId << ": no such zone");
throw ZoneOperationException("No such zone");
void ZonesManager::focus(const std::string& zoneId)
{
Lock lock(mMutex);
- auto iter = find(mZones, zoneId);
+ auto iter = findZone(zoneId);
focusInternal(iter);
}
return;
}
- Zone& zone = get(iter);
- std::string zoneId = zone.getId();
+ Zone& zoneToFocus = get(iter);
+ const std::string idToFocus = zoneToFocus.getId();
- if (zoneId == mActiveZoneId) {
+ if (idToFocus == mActiveZoneId) {
return;
}
- if (!zone.isRunning()) {
- LOGE("Can't focus not running zone " << zoneId);
+ if (!zoneToFocus.isRunning()) {
+ LOGE("Can't focus not running zone " << idToFocus);
assert(false);
return;
}
- LOGI("Focus to: " << zone.getId());
+ LOGI("Focus to: " << idToFocus);
- if (!zone.activateVT()) {
+ if (!zoneToFocus.activateVT()) {
LOGE("Failed to activate zones VT");
return;
}
for (auto& zone : mZones) {
std::string id = zone->getId();
- if (id == zoneId) {
+ if (id == idToFocus) {
LOGD(id << ": being sent to foreground");
zone->goForeground();
} else {
zone->goBackground();
}
}
- mActiveZoneId = zoneId;
+ mActiveZoneId = idToFocus;
}
void ZonesManager::refocus()
// assume mutex is locked
// check if refocus is required
- auto oldIter = find(mZones, mActiveZoneId);
+ auto oldIter = findZone(mActiveZoneId);
if (oldIter != mZones.end() && get(oldIter).isRunning()) {
return;
}
// try to refocus to defaultId
- auto iter = find(mZones, mDynamicConfig.defaultId);
- if (iter != mZones.end() && get(iter).isRunning()) {
- // focus to default
- focusInternal(iter);
- } else {
+ auto iter = findZone(mDynamicConfig.defaultId);
+ if (iter == mZones.end() || !get(iter).isRunning()) {
// focus to any running or to host if not found
- auto iter = std::find_if(mZones.begin(), mZones.end(), zoneIsRunning);
- if (iter == mZones.end()) {
- focusInternal(mZones.end());
- } else {
- focusInternal(iter);
- }
+ iter = std::find_if(mZones.begin(), mZones.end(), zoneIsRunning);
}
+ focusInternal(iter);
}
void ZonesManager::startAll()
{
Lock lock(mMutex);
- auto iter = find(mZones, zoneId);
+ auto iter = findZone(zoneId);
if (iter == mZones.end()) {
LOGE("No such zone id: " << zoneId);
throw ZoneOperationException("No such zone");
{
Lock lock(mMutex);
- auto iter = find(mZones, zoneId);
+ auto iter = findZone(zoneId);
if (iter == mZones.end()) {
LOGE("No such zone id: " << zoneId);
throw ZoneOperationException("No such zone");
if (mActiveZoneId.empty()) {
return mZones.end();
}
- auto iter = find(mZones, mActiveZoneId);
+ auto iter = findZone(mActiveZoneId);
if (!get(iter).isRunning()) {
// Can zone change its state by itself?
// Maybe when it is shut down by itself? TODO check it
ZonesManager::Zones::iterator ZonesManager::getNextToForegroundZoneIterator()
{
// assume mutex is locked
- auto current = find(mZones, mActiveZoneId);
+ auto current = findZone(mActiveZoneId);
if (current == mZones.end()) {
// find any running
return std::find_if(mZones.begin(), mZones.end(), zoneIsRunning);
// get config of currently set zone and switch if switchToDefaultAfterTimeout is true
Lock lock(mMutex);
- auto activeIter = find(mZones, mActiveZoneId);
- auto defaultIter = find(mZones, mDynamicConfig.defaultId);
+ auto activeIter = findZone(mActiveZoneId);
+ auto defaultIter = findZone(mDynamicConfig.defaultId);
if (activeIter != mZones.end() &&
defaultIter != mZones.end() &&
Lock lock(mMutex);
- auto srcIter = find(mZones, srcZoneId);
+ auto srcIter = findZone(srcZoneId);
if (srcIter == mZones.end()) {
LOGE("Source zone '" << srcZoneId << "' not found");
return;
}
Zone& srcZone = get(srcIter);
- auto dstIter = find(mZones, dstZoneId);
+ auto dstIter = findZone(dstZoneId);
if (dstIter == mZones.end()) {
LOGE("Destination zone '" << dstZoneId << "' not found");
result->set(g_variant_new("(s)", api::zone::FILE_MOVE_DESTINATION_NOT_FOUND.c_str()));
Lock lock(mMutex);
- auto targetIter = find(mZones, target);
+ auto targetIter = findZone(target);
if (targetIter == mZones.end()) {
LOGE("Target zone '" << target << "' not found");
result->setError(api::ERROR_INVALID_ID, "Unknown proxy call target");
Lock lock(mMutex);
- auto iter = find(mZones, id);
+ auto iter = findZone(id);
if (iter == mZones.end()) {
LOGE("No zone with id=" << id);
result->setError(api::ERROR_INVALID_ID, "No such zone id");
try {
Lock lock(mMutex);
- const std::string id = at(mZones, zone).declareFile(type, path, flags, mode);
+ const std::string id = getZone(zone).declareFile(type, path, flags, mode);
result->set(g_variant_new("(s)", id.c_str()));
} catch (const std::out_of_range&) {
LOGE("No zone with id=" << zone);
try {
Lock lock(mMutex);
- const std::string id = at(mZones, zone).declareMount(source, target, type, flags, data);
+ const std::string id = getZone(zone).declareMount(source, target, type, flags, data);
result->set(g_variant_new("(s)", id.c_str()));
} catch (const std::out_of_range&) {
LOGE("No zone with id=" << zone);
try {
Lock lock(mMutex);
- const std::string id = at(mZones, zone).declareLink(source, target);
+ const std::string id = getZone(zone).declareLink(source, target);
result->set(g_variant_new("(s)", id.c_str()));
} catch (const std::out_of_range&) {
LOGE("No zone with id=" << zone);
try {
Lock lock(mMutex);
- std::vector<std::string> declarations = at(mZones, zone).getDeclarations();
+ std::vector<std::string> declarations = getZone(zone).getDeclarations();
std::vector<GVariant*> out;
for (auto declaration : declarations) {
try {
Lock lock(mMutex);
- at(mZones, zone).removeDeclaration(declarationId);
+ getZone(zone).removeDeclaration(declarationId);
result->setVoid();
} catch (const std::out_of_range&) {
Lock lock(mMutex);
- auto iter = find(mZones, id);
+ auto iter = findZone(id);
if (iter == mZones.end()){
LOGE("No zone with id=" << id );
result->setError(api::ERROR_INVALID_ID, "No such zone id");
namespace fs = boost::filesystem;
// check if zone does not exist
- if (find(mZones, id) != mZones.end()) {
+ if (findZone(id) != mZones.end()) {
LOGE("Cannot create " << id << " zone - already exists!");
result->setError(api::ERROR_INVALID_ID, "Already exists");
return;
LOGT("Shutdown zone " << id);
Lock lock(mMutex);
- auto iter = find(mZones, id);
+ auto iter = findZone(id);
if (iter == mZones.end()) {
LOGE("Failed to shutdown zone - no such zone id: " << id);
result->setError(api::ERROR_INVALID_ID, "No such zone id");
LOGT("Start zone " << id );
Lock lock(mMutex);
- auto iter = find(mZones, id);
+ auto iter = findZone(id);
if (iter == mZones.end()) {
LOGE("Failed to start zone - no such zone id: " << id);
result->setError(api::ERROR_INVALID_ID, "No such zone id");
Lock lock(mMutex);
- auto iter = find(mZones, id);
+ auto iter = findZone(id);
if (iter == mZones.end()) {
LOGE("Failed to lock zone - no such zone id: " << id);
result->setError(api::ERROR_INVALID_ID, "No such zone id");
Lock lock(mMutex);
- auto iter = find(mZones, id);
+ auto iter = findZone(id);
if (iter == mZones.end()) {
LOGE("Failed to unlock zone - no such zone id: " << id);
result->setError(api::ERROR_INVALID_ID, "No such zone id");
Lock lock(mMutex);
- auto iter = find(mZones, id);
+ auto iter = findZone(id);
if (iter == mZones.end()) {
LOGE("Failed to grant device - no such zone id: " << id);
result->setError(api::ERROR_INVALID_ID, "No such zone id");
Lock lock(mMutex);
- auto iter = find(mZones, id);
+ auto iter = findZone(id);
if (iter == mZones.end()) {
LOGE("Failed to revoke device - no such zone id: " << id);
result->setError(api::ERROR_INVALID_ID, "No such zone id");