// 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/oid.h"

#include "../include/oid.h"

#include <iostream>

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

GitOid::GitOid(git_oid *raw, bool selfFreeing)
{
  this->raw = raw;
  this->selfFreeing = selfFreeing;
}

GitOid::~GitOid()
{
}

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

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

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

  NODE_SET_PROTOTYPE_METHOD(tpl, "allocfmt", Allocfmt);
  NODE_SET_PROTOTYPE_METHOD(tpl, "cmp", Cmp);
  NODE_SET_PROTOTYPE_METHOD(tpl, "equal", Equal);
  NODE_SET_METHOD(tpl, "fromString", Fromstr);
  NODE_SET_PROTOTYPE_METHOD(tpl, "iszero", Iszero);
  NODE_SET_PROTOTYPE_METHOD(tpl, "ncmp", Ncmp);
  NODE_SET_PROTOTYPE_METHOD(tpl, "strcmp", Strcmp);
  NODE_SET_PROTOTYPE_METHOD(tpl, "streq", Streq);

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

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

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

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

  NanReturnValue(args.This());
}

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

git_oid *GitOid::GetValue()
{
  return this->raw;
}

git_oid **GitOid::GetRefValue()
{
  return &this->raw;
}


/*
     * @return String  result    */
NAN_METHOD(GitOid::Allocfmt)
{
  NanEscapableScope();

  char * result = git_oid_allocfmt(
                    ObjectWrap::Unwrap<GitOid>(args.This())->GetValue()
                  );
  // null checks on pointers
  if (!result) {
    NodeGitPsueodoNanReturnEscapingValue(NanUndefined());
  }

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

  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}

/*
       * @return Number  result    */
NAN_METHOD(GitOid::Cmp)
{
  NanEscapableScope();

  int result = git_oid_cmp(
                 ObjectWrap::Unwrap<GitOid>(args.This())->GetValue()
                 ,ObjectWrap::Unwrap<GitOid>(args.This())->GetValue()
               );

  Handle<Value> to;
// start convert_to_v8 block
  to = NanNew<Number>(result);
  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}

/*
       * @return Number  result    */
NAN_METHOD(GitOid::Equal)
{
  NanEscapableScope();

  int result = git_oid_equal(
                 ObjectWrap::Unwrap<GitOid>(args.This())->GetValue()
                 ,ObjectWrap::Unwrap<GitOid>(args.This())->GetValue()
               );

  Handle<Value> to;
// start convert_to_v8 block
  to = NanNew<Number>(result);
  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}

/*
  * @param String str
     * @return Oid out    */
NAN_METHOD(GitOid::Fromstr)
{
  NanEscapableScope();

  if (args.Length() == 0 || !args[0]->IsString()) {
    return NanThrowError("String str is required.");
  }

  git_oid *out = (git_oid *)malloc(sizeof(git_oid));
// start convert_from_v8 block
  const char * from_str;

  String::Utf8Value str(args[0]->ToString());
  from_str = (const char *) strdup(*str);
// end convert_from_v8 block
  int result = git_oid_fromstr(
                 out
                 ,from_str
               );
  if (result != GIT_OK) {
    free(out);

    if (giterr_last()) {
      return NanThrowError(giterr_last()->message);
    } else {
      return NanThrowError("Unknown Error");
    }
  }

  Handle<Value> to;
// start convert_to_v8 block

  if (out != NULL) {
    // GitOid out
    to = GitOid::New((void *)out, false);
  } else {
    to = NanNull();
  }

// end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}

/*
     * @return Number  result    */
NAN_METHOD(GitOid::Iszero)
{
  NanEscapableScope();

  int result = git_oid_iszero(
                 ObjectWrap::Unwrap<GitOid>(args.This())->GetValue()
               );

  Handle<Value> to;
// start convert_to_v8 block
  to = NanNew<Number>(result);
  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}

/*
     * @param Number len
     * @return Number  result    */
NAN_METHOD(GitOid::Ncmp)
{
  NanEscapableScope();

  if (args.Length() == 0 || !args[0]->IsNumber()) {
    return NanThrowError("Number len is required.");
  }

// start convert_from_v8 block
  size_t from_len;
  from_len = (size_t)   args[0]->ToNumber()->Value();
// end convert_from_v8 block
  int result = git_oid_ncmp(
                 ObjectWrap::Unwrap<GitOid>(args.This())->GetValue()
                 ,ObjectWrap::Unwrap<GitOid>(args.This())->GetValue()
                 ,from_len
               );

  Handle<Value> to;
// start convert_to_v8 block
  to = NanNew<Number>(result);
  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}

/*
   * @param String str
     * @return Number  result    */
NAN_METHOD(GitOid::Strcmp)
{
  NanEscapableScope();

  if (args.Length() == 0 || !args[0]->IsString()) {
    return NanThrowError("String str is required.");
  }

// start convert_from_v8 block
  const char * from_str;

  String::Utf8Value str(args[0]->ToString());
  from_str = (const char *) strdup(*str);
// end convert_from_v8 block
  int result = git_oid_strcmp(
                 ObjectWrap::Unwrap<GitOid>(args.This())->GetValue()
                 ,from_str
               );

  Handle<Value> to;
// start convert_to_v8 block
  to = NanNew<Number>(result);
  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}

/*
   * @param String str
     * @return Number  result    */
NAN_METHOD(GitOid::Streq)
{
  NanEscapableScope();

  if (args.Length() == 0 || !args[0]->IsString()) {
    return NanThrowError("String str is required.");
  }

// start convert_from_v8 block
  const char * from_str;

  String::Utf8Value str(args[0]->ToString());
  from_str = (const char *) strdup(*str);
// end convert_from_v8 block
  int result = git_oid_streq(
                 ObjectWrap::Unwrap<GitOid>(args.This())->GetValue()
                 ,from_str
               );

  Handle<Value> to;
// start convert_to_v8 block
  to = NanNew<Number>(result);
  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
Persistent<Function> GitOid::constructor_template;
