//
//  MPWObjectCreatorStream.m
//  MPWFoundation
//
//  Created by Marcel Weiher on 16/05/16.
//
//

#import "MPWObjectCreatorStream.h"

@interface MPWObjectCreatorStream()

@property (assign) Class targetClass;

@end

@implementation NSObject(StreamClassCreating)

-(void)writeOnObjectCreationStream:(MPWObjectCreatorStream *)aStream
{
    [self flattenStructureOntoStream:aStream];
}

@end


@implementation MPWObjectCreatorStream


+streamWithClass:(Class)newTargetClass
{
    return [[[self alloc] initWithClass:newTargetClass target:nil] autorelease];
}

-initWithClass:(Class)newTargetClass target:newTarget
{
    self=[super initWithTarget:newTarget];
    self.targetClass=newTargetClass;
    
    return self;
}

-(SEL)streamWriterMessage
{
    return @selector(writeOnObjectCreationStream:);
}


-(void)writeDictionary:(NSDictionary *)aDictionary
{
    id anObject=[[self.targetClass alloc] initWithDictionary:aDictionary];
    FORWARD(anObject);
    [anObject release];
}

-(void)writeData:(NSData *)someData
{
    id anObject=[[self.targetClass alloc] initWithData:someData];
    FORWARD(anObject);
    [anObject release];
}

@end



@implementation NSData(StreamClassCreating)

-(void)writeOnObjectCreationStream:(MPWObjectCreatorStream *)aStream
{
    [aStream writeData:self];
}

@end
