Skip to content

Commit

Permalink
refactor: use range-based for loop to enhance readability
Browse files Browse the repository at this point in the history
  • Loading branch information
Integral-Tech committed Nov 2, 2024
1 parent 24ea557 commit 8d5cf53
Show file tree
Hide file tree
Showing 19 changed files with 98 additions and 140 deletions.
15 changes: 4 additions & 11 deletions UI/importers/classic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@ using namespace json11;

static bool source_name_exists(const Json::array &sources, const string &name)
{
for (size_t i = 0; i < sources.size(); i++) {
Json source = sources[i];
for (const auto &source : sources)
if (name == source["name"].string_value())
return true;
}

return false;
}
Expand Down Expand Up @@ -82,8 +80,7 @@ static Json::object translate_source(const Json &in, const Json &sources)
Json::array source_arr = sources.array_items();

if (id == "GlobalSource") {
for (size_t i = 0; i < source_arr.size(); i++) {
Json source = source_arr[i];
for (const auto &source : source_arr) {
if (name == source["name"].string_value()) {
Json::object obj = source.object_items();
obj["preexist"] = true;
Expand Down Expand Up @@ -263,9 +260,7 @@ static void translate_sc(const Json &in, Json &out)
Json::array global = in["globals"].array_items();

if (!in["globals"].is_null()) {
for (size_t i = 0; i < global.size(); i++) {
Json source = global[i];

for (const auto &source : global) {
Json out_source = translate_source(source, out_sources);
out_sources.push_back(out_source);
}
Expand All @@ -274,9 +269,7 @@ static void translate_sc(const Json &in, Json &out)
Json::array scenes = in["scenes"].array_items();
string first_name = "";

for (size_t i = 0; i < scenes.size(); i++) {
Json in_scene = scenes[i];

for (const auto &in_scene : scenes) {
if (first_name.empty())
first_name = in_scene["name"].string_value();

Expand Down
22 changes: 11 additions & 11 deletions UI/importers/importers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ int ImportSCFromProg(const string &path, string &name, const string &program, Js
return IMPORTER_FILE_NOT_FOUND;
}

for (size_t i = 0; i < importers.size(); i++) {
if (program == importers[i]->Prog()) {
return importers[i]->ImportScenes(path, name, res);
for (const auto &importer : importers) {
if (program == importer->Prog()) {
return importer->ImportScenes(path, name, res);
}
}

Expand Down Expand Up @@ -68,9 +68,9 @@ string DetectProgram(const string &path)
return "Null";
}

for (size_t i = 0; i < importers.size(); i++) {
if (importers[i]->Check(path)) {
return importers[i]->Prog();
for (const auto &importer : importers) {
if (importer->Check(path)) {
return importer->Prog();
}
}

Expand All @@ -79,9 +79,9 @@ string DetectProgram(const string &path)

string GetSCName(const string &path, const string &prog)
{
for (size_t i = 0; i < importers.size(); i++) {
if (importers[i]->Prog() == prog) {
return importers[i]->Name(path);
for (const auto &importer : importers) {
if (importer->Prog() == prog) {
return importer->Name(path);
}
}

Expand All @@ -92,8 +92,8 @@ OBSImporterFiles ImportersFindFiles()
{
OBSImporterFiles f;

for (size_t i = 0; i < importers.size(); i++) {
OBSImporterFiles f2 = importers[i]->FindFiles();
for (const auto &importer : importers) {
OBSImporterFiles f2 = importer->FindFiles();
if (f2.size() != 0) {
f.insert(f.end(), f2.begin(), f2.end());
}
Expand Down
39 changes: 13 additions & 26 deletions UI/importers/sl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ static string translate_hotkey(const Json &hotkey, const string &source)

static bool source_name_exists(const Json::array &sources, const string &name)
{
for (size_t i = 0; i < sources.size(); i++) {
Json item = sources[i];
for (const auto &item : sources) {
string source_name = item["name"].string_value();

if (source_name == name)
Expand All @@ -123,8 +122,7 @@ static bool source_name_exists(const Json::array &sources, const string &name)

static string get_source_name_from_id(const Json &root, const Json::array &sources, const string &id)
{
for (size_t i = 0; i < sources.size(); i++) {
Json item = sources[i];
for (const auto &item : sources) {
string source_id = item["sl_id"].string_value();

if (source_id == id)
Expand All @@ -133,8 +131,7 @@ static string get_source_name_from_id(const Json &root, const Json::array &sourc

Json::array scene_arr = root["scenes"]["items"].array_items();

for (size_t i = 0; i < scene_arr.size(); i++) {
Json item = scene_arr[i];
for (const auto &item : scene_arr) {
string source_id = item["id"].string_value();

if (source_id == id) {
Expand All @@ -156,15 +153,13 @@ static string get_source_name_from_id(const Json &root, const Json::array &sourc
static void get_hotkey_bindings(Json::object &out_hotkeys, const Json &in_hotkeys, const string &name)
{
Json::array hot_arr = in_hotkeys.array_items();
for (size_t i = 0; i < hot_arr.size(); i++) {
Json hotkey = hot_arr[i];
for (const auto &hotkey : hot_arr) {
Json::array bindings = hotkey["bindings"].array_items();
Json::array out_hotkey = Json::array{};

string hotkey_name = translate_hotkey(hotkey, name);

for (size_t x = 0; x < bindings.size(); x++) {
Json binding = bindings[x];
for (const auto &binding : bindings) {
Json modifiers = binding["modifiers"];

string key = translate_key(binding["key"].string_value());
Expand All @@ -191,9 +186,7 @@ static void get_scene_items(const Json &root, const Json::array &out_sources, Js
Json::object hotkeys = scene["hotkeys"].object_items();

Json::array out_items = Json::array{};
for (size_t i = 0; i < in.size(); i++) {
Json item = in[i];

for (const auto &item : in) {
Json in_crop = item["crop"];
string id = item["sourceId"].string_value();
string name = get_source_name_from_id(root, out_sources, id);
Expand Down Expand Up @@ -253,9 +246,7 @@ static int attempt_import(const Json &root, const string &name, Json &res)
Json::array out_sources = Json::array{};
Json::array out_transitions = Json::array{};

for (size_t i = 0; i < source_arr.size(); i++) {
Json source = source_arr[i];

for (const auto &source : source_arr) {
Json in_hotkeys = source["hotkeys"];
Json::array hotkey_items = source["hotkeys"]["items"].array_items();
Json in_filters = source["filters"];
Expand All @@ -275,8 +266,8 @@ static int attempt_import(const Json &root, const string &name, Json &res)
get_hotkey_bindings(out_hotkeys, hotkey_items, "");

Json::array out_filters = Json::array{};
for (size_t x = 0; x < filter_items.size(); x++) {
Json::object filter = filter_items[x].object_items();
for (const auto &f : filter_items) {
Json::object filter = f.object_items();
string type = filter["type"].string_value();
filter["id"] = type;

Expand Down Expand Up @@ -310,9 +301,7 @@ static int attempt_import(const Json &root, const string &name, Json &res)

string scene_name = "";

for (size_t i = 0; i < scenes_arr.size(); i++) {
Json scene = scenes_arr[i];

for (const auto &scene : scenes_arr) {
Json in_hotkeys = scene["hotkeys"];
Json::array hotkey_items = in_hotkeys["items"].array_items();
Json in_filters = scene["filters"];
Expand All @@ -326,8 +315,8 @@ static int attempt_import(const Json &root, const string &name, Json &res)
get_hotkey_bindings(out_hotkeys, hotkey_items, "");

Json::array out_filters = Json::array{};
for (size_t x = 0; x < filter_items.size(); x++) {
Json::object filter = filter_items[x].object_items();
for (const auto &f : filter_items) {
Json::object filter = f.object_items();
string type = filter["type"].string_value();
filter["id"] = type;

Expand Down Expand Up @@ -359,9 +348,7 @@ static int attempt_import(const Json &root, const string &name, Json &res)

string transition_name = "";

for (size_t i = 0; i < t_arr.size(); i++) {
Json transition = t_arr[i];

for (const auto &transition : t_arr) {
Json in_settings = transition["settings"];

int duration = transition["duration"].int_value();
Expand Down
6 changes: 3 additions & 3 deletions UI/importers/studio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ void TranslateOSStudio(Json &res)
Json::object out = res.object_items();
Json::array sources = out["sources"].array_items();

for (size_t i = 0; i < sources.size(); i++) {
Json::object source = sources[i].object_items();
for (auto &src : sources) {
Json::object source = src.object_items();
Json::object settings = source["settings"].object_items();

string id = source["id"].string_value();
Expand Down Expand Up @@ -131,7 +131,7 @@ void TranslateOSStudio(Json &res)
}
#endif
source["settings"] = settings;
sources[i] = source;
src = source;
#undef DirectTranslation
#undef ClearTranslation
}
Expand Down
14 changes: 7 additions & 7 deletions UI/importers/xsplit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ static Json::object parse_slideshow(QString &config)

Json::array files_out = Json::array{};

for (size_t i = 0; i < files.size(); i++) {
string file = files[i].string_value();
for (const auto &f : files) {
string file = f.string_value();
files_out.push_back(Json::object{{"value", file}});
}

Expand All @@ -188,8 +188,8 @@ static Json::object parse_slideshow(QString &config)

static bool source_name_exists(const string &name, const Json::array &sources)
{
for (size_t i = 0; i < sources.size(); i++) {
if (sources.at(i)["name"].string_value() == name)
for (const auto &source : sources) {
if (source["name"].string_value() == name)
return true;
}

Expand All @@ -198,9 +198,9 @@ static bool source_name_exists(const string &name, const Json::array &sources)

static Json get_source_with_id(const string &src_id, const Json::array &sources)
{
for (size_t i = 0; i < sources.size(); i++) {
if (sources.at(i)["src_id"].string_value() == src_id)
return sources.at(i);
for (const auto &source : sources) {
if (source["src_id"].string_value() == src_id)
return source;
}

return nullptr;
Expand Down
8 changes: 4 additions & 4 deletions UI/source-tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1143,8 +1143,8 @@ void SourceTree::dropEvent(QDropEvent *event)
/* determine if any base group is selected */

bool hasGroups = false;
for (int i = 0; i < indices.size(); i++) {
obs_sceneitem_t *item = items[indices[i].row()];
for (const auto &index : indices) {
obs_sceneitem_t *item = items[index.row()];
if (obs_sceneitem_is_group(item)) {
hasGroups = true;
break;
Expand Down Expand Up @@ -1180,8 +1180,8 @@ void SourceTree::dropEvent(QDropEvent *event)
/* --------------------------------------- */
/* save undo data */
std::vector<obs_source_t *> sources;
for (int i = 0; i < indices.size(); i++) {
obs_sceneitem_t *item = items[indices[i].row()];
for (const auto &index : indices) {
obs_sceneitem_t *item = items[index.row()];
if (obs_sceneitem_get_scene(item) != scene)
sources.push_back(obs_scene_get_source(obs_sceneitem_get_scene(item)));
}
Expand Down
16 changes: 8 additions & 8 deletions UI/window-basic-adv-audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ OBSBasicAdvAudio::~OBSBasicAdvAudio()
{
OBSBasic *main = reinterpret_cast<OBSBasic *>(parent());

for (size_t i = 0; i < controls.size(); ++i)
delete controls[i];
for (const auto &control : controls)
delete control;

main->SaveProject();
}
Expand Down Expand Up @@ -81,8 +81,8 @@ void OBSBasicAdvAudio::OBSSourceActivated(void *param, calldata_t *calldata)

inline void OBSBasicAdvAudio::AddAudioSource(obs_source_t *source)
{
for (size_t i = 0; i < controls.size(); i++) {
if (controls[i]->GetSource() == source)
for (const auto &control : controls) {
if (control->GetSource() == source)
return;
}
OBSAdvAudioCtrl *control = new OBSAdvAudioCtrl(ui->mainLayout, source);
Expand Down Expand Up @@ -129,8 +129,8 @@ void OBSBasicAdvAudio::on_usePercent_toggled(bool checked)
else
type = VolumeType::dB;

for (size_t i = 0; i < controls.size(); i++)
controls[i]->SetVolumeWidget(type);
for (const auto &control : controls)
control->SetVolumeWidget(type);

config_set_int(App()->GetUserConfig(), "BasicWindow", "AdvAudioVolumeType", (int)type);
}
Expand Down Expand Up @@ -182,7 +182,7 @@ void OBSBasicAdvAudio::SetIconsVisible(bool visible)
QLabel *headerLabel = qobject_cast<QLabel *>(item->widget());
visible ? headerLabel->show() : headerLabel->hide();

for (size_t i = 0; i < controls.size(); i++) {
controls[i]->SetIconVisible(visible);
for (const auto &control : controls) {
control->SetIconVisible(visible);
}
}
3 changes: 1 addition & 2 deletions UI/window-basic-main-dropfiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,7 @@ void OBSBasic::dropEvent(QDropEvent *event)
if (mimeData->hasUrls()) {
QList<QUrl> urls = mimeData->urls();

for (int i = 0; i < urls.size(); i++) {
QUrl url = urls[i];
for (const auto &url : urls) {
QString file = url.toLocalFile();
QFileInfo fileInfo(file);

Expand Down
Loading

0 comments on commit 8d5cf53

Please sign in to comment.