Panini 1.4.0
Header-only library for generating C++, written in C++17
FileWriter.hpp
Go to the documentation of this file.
1/*
2 MIT No Attribution
3
4 Copyright 2021-2023 Mr. Hands
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to
8 deal in the Software without restriction, including without limitation the
9 rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 sell copies of the Software, and to permit persons to whom the Software is
11 furnished to do so.
12
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19 DEALINGS IN THE SOFTWARE.
20*/
21
22#pragma once
23
25#include "writers/Writer.hpp"
26
27namespace panini
28{
29
45 : public ConfiguredWriter<FileWriterConfig>
46 {
47
48 public:
54 inline FileWriter(const FileWriterConfig& config = {})
55 : FileWriter(config.targetPath, config)
56 {
57 }
58
69 inline FileWriter(
70 const std::filesystem::path& path,
71 const WriterConfig& config = WriterConfig())
73 {
74 m_config.targetPath = path;
75
76 m_target.open(m_config.targetPath.string(), std::ios::out | std::ios::binary);
77 }
78
82 inline ~FileWriter() override
83 {
84 Commit();
85 }
86
90 inline bool IsChanged() const override
91 {
92 return m_target.is_open();
93 }
94
95 protected:
99 inline void Write(const std::string& chunk) override
100 {
101 if (!m_target.is_open())
102 {
103 return;
104 }
105
106 m_written += chunk;
107 }
108
112 inline bool OnCommit(bool force) override
113 {
114 (void)force;
115
116 m_target.write(m_written.c_str(), m_written.length());
117 m_target.close();
118
119 return true;
120 }
121
122 protected:
123 std::ofstream m_target;
124 std::string m_written;
125
126 };
127
128};
Base class implementation for writers.
Definition: Writer.hpp:185
bool Commit(bool force=false) override
Definition: Writer.hpp:453
FileWriterConfig m_config
Definition: Writer.hpp:468
Writes output to a target file using a file stream.
Definition: FileWriter.hpp:46
bool OnCommit(bool force) override
Definition: FileWriter.hpp:112
std::string m_written
Definition: FileWriter.hpp:124
bool IsChanged() const override
Definition: FileWriter.hpp:90
~FileWriter() override
Definition: FileWriter.hpp:82
void Write(const std::string &chunk) override
Definition: FileWriter.hpp:99
std::ofstream m_target
Definition: FileWriter.hpp:123
FileWriter(const FileWriterConfig &config={})
Definition: FileWriter.hpp:54
FileWriter(const std::filesystem::path &path, const WriterConfig &config=WriterConfig())
Definition: FileWriter.hpp:69
Definition: Braces.hpp:29
Configuration for the FileWriter class.
Definition: FileWriterConfig.hpp:37
std::filesystem::path targetPath
Definition: FileWriterConfig.hpp:41
Global configuration applied to writers.
Definition: WriterConfig.hpp:39