#include "pbio/ContextTemplate.h"
#import <Foundation/Foundation.h>
#import "ContextTemplate.h"


#import "TryCatch.mm"
#include <fstream>
#include <iostream>


@implementation ContextTemplate

-(nonnull instancetype) init: (void*) templ{
    _ptr = templ;
    return self;
}

-(void) dealloc{
    delete static_cast<pbio::ContextTemplate::Ptr*>(_ptr);
}

-(void* _Nonnull) ptr{
    return _ptr;
}

-(NSString *_Nonnull) getMethodName{
    pbio::ContextTemplate::Ptr& templ = *(static_cast<pbio::ContextTemplate::Ptr*>(_ptr));
    std::string value = tryCatch(templ->getMethodName());
    return [NSString stringWithUTF8String:value.c_str()];
}

-(void) save: (NSOutputStream* _Nonnull) output_stream {
    std::stringstream tmp;
    pbio::ContextTemplate::Ptr& templ = *(static_cast<pbio::ContextTemplate::Ptr*>(_ptr));
    tryCatch(templ->save(tmp));
    
    [output_stream write: reinterpret_cast<const uint8_t*>(tmp.str().c_str()) maxLength: tmp.str().size()];
}

-(size_t) size {
    pbio::ContextTemplate::Ptr& templ = *(static_cast<pbio::ContextTemplate::Ptr*>(_ptr));
    return tryCatch(templ->size());
}

@end
