build_info
{
- standard CXX "c++11";
+ standard CXX "c++14";
};
library "mspdatafile"
}
}
-IO::Seekable *BuiltinSource::open(const string &name) const
+unique_ptr<IO::Seekable> BuiltinSource::open(const string &name) const
{
auto i = objects.find(name);
if(i!=objects.end())
- return new IO::Memory(i->second.data, i->second.size);
+ return make_unique<IO::Memory>(i->second.data, i->second.size);
return nullptr;
}
bool is_loadable(const CollectionItemTypeBase &, const std::string &) const override;
NameList get_names(const CollectionItemTypeBase &) const override;
void load(Collection &, const CollectionItemTypeBase &, const std::string &) const override;
- IO::Seekable *open(const std::string &) const override;
+ std::unique_ptr<IO::Seekable> open(const std::string &) const override;
};
} // namespace DataFile
namespace Msp {
namespace DataFile {
-Collection::~Collection()
-{
- for(CollectionItemTypeBase *t: types)
- delete t;
-}
-
void Collection::add_var(const string &name, const CollectionItemTypeBase *type, const Variant &var)
{
insert_unique(items, name, var);
CollectionItemTypeBase *Collection::get_type(const CollectionItemTypeBase &type) const
{
- for(CollectionItemTypeBase *t: types)
+ for(const auto &t: types)
if(t->is_same_type(type))
- return t;
+ return t.get();
return nullptr;
}
CollectionItemTypeBase *Collection::get_type_for_item(const Variant &var) const
{
- for(CollectionItemTypeBase *t: types)
+ for(const auto &t: types)
if(t->check_item_type(var))
- return t;
+ return t.get();
return nullptr;
}
sources.push_back(&s);
}
-IO::Seekable *Collection::open_raw(const string &name) const
+unique_ptr<IO::Seekable> Collection::open_raw(const string &name) const
{
for(const CollectionSource *s: sources)
- if(IO::Seekable *io = s->open(name))
+ if(unique_ptr<IO::Seekable> io = s->open(name))
return io;
return nullptr;
Collection::Loader::Loader(Collection &c):
coll(c)
{
- for(const CollectionItemTypeBase *t: coll.types)
+ for(const auto &t: coll.types)
t->add_to_loader(*this);
}
-CollectionItemTypeBase::~CollectionItemTypeBase()
-{
- for(ExtractorBase *e: extractors)
- delete e;
-}
-
void CollectionItemTypeBase::set_keyword(const string &k)
{
kwd = k;
#ifndef MSP_DATAFILE_COLLECTION_H_
#define MSP_DATAFILE_COLLECTION_H_
+#include <memory>
#include <type_traits>
#include <msp/core/attributes.h>
#include <msp/core/maputils.h>
#include <msp/core/noncopyable.h>
-#include <msp/core/refptr.h>
#include "collectionsource.h"
#include "loader.h"
#include "meta.h"
template<typename T, typename S>
void item(const std::string &n)
{
- RefPtr<T> it = new T;
+ std::unique_ptr<T> it = std::make_unique<T>();
ItemLoader<T> ldr(*it, coll);
load_sub_with(ldr);
- coll.add<S>(n, it.get());
- it.release();
+ coll.add<S>(n, std::move(it));
}
};
private:
using ItemMap = std::map<std::string, Variant>;
- std::vector<CollectionItemTypeBase *> types;
+ std::vector<std::unique_ptr<CollectionItemTypeBase>> types;
ItemMap items;
std::vector<const CollectionSource *> sources;
Collection *fallback = nullptr;
public:
- virtual ~Collection();
+ virtual ~Collection() = default;
/** Adds an object into the collection. The name must not pre-exist. The
collection takes ownership of the object. */
template<typename T>
- void add(const std::string &name, T *item)
+ void add(const std::string &name, std::unique_ptr<T> item)
{
if(!item)
throw std::invalid_argument("Collection::add(item)");
using NCT = typename std::remove_cv<T>::type;
- RefPtr<NCT> ptr(item);
- try
- {
- add_var(name, get_type<NCT>(name), ptr);
- }
- catch(...)
- {
- // Avoid deleting the object
- ptr.release();
- throw;
- }
+ add_var(name, get_type<NCT>(name), std::shared_ptr<T>(std::move(item)));
}
/// Gets a typed object from the collection.
return get_status(name, *type);
ItemMap::const_iterator i = items.find(name);
- return (i!=items.end() && i->second.has_type<RefPtr<T> >());
+ return (i!=items.end() && i->second.has_type<std::shared_ptr<T>>());
}
public:
template<typename T>
const std::string &get_name(T *d) const
{
- using RPNCT = RefPtr<typename std::remove_cv<T>::type>;
+ using PNCT = std::shared_ptr<typename std::remove_cv<T>::type>;
for(const auto &kvp: items)
- if(kvp.second.has_type<RPNCT>())
- if(kvp.second.value<RPNCT>().get()==d)
+ if(kvp.second.has_type<PNCT>())
+ if(kvp.second.value<PNCT>().get()==d)
return kvp.first;
// XXX Need better exception class
/** Opens a raw resource, without interpreting it as object data. Null is
returned if no such file is found. The caller must dispose of the returned
object when done with it. */
- IO::Seekable *open_raw(const std::string &) const;
+ std::unique_ptr<IO::Seekable> open_raw(const std::string &) const;
private:
void gather_names_from_sources(std::list<std::string> &, const CollectionItemTypeBase &) const;
std::string kwd;
std::vector<std::string> suffixes;
- std::vector<ExtractorBase *> extractors;
+ std::vector<std::unique_ptr<ExtractorBase>> extractors;
CollectionItemTypeBase() = default;
-public:
- virtual ~CollectionItemTypeBase();
-protected:
void set_keyword(const std::string &);
void add_suffix(const std::string &);
public:
template<typename T>
bool can_extract() const
{
- for(ExtractorBase *e: extractors)
- if(dynamic_cast<Extractor<T> *>(e))
+ for(const auto &e: extractors)
+ if(dynamic_cast<Extractor<T> *>(e.get()))
return true;
return false;
}
template<typename T>
T *extract(const Variant &var) const
{
- for(ExtractorBase *e: extractors)
- if(Extractor<T> *ex = dynamic_cast<Extractor<T> *>(e))
+ for(const auto &e: extractors)
+ if(Extractor<T> *ex = dynamic_cast<Extractor<T> *>(e.get()))
return &ex->extract(var);
return 0;
}
struct Extractor: CollectionItemTypeBase::Extractor<B>
{
B &extract(const Variant &var) const override
- { return *var.value<RefPtr<T> >(); }
+ { return *var.value<std::shared_ptr<T>>(); }
};
- std::function<T *(const std::string &)> create_func;
+ std::function<std::unique_ptr<T> (const std::string &)> create_func;
std::vector<std::function<void(const std::string &, T &)>> notify_funcs;
public:
template<typename B>
CollectionItemType &base()
{
- extractors.push_back(new Extractor<B>);
+ extractors.push_back(std::make_unique<Extractor<B>>());
return *this;
}
{ return dynamic_cast<const CollectionItemType<T> *>(&other); }
bool check_item_type(const Variant &var) const override
- { return var.has_type<RefPtr<T> >(); }
+ { return var.has_type<std::shared_ptr<T>>(); }
void add_to_loader(Collection::Loader &) const override
{ }
{
if(!create_func)
throw std::runtime_error("no creator");
- T *obj = create_func(name);
+ std::unique_ptr<T> obj = create_func(name);
if(obj)
- coll.add(name, obj);
+ coll.add(name, move(obj));
}
void load_item(Collection &, Parser &, const std::string &) const override
void notify_item(const std::string &name, const Variant &var) const override
{
- RefPtr<T> obj = var.value<RefPtr<T> >();
+ std::shared_ptr<T> obj = var.value<std::shared_ptr<T>>();
for(const auto &n: notify_funcs)
n(name, *obj);
}
void load_item(Collection &coll, Parser &parser, const std::string &name) const override
{
- RefPtr<T> obj = new T;
+ std::unique_ptr<T> obj = std::make_unique<T>();
Collection::ItemLoader<T> ldr(*obj, coll);
ldr.load(parser);
- coll.add(name, obj.get());
- obj.release();
+ coll.add(name, std::move(obj));
}
};
template<typename T>
T &Collection::extract(const Variant &var) const
{
- if(!var.has_type<RefPtr<T> >())
+ if(!var.has_type<std::shared_ptr<T>>())
if(CollectionItemTypeBase *type = get_type_for_item(var))
if(T *item = type->extract<T>(var))
return *item;
- return *var.value<RefPtr<T> >();
+ return *var.value<std::shared_ptr<T>>();
}
template<typename T>
typename CollectionItemTypeChooser<T>::Type &Collection::add_type()
{
- typename CollectionItemTypeChooser<T>::Type *type = new typename CollectionItemTypeChooser<T>::Type;
- types.push_back(type);
- return *type;
+ types.emplace_back(std::make_unique<typename CollectionItemTypeChooser<T>::Type>());
+ return static_cast<typename CollectionItemTypeChooser<T>::Type &>(*types.back());
}
template<typename T>
template<typename T>
CollectionItemTypeBase *Collection::get_type(const std::string &name) const
{
- for(CollectionItemTypeBase *t: types)
- if(dynamic_cast<CollectionItemType<T> *>(t))
- return t;
+ for(const auto &t: types)
+ if(dynamic_cast<CollectionItemType<T> *>(t.get()))
+ return t.get();
CollectionItemTypeBase *type = nullptr;
- for(CollectionItemTypeBase *t: types)
+ for(const auto &t: types)
if(t->can_extract<T>())
{
if(!name.empty() && t->match_name(name))
- return t;
- type = t;
+ return t.get();
+ type = t.get();
}
return type;
}
#define COLLECTIONSOURCE_H_
#include <list>
+#include <memory>
#include <string>
#include <msp/io/seekable.h>
#include "mspdatafile_api.h"
/** Opens a raw resource. The caller is responsible for deleting the
returned object when done with it. */
- virtual IO::Seekable *open(const std::string &) const = 0;
+ virtual std::unique_ptr<IO::Seekable> open(const std::string &) const = 0;
};
} // namespace DataFile
}
}
-IO::Seekable *DirectorySource::open(const string &name) const
+unique_ptr<IO::Seekable> DirectorySource::open(const string &name) const
{
FS::Path file;
if(lookup_file(name, file))
- return new IO::BufferedFile(file.str());
+ return make_unique<IO::BufferedFile>(file.str());
return nullptr;
}
bool is_loadable(const CollectionItemTypeBase &, const std::string &) const override;
NameList get_names(const CollectionItemTypeBase &) const override;
void load(Collection &, const CollectionItemTypeBase &, const std::string &) const override;
- IO::Seekable *open(const std::string &) const override;
+ std::unique_ptr<IO::Seekable> open(const std::string &) const override;
bool lookup_file(const std::string &, FS::Path &) const;
};
#ifndef MSP_DATAFILE_DYNAMICOBJECTLOADER_H_
#define MSP_DATAFILE_DYNAMICOBJECTLOADER_H_
+#include <memory>
#include <msp/core/typeregistry.h>
#include "collection.h"
#include "except.h"
using TypeRegistry = Msp::TypeRegistry<CreateObject, DynamicObjectLoader &>;
Collection *coll = nullptr;
- T *object = nullptr;
+ std::unique_ptr<T> object = nullptr;
private:
- Loader *obj_loader = nullptr;
- void (*store_func)(Collection &, const std::string &, T *) = nullptr;
+ std::unique_ptr<Loader> obj_loader = nullptr;
+ void (*store_func)(Collection &, const std::string &, std::unique_ptr<T>) = nullptr;
static ActionMap shared_actions;
protected:
DynamicObjectLoader(Collection *);
-public:
- ~DynamicObjectLoader() { delete object; delete obj_loader; }
private:
void init_actions() override;
public:
- T *get_object() { T *o = object; object = 0; return o; }
+ std::unique_ptr<T> get_object() { return std::move(object); }
T *store_object(Collection &, const std::string &);
protected:
private:
template<typename U>
- typename std::enable_if<NeedsCollection<typename U::Loader>::value, typename U::Loader *>::type create_object_loader(U &obj) const;
+ typename std::enable_if<NeedsCollection<typename U::Loader>::value, std::unique_ptr<typename U::Loader>>::type create_object_loader(U &obj) const;
template<typename U>
- typename std::enable_if<!NeedsCollection<typename U::Loader>::value, typename U::Loader *>::type create_object_loader(U &obj) const;
+ typename std::enable_if<!NeedsCollection<typename U::Loader>::value, std::unique_ptr<typename U::Loader>>::type create_object_loader(U &obj) const;
protected:
virtual const TypeRegistry &get_type_registry() const = 0;
if(!store_func)
throw std::logic_error("no store function");
- T *o = object;
- store_func(c, name, object);
- object = 0;
+ T *o = object.get();
+ store_func(c, name, std::move(object));
return o;
}
template<typename T, typename C>
template<typename U>
-typename std::enable_if<NeedsCollection<typename U::Loader>::value, typename U::Loader *>::type DynamicObjectLoader<T, C>::create_object_loader(U &obj) const
+typename std::enable_if<NeedsCollection<typename U::Loader>::value, std::unique_ptr<typename U::Loader>>::type DynamicObjectLoader<T, C>::create_object_loader(U &obj) const
{
if(!coll)
throw no_collection(typeid(U));
- return new typename U::Loader(obj, *coll);
+ return std::make_unique<typename U::Loader>(obj, *coll);
}
template<typename T, typename C>
template<typename U>
-typename std::enable_if<!NeedsCollection<typename U::Loader>::value, typename U::Loader *>::type DynamicObjectLoader<T, C>::create_object_loader(U &obj) const
+typename std::enable_if<!NeedsCollection<typename U::Loader>::value, std::unique_ptr<typename U::Loader>>::type DynamicObjectLoader<T, C>::create_object_loader(U &obj) const
{
- return new typename U::Loader(obj);
+ return std::make_unique<typename U::Loader>(obj);
}
template<typename U>
void DynamicObjectLoader<T, C>::CreateObject<U>::operator()(const std::string &, DynamicObjectLoader &ldr) const
{
- U *obj = new U;
- ldr.object = obj;
- ldr.obj_loader = ldr.create_object_loader<U>(*obj);
+ ldr.object = std::make_unique<U>();
+ ldr.obj_loader = ldr.create_object_loader<U>(static_cast<U &>(*ldr.object));
ldr.add_auxiliary_loader(*ldr.obj_loader);
- ldr.store_func = [](Collection &c, const std::string &n, T *o){ c.add(n, static_cast<U *>(o)); };
+ ldr.store_func = [](Collection &c, const std::string &n, std::unique_ptr<T> o){ c.add(n, std::unique_ptr<U>(static_cast<U *>(o.release()))); };
}
} // namespace DataFile
#include <msp/io/zlibcompressed.h>
#include "input.h"
+using namespace std;
+
namespace Msp {
namespace DataFile {
in(&i)
{ }
-Input::~Input()
-{
- delete compressed;
-}
-
void Input::set_decompress()
{
- compressed = new IO::ZlibCompressed(*in, IO::M_READ);
- in = compressed;
+ compressed = make_unique<IO::ZlibCompressed>(*in, IO::M_READ);
+ in = compressed.get();
}
int Input::get()
#ifndef MSP_DATAFILE_INPUT_H_
#define MSP_DATAFILE_INPUT_H_
+#include <memory>
#include <msp/core/noncopyable.h>
#include <msp/io/base.h>
{
private:
IO::Base *in = nullptr;
- IO::Base *compressed = nullptr;
+ std::unique_ptr<IO::Base> compressed;
unsigned line = 1;
int next = -1;
public:
Input(IO::Base &);
- ~Input();
void set_decompress();
int get();
init_actions();
}
-void Loader::add(const string &kwd, LoaderAction *act)
+void Loader::add(const string &kwd, unique_ptr<LoaderAction> act)
{
if(!actions)
actions = &local_actions;
StatementKey key(kwd, act->get_signature());
- ActionMap::iterator i = actions->find(key);
- if(i!=actions->end())
- {
- delete i->second;
- i->second = act;
- }
- else
- (*actions)[key] = act;
+ (*actions)[key] = move(act);
}
void Loader::add_auxiliary_loader(Loader &ldr)
int m = signature_match(key.signature, i->first.signature);
if(m>match)
{
- act = i->second;
+ act = i->second.get();
match = m;
}
}
return cur_st->keyword;
}
-
-Loader::ActionMap::~ActionMap()
-{
- for(const auto &kvp: *this)
- delete kvp.second;
-}
-
} // namespace DataFile
} // namespace Msp
#ifndef MSP_DATAFILE_LOADER_H_
#define MSP_DATAFILE_LOADER_H_
-#include <type_traits>
+#include <memory>
#include <map>
+#include <type_traits>
#include <msp/io/file.h>
#include "loaderaction.h"
#include "meta.h"
class MSPDATAFILE_API Loader: private NonCopyable
{
protected:
- class MSPDATAFILE_API ActionMap: public std::map<StatementKey, LoaderAction *>, private NonCopyable
- {
- public:
- ~ActionMap();
- };
+ using ActionMap = std::map<StatementKey, std::unique_ptr<LoaderAction>>;
private:
ActionMap local_actions;
/** Adds a keyword that is loaded by calling a function. */
template<typename L>
void add(const std::string &k, void (L::*func)())
- { add(k, new LoaderFunc0<L>(func)); }
+ { add(k, std::make_unique<LoaderFunc0<L>>(func)); }
template<typename L, typename A0>
void add(const std::string &k, void (L::*func)(A0))
- { add(k, new LoaderFunc1<L, A0>(func)); }
+ { add(k, std::make_unique<LoaderFunc1<L, A0>>(func)); }
template<typename L, typename... Args>
void add(const std::string &k, void (L::*func)(Args...))
- { add(k, new LoaderFuncN<L, Args...>(func)); }
+ { add(k, std::make_unique<LoaderFuncN<L, Args...>>(func)); }
/** Adds a keyword that is loaded by calling a function with a bound
first argument. */
template<typename L, typename B0, typename... Args>
void add(const std::string &k, void (L::*func)(B0, Args...), const typename std::remove_reference<B0>::type &b0)
- { add(k, new LoaderFuncNBound1<L, B0, Args...>(func, b0)); }
+ { add(k, std::make_unique<LoaderFuncNBound1<L, B0, Args...>>(func, b0)); }
template<typename L, typename B0, typename... Args>
void add(const std::string &k, void (L::*func)(B0, Args...), B0 &&b0)
- { add(k, new LoaderFuncNBound1<L, B0, Args...>(func, std::forward<B0>(b0))); }
+ { add(k, std::make_unique<LoaderFuncNBound1<L, B0, Args...>>(func, std::forward<B0>(b0))); }
/** Adds a keyword that is loaded into a member of the loaded object. */
template<typename L, typename T0>
void add(const std::string &k, T0 L::*p0)
- { add(k, new LoadValue1<L, T0>(p0)); }
+ { add(k, std::make_unique<LoadValue1<L, T0>>(p0)); }
template<typename L, typename T0, typename T1>
void add(const std::string &k, T0 L::*p0, T1 L::*p1)
- { add(k, new LoadValue2<L, T0, T1>(p0, p1)); }
+ { add(k, std::make_unique<LoadValue2<L, T0, T1>>(p0, p1)); }
/** Adds a keyword that is recognized but ignored. */
void add(const std::string &k)
- { add(k, new LoaderDiscard); }
+ { add(k, std::make_unique<LoaderDiscard>()); }
private:
- void add(const std::string &, LoaderAction *);
+ void add(const std::string &, std::unique_ptr<LoaderAction>);
protected:
void add_auxiliary_loader(Loader &);
template<typename T, typename... Args>
void load(T &obj, typename T::Loader::Collection &coll, const std::string &fn, Args &&... args)
{
- RefPtr<IO::Seekable> in = coll.open_raw(fn);
+ std::unique_ptr<IO::Seekable> in = coll.open_raw(fn);
if(!in)
throw IO::file_not_found(fn);
template<typename T, typename C, typename... Args>
typename std::enable_if<!NeedsCollection<typename T::Loader>::value>::type load(T &obj, C &coll, const std::string &fn, Args &&... args)
{
- RefPtr<IO::Seekable> in = coll.open_raw(fn);
+ std::unique_ptr<IO::Seekable> in = coll.open_raw(fn);
if(!in)
throw IO::file_not_found(fn);
out(&o)
{ }
-Output::~Output()
-{
- delete compressed;
-}
-
void Output::set_compressed()
{
- compressed = new IO::ZlibCompressed(*out, IO::M_WRITE);
- out = compressed;
+ compressed = make_unique<IO::ZlibCompressed>(*out, IO::M_WRITE);
+ out = compressed.get();
}
size_t Output::put(char c)
#ifndef MSP_DATAFILE_OUTPUT_H_
#define MSP_DATAFILE_OUTPUT_H_
+#include <memory>
#include <msp/core/noncopyable.h>
#include <msp/io/base.h>
{
private:
IO::Base *out = nullptr;
- IO::Base *compressed = nullptr;
+ std::unique_ptr<IO::Base> compressed;
public:
Output(IO::Base &);
- ~Output();
void set_compressed();
const File &file = i->second->get_file();
- RefPtr<IO::Base> in = file.open();
+ unique_ptr<IO::Base> in = file.open();
Parser parser(*in, file.get_full_name());
if(file.is_collection())
{
type.load_item(coll, parser, name);
}
-IO::Seekable *PackSource::open(const string &fn) const
+unique_ptr<IO::Seekable> PackSource::open(const string &fn) const
{
auto i = files.find(fn);
if(i!=files.end())
- return i->second->open().release();
+ return i->second->open();
return nullptr;
}
objects.push_back(Object(o, *this));
}
-RefPtr<IO::Seekable> PackSource::File::open() const
+unique_ptr<IO::Seekable> PackSource::File::open() const
{
if(pack.get_io())
// TODO Performance may be poor without buffering
- return new IO::Slice(*pack.get_io(), pack.get_base_offset()+offset, length);
+ return make_unique<IO::Slice>(*pack.get_io(), pack.get_base_offset()+offset, length);
else
{
IO::BufferedFile *io_file = new IO::BufferedFile(pack.get_filename());
- IO::Slice *io_slice = new IO::Slice(*io_file, pack.get_base_offset()+offset, length);
+ unique_ptr<IO::Slice> io_slice = make_unique<IO::Slice>(*io_file, pack.get_base_offset()+offset, length);
io_slice->signal_deleted.connect(sigc::bind(sigc::ptr_fun(delete_io), io_file));
return io_slice;
}
#ifndef MSP_DATAFILE_PACKSOURCE_H_
#define MSP_DATAFILE_PACKSOURCE_H_
+#include <memory>
#include <msp/core/noncopyable.h>
-#include <msp/core/refptr.h>
#include "collectionsource.h"
#include "mspdatafile_api.h"
#include "objectloader.h"
File(const Pack &, const std::string &);
File(const File &, const Pack &);
- RefPtr<IO::Seekable> open() const;
+ std::unique_ptr<IO::Seekable> open() const;
const std::string &get_filename() const { return filename; }
FileInfo get_info() const;
std::string get_full_name() const;
bool is_loadable(const CollectionItemTypeBase &, const std::string &) const override;
NameList get_names(const CollectionItemTypeBase &) const override;
void load(Collection &, const CollectionItemTypeBase &, const std::string &) const override;
- IO::Seekable *open(const std::string &) const override;
+ std::unique_ptr<IO::Seekable> open(const std::string &) const override;
};
} // namespace DataFile
{
char c = in.peek();
if(c=='{' || c=='[')
- mode = new JsonParser(in, src);
+ mode = make_unique<JsonParser>(in, src);
else
- mode = new TextParser(in, src);
+ mode = make_unique<TextParser>(in, src);
}
-Parser::~Parser()
-{
- delete mode;
-}
+Parser::~Parser() = default;
Statement Parser::parse(bool raw)
{
{
if(st.keyword=="__bin")
{
- delete mode;
- mode = new BinaryParser(in, src);
+ mode = make_unique<BinaryParser>(in, src);
while(in.peek()=='\n')
in.get();
}
else if(st.keyword=="__text")
- {
- delete mode;
- mode = new TextParser(in, src);
- }
+ mode = make_unique<TextParser>(in, src);
else if(st.keyword=="__z")
in.set_decompress();
else if(st.keyword=="__src")
#ifndef MSP_DATAFILE_PARSER_H_
#define MSP_DATAFILE_PARSER_H_
+#include <memory>
#include <string>
#include <msp/core/noncopyable.h>
#include "input.h"
std::string main_src;
std::string src;
bool good = true;
- ParserMode *mode = nullptr;
+ std::unique_ptr<ParserMode> mode;
public:
Parser(IO::Base &i, const std::string &s);
RawData::~RawData()
{
delete[] owned_data;
- delete compressed;
- if(in_owned)
- delete in;
}
bool RawData::detect_signature(const std::string &sig)
if(in)
throw logic_error("input already exists");
- RefPtr<IO::Base> opened = coll.open_raw(fn);
+ unique_ptr<IO::Base> opened = coll.open_raw(fn);
if(!opened)
throw IO::file_not_found(fn);
open_io(*opened, fn);
- opened.release();
- in_owned = true;
+ owned_in = move(opened);
}
void RawData::open_io(IO::Base &i, const string &fn)
src_name = fn;
in = &i;
if(flags&COMPRESSED)
- compressed = new IO::ZlibCompressed(*in, IO::M_READ);
+ compressed = make_unique<IO::ZlibCompressed>(*in, IO::M_READ);
}
void RawData::load()
data = static_cast<char *>(buffer);
- IO::Base *src = (compressed ? compressed : in);
+ IO::Base *src = (compressed ? compressed.get() : in);
size_t pos = 0;
while(pos<size)
{
pos += len;
}
- if(in_owned)
- delete in;
+ owned_in.reset();
in = nullptr;
}
#ifndef MSP_DATAFILE_RAWDATA_H_
#define MSP_DATAFILE_RAWDATA_H_
+#include <memory>
#include <string>
#include <msp/core/noncopyable.h>
#include <msp/io/base.h>
};
std::string src_name;
+ std::unique_ptr<IO::Base> owned_in;
IO::Base *in = nullptr;
- bool in_owned = false;
- IO::Base *compressed = nullptr;
+ std::unique_ptr<IO::Base> compressed;
std::size_t size = 0;
char *data = nullptr;
char *owned_data = nullptr;
Writer::Writer(IO::Base &o):
out(o),
- mode(new TextWriter(out))
+ mode(make_unique<TextWriter>(out))
{ }
-Writer::~Writer()
-{
- delete mode;
-}
+Writer::~Writer() = default;
void Writer::write(const Statement &st)
{
mode->write(st);
- delete mode;
if(binary)
- mode = new BinaryWriter(out);
+ mode = make_unique<BinaryWriter>(out);
else
- mode = new TextWriter(out);
+ mode = make_unique<TextWriter>(out);
}
void Writer::set_compressed()
#define MSP_DATAFILE_WRITER_H_
#include <map>
+#include <memory>
#include <msp/core/noncopyable.h>
#include <msp/io/base.h>
#include "mspdatafile_api.h"
{
private:
Output out;
- WriterMode *mode = nullptr;
+ std::unique_ptr<WriterMode> mode;
bool binary = false;
public:
tmp_file(tempfile(tmp_path))
{ }
-IO::BufferedFile *Packer::tempfile(FS::Path &out_fn)
+unique_ptr<IO::BufferedFile> Packer::tempfile(FS::Path &out_fn)
{
FS::Path tmpdir = FS::get_temp_dir();
try
{
FS::Path filename = tmpdir/format("mspdatatool.%d", i);
- IO::BufferedFile *file = new IO::BufferedFile(filename.str(), IO::M_RDWR, IO::File::C_NEW);
+ unique_ptr<IO::BufferedFile> file = make_unique<IO::BufferedFile>(filename.str(), IO::M_RDWR, IO::File::C_NEW);
out_fn = filename;
return file;
}
Packer::~Packer()
{
- delete tmp_file;
+ tmp_file.reset();
FS::unlink(tmp_path);
}
{
DataFile::Parser parser(in, fn);
- DataFile::Writer *writer = tool.create_writer(out);
+ unique_ptr<DataFile::Writer> writer = tool.create_writer(out);
bool collection = FS::extpart(fn)==".mdc";
while(parser)
}
}
writer->write(DataFile::Statement("__end"));
- delete writer;
}
void Packer::transfer_raw_data(IO::Base &in, const string &fn, IO::Base &out)
{
mem.seek(0, IO::S_BEG);
- DataFile::Writer *writer = tool.create_writer(mem);
+ unique_ptr<DataFile::Writer> writer = tool.create_writer(mem);
for(list<DataFile::Statement>::const_iterator i=directory.begin(); i!=directory.end(); ++i)
writer->write(*i);
base_offset = mem.tell();
writer->write((DataFile::Statement("base_offset"), base_offset));
writer->write(DataFile::Statement("__end"));
- delete writer;
+ writer.reset();
unsigned dir_size = mem.tell();
if(dir_size<=base_offset)
#ifndef PACKER_H_
#define PACKER_H_
+#include <memory>
#include <string>
#include <msp/datafile/statement.h>
#include <msp/io/buffered.h>
DataTool &tool;
Msp::FS::Path tmp_path;
- Msp::IO::BufferedFile *tmp_file = nullptr;
+ std::unique_ptr<Msp::IO::BufferedFile> tmp_file;
std::list<Msp::DataFile::Statement> directory;
unsigned dir_alloc = 0;
public:
Packer(DataTool &);
private:
- static Msp::IO::BufferedFile *tempfile(Msp::FS::Path &);
+ static std::unique_ptr<Msp::IO::BufferedFile> tempfile(Msp::FS::Path &);
public:
~Packer();
void DataTool::do_transfer()
{
- IO::Base *out = open_output(out_fn);
- DataFile::Writer *writer = create_writer(*out);
+ unique_ptr<IO::Base> out = open_output(out_fn);
+ unique_ptr<DataFile::Writer> writer = create_writer(*out);
for(list<string>::const_iterator i=in_fns.begin(); i!=in_fns.end(); ++i)
{
- IO::Base *in = open_input(*i);
+ unique_ptr<IO::Base> in = open_input(*i);
DataFile::Parser parser(*in, *i);
while(parser)
if(st.valid && (!st.control || st.keyword=="__src" || debug))
writer->write(st);
}
-
- delete in;
}
-
- delete writer;
- delete out;
}
void DataTool::do_compile()
{
- IO::Base *out = open_output(out_fn);
- DataFile::Writer *writer = create_writer(*out);
+ unique_ptr<IO::Base> out = open_output(out_fn);
+ unique_ptr<DataFile::Writer> writer = create_writer(*out);
Compiler compiler(*writer);
for(list<string>::const_iterator i=in_fns.begin(); i!=in_fns.end(); ++i)
{
- IO::Base *in = open_input(*i);
+ unique_ptr<IO::Base> in = open_input(*i);
DataFile::Parser parser(*in, *i);
compiler.load(parser);
- delete in;
}
-
- delete writer;
- delete out;
}
void DataTool::do_pack()
list<DataFile::PackSource::FileInfo> files = source.list_files();
for(list<DataFile::PackSource::FileInfo>::const_iterator i=files.begin(); i!=files.end(); ++i)
{
- IO::Seekable *in = source.open(i->name);
- IO::Base *out = open_output(i->name);
+ unique_ptr<IO::Seekable> in = source.open(i->name);
+ unique_ptr<IO::Base> out = open_output(i->name);
char buf[16384];
while(1)
{
if(len<sizeof(buf))
break;
}
- delete in;
- delete out;
}
}
void DataTool::do_generate_builtin()
{
- IO::Base *out = open_output(out_fn);
+ unique_ptr<IO::Base> out = open_output(out_fn);
BuiltinGenerator generator(*out);
generator.begin(builtin_ns);
for(list<string>::const_iterator i=in_fns.begin(); i!=in_fns.end(); ++i)
generator.add_file(*i);
generator.end(builtin_module);
- delete out;
}
-IO::Base *DataTool::open_output(const string &fn)
+unique_ptr<IO::Base> DataTool::open_output(const string &fn)
{
if(fn=="-")
- return new IO::Buffered(IO::cout);
+ return make_unique<IO::Buffered>(IO::cout);
else
- return new IO::BufferedFile(fn, IO::M_WRITE);
+ return make_unique<IO::BufferedFile>(fn, IO::M_WRITE);
}
-IO::Base *DataTool::open_input(const string &fn)
+unique_ptr<IO::Base> DataTool::open_input(const string &fn)
{
if(fn=="-")
- return new IO::Buffered(IO::cin);
+ return make_unique<IO::Buffered>(IO::cin);
else
- return new IO::BufferedFile(fn, IO::M_READ);
+ return make_unique<IO::BufferedFile>(fn, IO::M_READ);
}
-DataFile::Writer *DataTool::create_writer(IO::Base &out)
+unique_ptr<DataFile::Writer> DataTool::create_writer(IO::Base &out)
{
- DataFile::Writer *writer = new DataFile::Writer(out);
+ unique_ptr<DataFile::Writer> writer = make_unique<DataFile::Writer>(out);
if(compress)
writer->set_compressed();
if(binary)
#ifndef TOOL_H_
#define TOOL_H_
+#include <memory>
#include <string>
#include <msp/core/application.h>
#include <msp/datafile/writer.h>
void do_pack();
void do_unpack();
void do_generate_builtin();
- Msp::IO::Base *open_output(const std::string &);
- Msp::IO::Base *open_input(const std::string &);
+ std::unique_ptr<Msp::IO::Base> open_output(const std::string &);
+ std::unique_ptr<Msp::IO::Base> open_input(const std::string &);
public:
bool is_compressed() const { return compress; }
- Msp::DataFile::Writer *create_writer(Msp::IO::Base &);
+ std::unique_ptr<Msp::DataFile::Writer> create_writer(Msp::IO::Base &);
};
#endif