make Serializer a non-templated struct (#237)

make Serializer a non-templated struct
merged-on-behalf-of: Basile-z <Basile-z@users.noreply.github.com>
This commit is contained in:
Cameron Ross 2019-03-17 17:10:10 -02:30 committed by The Dlang Bot
parent 267b8662f0
commit 11020e46ed
2 changed files with 29 additions and 26 deletions

View file

@ -150,13 +150,15 @@ struct Dumper
try try
{ {
auto emitter = new Emitter!(Range, CharacterType)(range, canonical, indent_, textWidth, lineBreak); auto emitter = new Emitter!(Range, CharacterType)(range, canonical, indent_, textWidth, lineBreak);
auto serializer = Serializer!(Range, CharacterType)(emitter, resolver, explicitStart ? Yes.explicitStart : No.explicitStart, auto serializer = Serializer(resolver, explicitStart ? Yes.explicitStart : No.explicitStart,
explicitEnd ? Yes.explicitEnd : No.explicitEnd, YAMLVersion, tags_); explicitEnd ? Yes.explicitEnd : No.explicitEnd, YAMLVersion, tags_);
serializer.startStream(emitter);
foreach(ref document; documents) foreach(ref document; documents)
{ {
auto data = representData(document, defaultScalarStyle, defaultCollectionStyle); auto data = representData(document, defaultScalarStyle, defaultCollectionStyle);
serializer.serialize(data); serializer.serialize(emitter, data);
} }
serializer.endStream(emitter);
} }
catch(YAMLException e) catch(YAMLException e)
{ {

View file

@ -27,11 +27,9 @@ import dyaml.token;
package: package:
///Serializes represented YAML nodes, generating events which are then emitted by Emitter. ///Serializes represented YAML nodes, generating events which are then emitted by Emitter.
struct Serializer(Range, CharType) struct Serializer
{ {
private: private:
///Emitter to emit events produced.
Emitter!(Range, CharType)* emitter_;
///Resolver used to determine which tags are automaticaly resolvable. ///Resolver used to determine which tags are automaticaly resolvable.
Resolver resolver_; Resolver resolver_;
@ -64,35 +62,38 @@ struct Serializer(Range, CharType)
* YAMLVersion = YAML version string. * YAMLVersion = YAML version string.
* tagDirectives = Tag directives to emit. * tagDirectives = Tag directives to emit.
*/ */
this(Emitter!(Range, CharType)* emitter, Resolver resolver, this(Resolver resolver,
const Flag!"explicitStart" explicitStart, const Flag!"explicitStart" explicitStart,
const Flag!"explicitEnd" explicitEnd, string YAMLVersion, const Flag!"explicitEnd" explicitEnd, string YAMLVersion,
TagDirective[] tagDirectives) @safe TagDirective[] tagDirectives) @safe
{ {
emitter_ = emitter;
resolver_ = resolver; resolver_ = resolver;
explicitStart_ = explicitStart; explicitStart_ = explicitStart;
explicitEnd_ = explicitEnd; explicitEnd_ = explicitEnd;
YAMLVersion_ = YAMLVersion; YAMLVersion_ = YAMLVersion;
tagDirectives_ = tagDirectives; tagDirectives_ = tagDirectives;
emitter_.emit(streamStartEvent(Mark(), Mark()));
} }
///Destroy the Serializer. ///Begin the stream.
~this() @safe void startStream(EmitterT)(ref EmitterT emitter) @safe
{ {
emitter_.emit(streamEndEvent(Mark(), Mark())); emitter.emit(streamStartEvent(Mark(), Mark()));
}
///End the stream.
void endStream(EmitterT)(ref EmitterT emitter) @safe
{
emitter.emit(streamEndEvent(Mark(), Mark()));
} }
///Serialize a node, emitting it in the process. ///Serialize a node, emitting it in the process.
void serialize(ref Node node) @safe void serialize(EmitterT)(ref EmitterT emitter, ref Node node) @safe
{ {
emitter_.emit(documentStartEvent(Mark(), Mark(), explicitStart_, emitter.emit(documentStartEvent(Mark(), Mark(), explicitStart_,
YAMLVersion_, tagDirectives_)); YAMLVersion_, tagDirectives_));
anchorNode(node); anchorNode(node);
serializeNode(node); serializeNode(emitter, node);
emitter_.emit(documentEndEvent(Mark(), Mark(), explicitEnd_)); emitter.emit(documentEndEvent(Mark(), Mark(), explicitEnd_));
serializedNodes_.destroy(); serializedNodes_.destroy();
anchors_.destroy(); anchors_.destroy();
string[Node] emptyAnchors; string[Node] emptyAnchors;
@ -187,7 +188,7 @@ struct Serializer(Range, CharType)
} }
///Serialize a node and all its subnodes. ///Serialize a node and all its subnodes.
void serializeNode(ref Node node) @safe void serializeNode(EmitterT)(ref EmitterT emitter, ref Node node) @safe
{ {
//If the node has an anchor, emit an anchor (as aliasEvent) on the //If the node has an anchor, emit an anchor (as aliasEvent) on the
//first occurrence, save it in serializedNodes_, and emit an alias //first occurrence, save it in serializedNodes_, and emit an alias
@ -198,7 +199,7 @@ struct Serializer(Range, CharType)
aliased = anchors_[node]; aliased = anchors_[node];
if((node in serializedNodes_) !is null) if((node in serializedNodes_) !is null)
{ {
emitter_.emit(aliasEvent(Mark(), Mark(), aliased)); emitter.emit(aliasEvent(Mark(), Mark(), aliased));
return; return;
} }
serializedNodes_[node] = true; serializedNodes_[node] = true;
@ -208,25 +209,25 @@ struct Serializer(Range, CharType)
case NodeID.mapping: case NodeID.mapping:
const defaultTag = resolver_.defaultMappingTag; const defaultTag = resolver_.defaultMappingTag;
const implicit = node.tag_ == defaultTag; const implicit = node.tag_ == defaultTag;
emitter_.emit(mappingStartEvent(Mark(), Mark(), aliased, node.tag_, emitter.emit(mappingStartEvent(Mark(), Mark(), aliased, node.tag_,
implicit, node.collectionStyle)); implicit, node.collectionStyle));
foreach(ref Node key, ref Node value; node) foreach(ref Node key, ref Node value; node)
{ {
serializeNode(key); serializeNode(emitter, key);
serializeNode(value); serializeNode(emitter, value);
} }
emitter_.emit(mappingEndEvent(Mark(), Mark())); emitter.emit(mappingEndEvent(Mark(), Mark()));
return; return;
case NodeID.sequence: case NodeID.sequence:
const defaultTag = resolver_.defaultSequenceTag; const defaultTag = resolver_.defaultSequenceTag;
const implicit = node.tag_ == defaultTag; const implicit = node.tag_ == defaultTag;
emitter_.emit(sequenceStartEvent(Mark(), Mark(), aliased, node.tag_, emitter.emit(sequenceStartEvent(Mark(), Mark(), aliased, node.tag_,
implicit, node.collectionStyle)); implicit, node.collectionStyle));
foreach(ref Node item; node) foreach(ref Node item; node)
{ {
serializeNode(item); serializeNode(emitter, item);
} }
emitter_.emit(sequenceEndEvent(Mark(), Mark())); emitter.emit(sequenceEndEvent(Mark(), Mark()));
return; return;
case NodeID.scalar: case NodeID.scalar:
assert(node.type == NodeType.string, "Scalar node type must be string before serialized"); assert(node.type == NodeType.string, "Scalar node type must be string before serialized");
@ -234,7 +235,7 @@ struct Serializer(Range, CharType)
const detectedTag = resolver_.resolve(NodeID.scalar, null, value, true); const detectedTag = resolver_.resolve(NodeID.scalar, null, value, true);
const bool isDetected = node.tag_ == detectedTag; const bool isDetected = node.tag_ == detectedTag;
emitter_.emit(scalarEvent(Mark(), Mark(), aliased, node.tag_, emitter.emit(scalarEvent(Mark(), Mark(), aliased, node.tag_,
isDetected, value, node.scalarStyle)); isDetected, value, node.scalarStyle));
return; return;
case NodeID.invalid: case NodeID.invalid: