// This is a generated file, modify: generate/templates/class_content.cc.
#include <nan.h>
#include <string.h>

extern "C" {
#include <git2.h>
}


#include "../include/functions/copy.h"
#include "../include/macros.h"
#include "../include/error.h"


#include <iostream>

using namespace std;
using namespace v8;
using namespace node;

GitError::GitError(git_error *raw, bool selfFreeing)
{
  this->raw = raw;
  this->selfFreeing = selfFreeing;
}

GitError::~GitError()
{
}

void GitError::InitializeComponent(Handle<v8::Object> target)
{
  NanScope();

  Local<FunctionTemplate> tpl = NanNew<FunctionTemplate>(JSNewFunction);

  tpl->InstanceTemplate()->SetInternalFieldCount(1);
  tpl->SetClassName(NanNew<String>("Error"));

  NODE_SET_PROTOTYPE_METHOD(tpl, "message", Message);
  NODE_SET_PROTOTYPE_METHOD(tpl, "klass", Klass);

  Local<Function> _constructor_template = tpl->GetFunction();
  NanAssignPersistent(constructor_template, _constructor_template);
  target->Set(NanNew<String>("Error"), _constructor_template);
}

NAN_METHOD(GitError::JSNewFunction)
{
  NanScope();

  if (args.Length() == 0 || !args[0]->IsExternal()) {
    return NanThrowError("A new GitError cannot be instantiated.");
  }

  GitError* object = new GitError(static_cast<git_error *>(Handle<External>::Cast(args[0])->Value()), args[1]->BooleanValue());
  object->Wrap(args.This());

  NanReturnValue(args.This());
}

Handle<Value> GitError::New(void *raw, bool selfFreeing)
{
  NanEscapableScope();
  Handle<Value> argv[2] = { NanNew<External>((void *)raw), NanNew<Boolean>(selfFreeing) };
  return NanEscapeScope(NanNew<Function>(GitError::constructor_template)->NewInstance(2, argv));
}

git_error *GitError::GetValue()
{
  return this->raw;
}

git_error **GitError::GetRefValue()
{
  return &this->raw;
}

NAN_METHOD(GitError::Message)
{
  NanScope();
  Handle<Value> to;

  char *  message =
    ObjectWrap::Unwrap<GitError>(args.This())->GetValue()->message;

// start convert_to_v8 block
  if (message) {
    to = NanNew<String>(message);
  } else {
    to = NanNull();
  }

  // end convert_to_v8 block
  NanReturnValue(to);
}
NAN_METHOD(GitError::Klass)
{
  NanScope();
  Handle<Value> to;

  int  klass =
    ObjectWrap::Unwrap<GitError>(args.This())->GetValue()->klass;

// start convert_to_v8 block
  to = NanNew<Number>(klass);
  // end convert_to_v8 block
  NanReturnValue(to);
}
Persistent<Function> GitError::constructor_template;
