Return a unique_ptr from agi::io::Open

This commit is contained in:
Thomas Goyne 2013-06-07 20:18:00 -07:00
parent cf7d548d17
commit f051e59a61
7 changed files with 31 additions and 19 deletions

View file

@ -30,16 +30,15 @@
namespace agi { namespace agi {
namespace io { namespace io {
std::ifstream* Open(fs::path const& file, bool binary) { std::unique_ptr<std::ifstream> Open(fs::path const& file, bool binary) {
LOG_D("agi/io/open/file") << file; LOG_D("agi/io/open/file") << file;
acs::CheckFileRead(file); acs::CheckFileRead(file);
auto stream = new boost::filesystem::ifstream(file, (binary ? std::ios::binary : std::ios::in)); std::unique_ptr<std::ifstream> stream(
new boost::filesystem::ifstream(file, (binary ? std::ios::binary : std::ios::in)));
if (stream->fail()) { if (stream->fail())
delete stream;
throw IOFatal("Unknown fatal error as occurred"); throw IOFatal("Unknown fatal error as occurred");
}
return stream; return stream;
} }

View file

@ -27,14 +27,13 @@
#include "libaegisub/fs.h" #include "libaegisub/fs.h"
#include "libaegisub/io.h" #include "libaegisub/io.h"
#include "libaegisub/log.h" #include "libaegisub/log.h"
#include "libaegisub/util.h"
namespace agi { namespace agi {
namespace json_util { namespace json_util {
json::UnknownElement parse(std::istream *stream) { json::UnknownElement parse(std::unique_ptr<std::istream> stream) {
try { try {
std::unique_ptr<std::istream> stream_deleter(stream);
json::UnknownElement root; json::UnknownElement root;
json::Reader::Read(root, *stream); json::Reader::Read(root, *stream);
return root; return root;
@ -57,15 +56,15 @@ json::UnknownElement file(agi::fs::path const& file, const std::string &default_
} }
catch (fs::FileNotFound const&) { catch (fs::FileNotFound const&) {
// Not an error // Not an error
return parse(new std::istringstream(default_config)); return parse(util::make_unique<std::istringstream>(default_config));
} }
catch (json::Exception&) { catch (json::Exception&) {
// Already logged in parse // Already logged in parse
return parse(new std::istringstream(default_config)); return parse(util::make_unique<std::istringstream>(default_config));
} }
catch (agi::Exception& e) { catch (agi::Exception& e) {
LOG_E("json/file") << "Unexpected error when reading config file " << file << ": " << e.GetMessage(); LOG_E("json/file") << "Unexpected error when reading config file " << file << ": " << e.GetMessage();
return parse(new std::istringstream(default_config)); return parse(util::make_unique<std::istringstream>(default_config));
} }
} }

View file

@ -198,9 +198,9 @@ Framerate::Framerate(fs::path const& filename)
: denominator(default_denominator) : denominator(default_denominator)
, numerator(0) , numerator(0)
{ {
scoped_ptr<std::ifstream> file(agi::io::Open(filename)); auto file = agi::io::Open(filename);
std::string encoding = agi::charset::Detect(filename); auto encoding = agi::charset::Detect(filename);
std::string line = *line_iterator<std::string>(*file, encoding); auto line = *line_iterator<std::string>(*file, encoding);
if (line == "# timecode format v2") { if (line == "# timecode format v2") {
copy(line_iterator<int>(*file, encoding), line_iterator<int>(), back_inserter(timecodes)); copy(line_iterator<int>(*file, encoding), line_iterator<int>(), back_inserter(timecodes));
SetFromTimecodes(); SetFromTimecodes();
@ -218,7 +218,7 @@ Framerate::Framerate(fs::path const& filename)
void Framerate::Save(fs::path const& filename, int length) const { void Framerate::Save(fs::path const& filename, int length) const {
agi::io::Save file(filename); agi::io::Save file(filename);
std::ofstream &out = file.Get(); auto &out = file.Get();
out << "# timecode format v2\n"; out << "# timecode format v2\n";
boost::copy(timecodes, std::ostream_iterator<int>(out, "\n")); boost::copy(timecodes, std::ostream_iterator<int>(out, "\n"));

View file

@ -21,6 +21,7 @@
#include <boost/filesystem/path.hpp> #include <boost/filesystem/path.hpp>
#include <iosfwd> #include <iosfwd>
#include <memory>
namespace agi { namespace agi {
namespace io { namespace io {
@ -28,7 +29,7 @@ namespace agi {
DEFINE_BASE_EXCEPTION_NOINNER(IOError, Exception) DEFINE_BASE_EXCEPTION_NOINNER(IOError, Exception)
DEFINE_SIMPLE_EXCEPTION_NOINNER(IOFatal, IOError, "io/fatal") DEFINE_SIMPLE_EXCEPTION_NOINNER(IOFatal, IOError, "io/fatal")
std::ifstream* Open(fs::path const& file, bool binary = false); std::unique_ptr<std::ifstream> Open(fs::path const& file, bool binary = false);
class Save { class Save {
std::ofstream *fp; std::ofstream *fp;

View file

@ -20,13 +20,15 @@
#include <libaegisub/cajun/elements.h> #include <libaegisub/cajun/elements.h>
#include <libaegisub/fs_fwd.h> #include <libaegisub/fs_fwd.h>
#include <memory>
namespace agi { namespace agi {
namespace json_util { namespace json_util {
/// Parse a JSON stream. /// Parse a JSON stream.
/// @param stream JSON stream, this is deleted internally. /// @param stream JSON stream to parse
/// @return json::UnknownElement /// @return json::UnknownElement
json::UnknownElement parse(std::istream *stream); json::UnknownElement parse(std::unique_ptr<std::istream> stream);
/// Parse a JSON file. /// Parse a JSON file.
/// @param file Path JSON to file /// @param file Path JSON to file

View file

@ -69,5 +69,15 @@ namespace agi {
/// @param name New name for the thread /// @param name New name for the thread
void SetThreadName(const char *name); void SetThreadName(const char *name);
template<typename T>
std::unique_ptr<T> make_unique() {
return std::unique_ptr<T>(new T);
}
template<typename T, typename A1>
std::unique_ptr<T> make_unique(A1&& a1) {
return std::unique_ptr<T>(new T(std::forward<A1>(a1)));
}
} // namespace util } // namespace util
} // namespace agi } // namespace agi

View file

@ -31,6 +31,7 @@
#include <libaegisub/json.h> #include <libaegisub/json.h>
#include <libaegisub/log.h> #include <libaegisub/log.h>
#include <libaegisub/signal.h> #include <libaegisub/signal.h>
#include <libaegisub/util.h>
#include <boost/algorithm/string/join.hpp> #include <boost/algorithm/string/join.hpp>
#include <sstream> #include <sstream>
@ -43,7 +44,7 @@ namespace {
json::Object const& get_root() { json::Object const& get_root() {
static json::Object root; static json::Object root;
if (root.empty()) if (root.empty())
root = agi::json_util::parse(new std::istringstream(GET_DEFAULT_CONFIG(default_toolbar))); root = agi::json_util::parse(agi::util::make_unique<std::istringstream>(GET_DEFAULT_CONFIG(default_toolbar)));
return root; return root;
} }