Loader creates Constructor/Resolver lazily to avoid garbage when user-provided

This commit is contained in:
Ferdinand Majerech 2014-08-04 02:14:01 +02:00
parent 46a574c0b4
commit 97e717df1b

View file

@ -179,8 +179,6 @@ struct Loader
reader_ = new Reader(streamBytes); reader_ = new Reader(streamBytes);
scanner_ = new Scanner(reader_); scanner_ = new Scanner(reader_);
parser_ = new Parser(scanner_); parser_ = new Parser(scanner_);
resolver_ = new Resolver();
constructor_ = new Constructor();
} }
catch(YAMLException e) catch(YAMLException e)
{ {
@ -211,8 +209,6 @@ struct Loader
reader_ = new Reader(cast(ubyte[])yamlData); reader_ = new Reader(cast(ubyte[])yamlData);
scanner_ = new Scanner(reader_); scanner_ = new Scanner(reader_);
parser_ = new Parser(scanner_); parser_ = new Parser(scanner_);
resolver_ = new Resolver();
constructor_ = new Constructor();
} }
catch(YAMLException e) catch(YAMLException e)
{ {
@ -267,6 +263,7 @@ struct Loader
{ {
try try
{ {
lazyInitConstructorResolver();
scope(exit) { done_ = true; } scope(exit) { done_ = true; }
auto composer = new Composer(parser_, resolver_, constructor_); auto composer = new Composer(parser_, resolver_, constructor_);
enforce(composer.checkNode(), new YAMLException("No YAML document to load")); enforce(composer.checkNode(), new YAMLException("No YAML document to load"));
@ -320,6 +317,7 @@ struct Loader
scope(exit) { done_ = true; } scope(exit) { done_ = true; }
try try
{ {
lazyInitConstructorResolver();
auto composer = new Composer(parser_, resolver_, constructor_); auto composer = new Composer(parser_, resolver_, constructor_);
int result = 0; int result = 0;
@ -393,6 +391,14 @@ struct Loader
.format(name_, e.msg)); .format(name_, e.msg));
} }
} }
// Construct default constructor/resolver if the user has not yet specified
// their own.
void lazyInitConstructorResolver() @safe
{
if(resolver_ is null) { resolver_ = new Resolver(); }
if(constructor_ is null) { constructor_ = new Constructor(); }
}
} }
unittest unittest