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
19 changes: 3 additions & 16 deletions include/flatbuffers/file_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#define FLATBUFFERS_FILE_MANAGER_H_

#include <cstddef>
#include <memory>
#include <set>
#include <string>

Expand Down Expand Up @@ -48,22 +49,8 @@ class FileSaver {
FileSaver& operator=(FileSaver&&) = default;
};

class RealFileSaver final : public FileSaver {
public:
bool SaveFile(const char* name, const char* buf, size_t len,
bool binary) final;
};

class FileNameSaver final : public FileSaver {
public:
bool SaveFile(const char* name, const char* buf, size_t len,
bool binary) final;

void Finish() final;

private:
std::set<std::string> file_names_{};
};
std::unique_ptr<FileSaver> CreateFileSaver();
std::unique_ptr<FileSaver> CreateFileNameCollector();

} // namespace flatbuffers

Expand Down
26 changes: 20 additions & 6 deletions src/file_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,26 @@

namespace flatbuffers {

bool RealFileSaver::SaveFile(const char* name, const char* buf, size_t len,
bool binary) {
std::ofstream ofs(name, binary ? std::ofstream::binary : std::ofstream::out);
if (!ofs.is_open()) return false;
ofs.write(buf, len);
return !ofs.bad();
class RealFileSaver final : public FileSaver {
public:
bool SaveFile(const char* name, const char* buf, size_t len,
bool binary) final {
std::ofstream ofs{name,
binary ? std::ofstream::binary : std::ofstream::out};

if (!ofs.is_open()) {
return false;
}

ofs.write(buf, len);

return !ofs.bad();
}
};

std::unique_ptr<FileSaver> CreateFileSaver() {
// compiler limitations mean we can't use std::make_unique
return std::unique_ptr<FileSaver>{new RealFileSaver()};
}

} // namespace flatbuffers
41 changes: 26 additions & 15 deletions src/file_name_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,35 @@

namespace flatbuffers {

bool FileNameSaver::SaveFile(const char* name, const char* buf, size_t len,
bool binary) {
(void)buf;
(void)len;
(void)binary;
class FileNameSaver final : public FileSaver {
public:
bool SaveFile(const char* name, const char* buf, size_t len,
bool binary) final {
(void)buf;
(void)len;
(void)binary;

std::ignore = file_names_.insert(name);

// we want to simulate always successful save
return true;
}

std::ignore = file_names_.insert(name);
void Finish() final {
for (const auto& file_name : file_names_) {
// Just print the file names to standard output.
// No actual file is created.
std::cout << file_name << "\n";
}
}

// we want to simulate always successful save
return true;
}
private:
std::set<std::string> file_names_{};
};

void FileNameSaver::Finish() {
for (const auto& file_name : file_names_) {
// Just print the file names to standard output.
// No actual file is created.
std::cout << file_name << "\n";
}
std::unique_ptr<FileSaver> CreateFileNameCollector() {
// compiler limitations mean we can't use std::make_unique
return std::unique_ptr<FileSaver>{new FileNameSaver()};
}

} // namespace flatbuffers
4 changes: 2 additions & 2 deletions src/flatc_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,9 @@ int main(int argc, const char* argv[]) {
// this exists here to ensure file_saver outlives the compilation process
std::unique_ptr<flatbuffers::FileSaver> file_saver;
if (options.file_names_only) {
file_saver.reset(new flatbuffers::FileNameSaver{});
file_saver = flatbuffers::CreateFileNameCollector();
} else {
file_saver.reset(new flatbuffers::RealFileSaver{});
file_saver = flatbuffers::CreateFileSaver();
}

options.opts.file_saver = file_saver.get();
Expand Down
Loading