Enable C++11 support.
This CL catches the sailing C++11 train by enabling C++11 support in the
gyp file and migrating code to use range-based for loops and braced
initializer lists.
BUG=None
TEST=Tested the following:
1. `FEATURES=test emerge-$BOARD platform2`
2. Run the following tests:
- platform_CrosDisksDBus
- platform_CrosDisksArchive
- platform_CrosDisksFilesystem
- platform_CrosDisksFormat
Change-Id: Ifdb720f27a9c8c6a6e6c59941bb2b4b1664be231
Reviewed-on: https://chromium-review.googlesource.com/196389
Reviewed-by: Paul Stewart <pstew@chromium.org>
Commit-Queue: Ben Chan <benchan@chromium.org>
Tested-by: Ben Chan <benchan@chromium.org>
diff --git a/cros-disks.gyp b/cros-disks.gyp
index 19a1d1f..6465c8b 100644
--- a/cros-disks.gyp
+++ b/cros-disks.gyp
@@ -19,6 +19,9 @@
'libudev',
],
},
+ 'cflags_cc': [
+ '-std=gnu++11',
+ ],
},
'targets': [
{
diff --git a/cros_disks_server.cc b/cros_disks_server.cc
index 690e683..0795333 100644
--- a/cros_disks_server.cc
+++ b/cros_disks_server.cc
@@ -88,9 +88,7 @@
string mount_path;
if (platform_->GetRealPath(path, &source_path)) {
- for (vector<MountManager*>::iterator manager_iter = mount_managers_.begin();
- manager_iter != mount_managers_.end(); ++manager_iter) {
- MountManager* manager = *manager_iter;
+ for (const auto& manager : mount_managers_) {
if (manager->CanMount(source_path)) {
source_type = manager->GetMountSourceType();
error_type =
@@ -110,9 +108,7 @@
const vector<string>& options,
DBus::Error& error) { // NOLINT
MountErrorType error_type = MOUNT_ERROR_INVALID_PATH;
- for (vector<MountManager*>::iterator manager_iter = mount_managers_.begin();
- manager_iter != mount_managers_.end(); ++manager_iter) {
- MountManager* manager = *manager_iter;
+ for (const auto& manager : mount_managers_) {
if (manager->CanUnmount(path)) {
error_type = manager->Unmount(path, options);
break;
@@ -130,9 +126,7 @@
}
void CrosDisksServer::DoUnmountAll() {
- for (vector<MountManager*>::iterator manager_iter = mount_managers_.begin();
- manager_iter != mount_managers_.end(); ++manager_iter) {
- MountManager* manager = *manager_iter;
+ for (const auto& manager : mount_managers_) {
manager->UnmountAll();
}
}
@@ -142,10 +136,9 @@
vector<Disk> disks = disk_manager_->EnumerateDisks();
vector<string> devices;
devices.reserve(disks.size());
- for (vector<Disk>::const_iterator disk_iterator = disks.begin();
- disk_iterator != disks.end(); ++disk_iterator) {
- if (!auto_mountable_only || disk_iterator->is_auto_mountable()) {
- devices.push_back(disk_iterator->native_path());
+ for (const auto& disk : disks) {
+ if (!auto_mountable_only || disk.is_auto_mountable()) {
+ devices.push_back(disk.native_path());
}
}
return devices;
@@ -194,17 +187,13 @@
}
void CrosDisksServer::OnSessionStarted() {
- for (vector<MountManager*>::iterator manager_iter = mount_managers_.begin();
- manager_iter != mount_managers_.end(); ++manager_iter) {
- MountManager* manager = *manager_iter;
+ for (const auto& manager : mount_managers_) {
manager->StartSession();
}
}
void CrosDisksServer::OnSessionStopped() {
- for (vector<MountManager*>::iterator manager_iter = mount_managers_.begin();
- manager_iter != mount_managers_.end(); ++manager_iter) {
- MountManager* manager = *manager_iter;
+ for (const auto& manager : mount_managers_) {
manager->StopSession();
}
}
diff --git a/device_event_moderator.cc b/device_event_moderator.cc
index 9139a21..4f585ff 100644
--- a/device_event_moderator.cc
+++ b/device_event_moderator.cc
@@ -62,14 +62,12 @@
DeviceEventList events;
if (event_source_->GetDeviceEvents(&events)) {
if (is_event_queued_) {
- for (DeviceEventList::const_iterator event_iter = events.begin();
- event_iter != events.end(); ++event_iter) {
- event_queue_.Add(*event_iter);
+ for (const auto& event : events) {
+ event_queue_.Add(event);
}
} else {
- for (DeviceEventList::const_iterator event_iter = events.begin();
- event_iter != events.end(); ++event_iter) {
- event_dispatcher_->DispatchDeviceEvent(*event_iter);
+ for (const auto& event : events) {
+ event_dispatcher_->DispatchDeviceEvent(event);
}
}
}
diff --git a/device_event_moderator_unittest.cc b/device_event_moderator_unittest.cc
index dae8b06..8816524 100644
--- a/device_event_moderator_unittest.cc
+++ b/device_event_moderator_unittest.cc
@@ -34,11 +34,10 @@
DeviceEventModeratorTest()
: moderator_(&event_dispatcher_, &event_source_),
event1_(DeviceEvent::kDeviceAdded, "1"),
- event2_(DeviceEvent::kDeviceAdded, "2") {
- event_list1_.push_back(event1_);
- event_list2_.push_back(event2_);
- event_list3_.push_back(event1_);
- event_list3_.push_back(event2_);
+ event2_(DeviceEvent::kDeviceAdded, "2"),
+ event_list1_({event1_}),
+ event_list2_({event2_}),
+ event_list3_({event1_, event2_}) {
}
protected:
diff --git a/disk_manager.cc b/disk_manager.cc
index d01983f..5b03f33 100644
--- a/disk_manager.cc
+++ b/disk_manager.cc
@@ -76,12 +76,9 @@
// Initialize |disks_detected_| with auto-mountable devices that already
// exist when disk manager starts since there is no udev add event that adds
// these devices to |disks_detected_|.
- vector<Disk> disks = EnumerateDisks();
- for (vector<Disk>::const_iterator disk_iterator = disks.begin();
- disk_iterator != disks.end(); ++disk_iterator) {
- if (disk_iterator->is_auto_mountable()) {
- disks_detected_.insert(
- std::make_pair(disk_iterator->native_path(), set<string>()));
+ for (const auto& disk : EnumerateDisks()) {
+ if (disk.is_auto_mountable()) {
+ disks_detected_.insert(std::make_pair(disk.native_path(), set<string>()));
}
}
@@ -189,9 +186,8 @@
} else if (child_disk_removed) {
if (ContainsKey(disks_detected_, device_path)) {
set<string>& child_disks = disks_detected_[device_path];
- for (set<string>::const_iterator child_iter = child_disks.begin();
- child_iter != child_disks.end(); ++child_iter) {
- events->push_back(DeviceEvent(DeviceEvent::kDiskRemoved, *child_iter));
+ for (const auto& child_disk : child_disks) {
+ events->push_back(DeviceEvent(DeviceEvent::kDiskRemoved, child_disk));
}
}
}
diff --git a/disk_manager_unittest.cc b/disk_manager_unittest.cc
index 2524243..b9ae811 100644
--- a/disk_manager_unittest.cc
+++ b/disk_manager_unittest.cc
@@ -66,12 +66,7 @@
filesystem.set_mounter_type(ExFATMounter::kMounterType);
string target_path = "/media/disk";
-
- vector<string> options;
- options.push_back("rw");
- options.push_back("nodev");
- options.push_back("noexec");
- options.push_back("nosuid");
+ vector<string> options = {"rw", "nodev", "noexec", "nosuid"};
scoped_ptr<Mounter> mounter(manager_.CreateMounter(disk, filesystem,
target_path, options));
@@ -91,12 +86,7 @@
filesystem.set_mounter_type(ExternalMounter::kMounterType);
string target_path = "/media/disk";
-
- vector<string> options;
- options.push_back("rw");
- options.push_back("nodev");
- options.push_back("noexec");
- options.push_back("nosuid");
+ vector<string> options = {"rw", "nodev", "noexec", "nosuid"};
scoped_ptr<Mounter> mounter(manager_.CreateMounter(disk, filesystem,
target_path, options));
@@ -115,12 +105,7 @@
filesystem.set_mounter_type(NTFSMounter::kMounterType);
string target_path = "/media/disk";
-
- vector<string> options;
- options.push_back("rw");
- options.push_back("nodev");
- options.push_back("noexec");
- options.push_back("nosuid");
+ vector<string> options = {"rw", "nodev", "noexec", "nosuid"};
scoped_ptr<Mounter> mounter(manager_.CreateMounter(disk, filesystem,
target_path, options));
@@ -140,12 +125,7 @@
filesystem.AddExtraMountOption("shortname=mixed");
string target_path = "/media/disk";
-
- vector<string> options;
- options.push_back("rw");
- options.push_back("nodev");
- options.push_back("noexec");
- options.push_back("nosuid");
+ vector<string> options = {"rw", "nodev", "noexec", "nosuid"};
scoped_ptr<Mounter> mounter(manager_.CreateMounter(disk, filesystem,
target_path, options));
@@ -295,8 +275,7 @@
TEST_F(DiskManagerTest, DoUnmountDiskWithInvalidUnmountOptions) {
string source_path = "/dev/nonexistent-path";
- vector<string> options;
- options.push_back("invalid-unmount-option");
+ vector<string> options = {"invalid-unmount-option"};
EXPECT_EQ(MOUNT_ERROR_INVALID_UNMOUNT_OPTIONS,
manager_.DoUnmount(source_path, options));
}
diff --git a/mount_info.cc b/mount_info.cc
index 0cd9812..8792d5c 100644
--- a/mount_info.cc
+++ b/mount_info.cc
@@ -65,18 +65,16 @@
vector<string> MountInfo::GetMountPaths(const string& source_path) const {
vector<string> mount_paths;
- for (vector<MountPoint>::const_iterator iter = mount_points_.begin();
- iter != mount_points_.end(); ++iter) {
- if (iter->source_path == source_path)
- mount_paths.push_back(iter->mount_path);
+ for (const auto& mount_point : mount_points_) {
+ if (mount_point.source_path == source_path)
+ mount_paths.push_back(mount_point.mount_path);
}
return mount_paths;
}
bool MountInfo::HasMountPath(const string& mount_path) const {
- for (vector<MountPoint>::const_iterator iter = mount_points_.begin();
- iter != mount_points_.end(); ++iter) {
- if (iter->mount_path == mount_path)
+ for (const auto& mount_point : mount_points_) {
+ if (mount_point.mount_path == mount_path)
return true;
}
return false;
diff --git a/mount_info_unittest.cc b/mount_info_unittest.cc
index ad3708d..f73977b 100644
--- a/mount_info_unittest.cc
+++ b/mount_info_unittest.cc
@@ -70,13 +70,10 @@
TEST_F(MountInfoTest, GetMountPaths) {
EXPECT_TRUE(manager_.RetrieveFromFile(mount_file_));
- vector<string> expected_paths;
- expected_paths.push_back("/var");
- expected_paths.push_back("/home");
+ vector<string> expected_paths = {"/var", "/home"};
EXPECT_TRUE(expected_paths == manager_.GetMountPaths("/dev/sda1"));
- expected_paths.clear();
- expected_paths.push_back("/media/Test 1");
+ expected_paths = {"/media/Test 1"};
EXPECT_TRUE(expected_paths == manager_.GetMountPaths("/dev/sdb1"));
expected_paths.clear();
diff --git a/mount_manager.cc b/mount_manager.cc
index ee79ef1..12072e3 100644
--- a/mount_manager.cc
+++ b/mount_manager.cc
@@ -224,9 +224,8 @@
// Make a copy of the mount path cache before iterating through it
// as Unmount modifies the cache.
MountPathMap mount_paths_copy = mount_paths_;
- for (MountPathMap::const_iterator path_iterator = mount_paths_copy.begin();
- path_iterator != mount_paths_copy.end(); ++path_iterator) {
- if (Unmount(path_iterator->first, options) != MOUNT_ERROR_NONE) {
+ for (const auto& path_pair : mount_paths_copy) {
+ if (Unmount(path_pair.first, options) != MOUNT_ERROR_NONE) {
all_umounted = false;
}
}
@@ -242,10 +241,9 @@
string* source_path) const {
CHECK(source_path) << "Invalid source path argument";
- for (MountPathMap::const_iterator path_iterator = mount_paths_.begin();
- path_iterator != mount_paths_.end(); ++path_iterator) {
- if (path_iterator->second == mount_path) {
- *source_path = path_iterator->first;
+ for (const auto& path_pair : mount_paths_) {
+ if (path_pair.second == mount_path) {
+ *source_path = path_pair.first;
return true;
}
}
@@ -265,9 +263,8 @@
}
bool MountManager::IsMountPathInCache(const string& mount_path) const {
- for (MountPathMap::const_iterator path_iterator = mount_paths_.begin();
- path_iterator != mount_paths_.end(); ++path_iterator) {
- if (path_iterator->second == mount_path)
+ for (const auto& path_pair : mount_paths_) {
+ if (path_pair.second == mount_path)
return true;
}
return false;
@@ -300,11 +297,8 @@
set<string> MountManager::GetReservedMountPaths() const {
set<string> reserved_paths;
- for (ReservedMountPathMap::const_iterator
- path_iterator = reserved_mount_paths_.begin();
- path_iterator != reserved_mount_paths_.end();
- ++path_iterator) {
- reserved_paths.insert(path_iterator->first);
+ for (const auto& path_pair : reserved_mount_paths_) {
+ reserved_paths.insert(path_pair.first);
}
return reserved_paths;
}
@@ -346,9 +340,7 @@
CHECK(unmount_flags) << "Invalid unmount flags argument";
*unmount_flags = 0;
- for (vector<string>::const_iterator option_iterator = options.begin();
- option_iterator != options.end(); ++option_iterator) {
- const string& option = *option_iterator;
+ for (const auto& option : options) {
if (option == kUnmountOptionForce) {
*unmount_flags |= MNT_FORCE;
} else if (option == kUnmountOptionLazy) {
diff --git a/mount_manager_unittest.cc b/mount_manager_unittest.cc
index a62c627..84d9334 100644
--- a/mount_manager_unittest.cc
+++ b/mount_manager_unittest.cc
@@ -1018,11 +1018,8 @@
// Verifies that MountManager::ExtractMountLabelFromOptions() extracts a mount
// label from the given options and returns true.
TEST_F(MountManagerTest, ExtractMountLabelFromOptions) {
- vector<string> options;
+ vector<string> options = {"ro", "mountlabel=My USB Drive", "noexec"};
string mount_label;
- options.push_back("ro");
- options.push_back("mountlabel=My USB Drive");
- options.push_back("noexec");
EXPECT_TRUE(manager_.ExtractMountLabelFromOptions(&options, &mount_label));
EXPECT_THAT(options, ElementsAre("ro", "noexec"));
@@ -1053,12 +1050,9 @@
// Verifies that MountManager::ExtractMountLabelFromOptions() extracts the last
// mount label from the given options with two mount labels.
TEST_F(MountManagerTest, ExtractMountLabelFromOptionsWithTwoMountLabels) {
- vector<string> options;
+ vector<string> options = {"ro", "mountlabel=My USB Drive", "noexec",
+ "mountlabel=Another Label"};
string mount_label;
- options.push_back("ro");
- options.push_back("mountlabel=My USB Drive");
- options.push_back("noexec");
- options.push_back("mountlabel=Another Label");
EXPECT_TRUE(manager_.ExtractMountLabelFromOptions(&options, &mount_label));
EXPECT_THAT(options, ElementsAre("ro", "noexec"));
diff --git a/mount_options.cc b/mount_options.cc
index cb7c1a5..de49367 100644
--- a/mount_options.cc
+++ b/mount_options.cc
@@ -37,10 +37,7 @@
bool option_read_only = false, option_read_write = false;
string option_user_id, option_group_id;
- for (vector<string>::const_iterator option_iterator = options.begin();
- option_iterator != options.end(); ++option_iterator) {
- const string& option = *option_iterator;
-
+ for (const auto& option : options) {
// Skip early if |option| contains a comma.
if (option.find(",") != string::npos) {
LOG(WARNING) << "Ignoring invalid mount option '" << option << "'.";
@@ -124,10 +121,7 @@
vector<string> data;
data.reserve(options_.size());
- for (vector<string>::const_iterator
- option_iterator = options_.begin(); option_iterator != options_.end();
- ++option_iterator) {
- const string& option = *option_iterator;
+ for (const auto& option : options_) {
if (option == kOptionReadOnly) {
flags |= MS_RDONLY;
} else if (option == kOptionReadWrite) {
diff --git a/mount_options_unittest.cc b/mount_options_unittest.cc
index ddb3212..add85dc 100644
--- a/mount_options_unittest.cc
+++ b/mount_options_unittest.cc
@@ -31,26 +31,23 @@
TEST_F(MountOptionsTest, IsReadOnlyOptionSet) {
MountOptions mount_options;
- vector<string> options;
// default construction
EXPECT_TRUE(mount_options.IsReadOnlyOptionSet());
// options: ro
- options.push_back("ro");
+ vector<string> options = {"ro"};
mount_options.Initialize(options, false, "", "");
EXPECT_TRUE(mount_options.IsReadOnlyOptionSet());
// options: rw
- options.clear();
- options.push_back("rw");
+ options = {"rw"};
mount_options.Initialize(options, false, "", "");
EXPECT_FALSE(mount_options.IsReadOnlyOptionSet());
}
TEST_F(MountOptionsTest, SetReadOnlyOption) {
MountOptions mount_options;
- vector<string> options;
string expected_string_default = "ro";
string expected_string_initialize = "ro,nodev,noexec,nosuid";
@@ -59,14 +56,13 @@
EXPECT_EQ(expected_string_default, mount_options.ToString());
// options: ro
- options.push_back("ro");
+ vector<string> options = {"ro"};
mount_options.Initialize(options, false, "", "");
mount_options.SetReadOnlyOption();
EXPECT_EQ(expected_string_initialize, mount_options.ToString());
// options: rw
- options.clear();
- options.push_back("rw");
+ options = {"rw"};
mount_options.Initialize(options, false, "", "");
mount_options.SetReadOnlyOption();
EXPECT_EQ(expected_string_initialize, mount_options.ToString());