Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 34 additions & 50 deletions tools/io/InputSet.cpp
Original file line number Diff line number Diff line change
@@ -1,78 +1,62 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.

#include "tools/io/InputSet.h"
#include <stdexcept>

namespace openzl::tools::io {

InputSet::Iterator InputSet::begin() const
{
return Iterator{ begin_state() };
}
InputSet::Iterator InputSet::begin() const { return Iterator{begin_state()}; }

InputSet::Iterator InputSet::end() const
{
return Iterator{};
}
InputSet::Iterator InputSet::end() const { return Iterator{}; }

// end()
InputSet::Iterator::Iterator() : state_() {}

// begin()
InputSet::Iterator::Iterator(std::unique_ptr<IteratorState> state)
: state_((state && **state) ? std::move(state)
: std::unique_ptr<IteratorState>{})
{
}
: state_((state && **state) ? std::move(state)
: std::unique_ptr<IteratorState>{}) {}

InputSet::Iterator::Iterator(const Iterator& o)
: state_(o.state_ ? o.state_->copy() : std::unique_ptr<IteratorState>{})
{
}
InputSet::Iterator::Iterator(const Iterator &o)
: state_(o.state_ ? o.state_->copy() : std::unique_ptr<IteratorState>{}) {}

InputSet::Iterator& InputSet::Iterator::operator=(const Iterator& o)
{
state_ = o.state_ ? o.state_->copy() : std::unique_ptr<IteratorState>{};
return *this;
InputSet::Iterator &InputSet::Iterator::operator=(const Iterator &o) {
state_ = o.state_ ? o.state_->copy() : std::unique_ptr<IteratorState>{};
return *this;
}

const std::shared_ptr<Input>& InputSet::Iterator::operator*() const
{
static const std::shared_ptr<Input> null_input{};
if (!state_) {
throw std::runtime_error("Can't deref end InputSet::Iterator.");
}
return **state_;
const std::shared_ptr<Input> &InputSet::Iterator::operator*() const {
static const std::shared_ptr<Input> null_input{};
if (!state_) {
throw std::runtime_error("Can't deref end InputSet::Iterator.");
}
return **state_;
}

InputSet::Iterator& InputSet::Iterator::operator++()
{
if (!state_) {
throw std::runtime_error(
"Can't advance InputSet::Iterator past the end of the InputSet.");
}
++(*state_);
if (!**state_) {
state_.reset();
}
return *this;
InputSet::Iterator &InputSet::Iterator::operator++() {
if (!state_) {
throw std::runtime_error(
"Can't advance InputSet::Iterator past the end of the InputSet.");
}
++(*state_);
if (!**state_) {
state_.reset();
}
return *this;
}

InputSet::Iterator InputSet::Iterator::operator++(int) const
{
Iterator new_it{ *this };
++new_it;
return new_it;
InputSet::Iterator InputSet::Iterator::operator++(int) const {
Iterator new_it{*this};
++new_it;
return new_it;
}

bool InputSet::Iterator::operator==(const Iterator& o) const
{
return (!state_ && !o.state_)
|| (state_ && o.state_ && *state_ == *o.state_);
bool InputSet::Iterator::operator==(const Iterator &o) const {
return (!state_ && !o.state_) || (state_ && o.state_ && *state_ == *o.state_);
}

bool InputSet::Iterator::operator!=(const Iterator& o) const
{
return !(*this == o);
bool InputSet::Iterator::operator!=(const Iterator &o) const {
return !(*this == o);
}

} // namespace openzl::tools::io
27 changes: 15 additions & 12 deletions tools/io/InputSetBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#pragma once

#include <memory>
#include <optional>
#include <stdexcept>
#include <string>
#include <vector>

#include "tools/io/InputSet.h"
Expand All @@ -13,23 +16,23 @@ namespace openzl::tools::io {
* Helper class to build up an input set from a bunch of path arguments.
*/
class InputSetBuilder {
public:
explicit InputSetBuilder(bool recursive, bool verbose = false);
public:
explicit InputSetBuilder(bool recursive, bool verbose = false);

InputSetBuilder& add_path(std::string path) &;
InputSetBuilder&& add_path(std::string path) &&;
InputSetBuilder &add_path(std::string path) &;
InputSetBuilder &&add_path(std::string path) &&;

InputSetBuilder& add_path(std::optional<std::string> path_opt) &;
InputSetBuilder&& add_path(std::optional<std::string> path_opt) &&;
InputSetBuilder &add_path(std::optional<std::string> path_opt) &;
InputSetBuilder &&add_path(std::optional<std::string> path_opt) &&;

std::unique_ptr<InputSet> build() &&;
std::unique_ptr<InputSet> build() &&;

std::unique_ptr<InputSet> build_static() &&;
std::unique_ptr<InputSet> build_static() &&;

private:
const bool recursive_;
const bool verbose_;
private:
const bool recursive_;
const bool verbose_;

std::vector<std::unique_ptr<InputSet>> input_sets_;
std::vector<std::unique_ptr<InputSet>> input_sets_;
};
} // namespace openzl::tools::io
124 changes: 58 additions & 66 deletions tools/io/InputSetDir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,93 +3,85 @@
#include "tools/io/InputSetDir.h"

#include <filesystem>
#include <stdexcept>

#include "tools/io/InputFile.h"

namespace openzl::tools::io {

template <typename IterT>
class InputSetDir::IteratorStateDir : public InputSet::IteratorState {
public:
IteratorStateDir(const InputSetDir& isd, const std::string& path)
: isd_(isd), it_(path)
{
advance_to_next_regular_file();
}
public:
IteratorStateDir(const InputSetDir &isd, const std::string &path)
: isd_(isd), it_(path) {
advance_to_next_regular_file();
}

std::unique_ptr<IteratorState> copy() const override
{
return std::make_unique<IteratorStateDir>(*this);
}
std::unique_ptr<IteratorState> copy() const override {
return std::make_unique<IteratorStateDir>(*this);
}

// Advance to the next regular file.
IteratorState& operator++() override
{
input_.reset();
if (it_ == IterT{}) {
throw std::runtime_error(
"Can't advance iterator past the end of the InputSet.");
}
++it_;
advance_to_next_regular_file();
return *this;
// Advance to the next regular file.
IteratorState &operator++() override {
input_.reset();
if (it_ == IterT{}) {
throw std::runtime_error(
"Can't advance iterator past the end of the InputSet.");
}
++it_;
advance_to_next_regular_file();
return *this;
}

const std::shared_ptr<Input>& operator*() const override
{
if (input_) {
return input_;
}
if (it_ == IterT{}) {
return input_;
}
input_ = std::make_shared<InputFile>(it_->path().string());
return input_;
const std::shared_ptr<Input> &operator*() const override {
if (input_) {
return input_;
}
if (it_ == IterT{}) {
return input_;
}
input_ = std::make_shared<InputFile>(it_->path().string());
return input_;
}

bool operator==(const IteratorState& o) const override
{
auto ptr = dynamic_cast<const IteratorStateDir*>(&o);
if (ptr == nullptr) {
return false;
}
return (&isd_ == &ptr->isd_) && (it_ == ptr->it_);
bool operator==(const IteratorState &o) const override {
auto ptr = dynamic_cast<const IteratorStateDir *>(&o);
if (ptr == nullptr) {
return false;
}
return (&isd_ == &ptr->isd_) && (it_ == ptr->it_);
}

private:
void advance_to_next_regular_file()
{
while (true) {
if (it_ == IterT{}) {
break;
}
if (it_->is_regular_file()) {
break;
}
++it_;
}
private:
void advance_to_next_regular_file() {
while (true) {
if (it_ == IterT{}) {
break;
}
if (it_->is_regular_file()) {
break;
}
++it_;
}
}

const InputSetDir& isd_;
IterT it_;
mutable std::shared_ptr<Input> input_;
const InputSetDir &isd_;
IterT it_;
mutable std::shared_ptr<Input> input_;
};

InputSetDir::InputSetDir(std::string path, bool recursive)
: path_(std::move(path)), recursive_(recursive)
{
}
: path_(std::move(path)), recursive_(recursive) {}

std::unique_ptr<InputSet::IteratorState> InputSetDir::begin_state() const
{
if (recursive_) {
return std::make_unique<IteratorStateDir<
std::filesystem::recursive_directory_iterator>>(*this, path_);
} else {
return std::make_unique<
IteratorStateDir<std::filesystem::directory_iterator>>(
*this, path_);
}
std::unique_ptr<InputSet::IteratorState> InputSetDir::begin_state() const {
if (recursive_) {
return std::make_unique<
IteratorStateDir<std::filesystem::recursive_directory_iterator>>(*this,
path_);
} else {
return std::make_unique<
IteratorStateDir<std::filesystem::directory_iterator>>(*this, path_);
}
}

} // namespace openzl::tools::io
Loading
Loading