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

  #include "../include/packbuilder.h"
  #include "../include/oid.h"
 
#include <iostream>

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

  GitPackbuilder::GitPackbuilder(git_packbuilder *raw, bool selfFreeing) {
    this->raw = raw;
    this->selfFreeing = selfFreeing;
  }

  GitPackbuilder::~GitPackbuilder() {
      if (this->selfFreeing) {
        git_packbuilder_free(this->raw);
      }
   }

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

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

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

          NODE_SET_PROTOTYPE_METHOD(tpl, "hash", Hash);
            NODE_SET_PROTOTYPE_METHOD(tpl, "insert", Insert);
            NODE_SET_PROTOTYPE_METHOD(tpl, "insertCommit", InsertCommit);
            NODE_SET_PROTOTYPE_METHOD(tpl, "insertTree", InsertTree);
            NODE_SET_PROTOTYPE_METHOD(tpl, "objectCount", ObjectCount);
            NODE_SET_PROTOTYPE_METHOD(tpl, "setThreads", SetThreads);
            NODE_SET_PROTOTYPE_METHOD(tpl, "written", Written);
    
    Local<Function> _constructor_template = tpl->GetFunction();
    NanAssignPersistent(constructor_template, _constructor_template);
    target->Set(NanNew<String>("Packbuilder"), _constructor_template);
  }

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

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

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

    NanReturnValue(args.This());
  }

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

  git_packbuilder *GitPackbuilder::GetValue() {
    return this->raw;
  }

  git_packbuilder **GitPackbuilder::GetRefValue() {
    return &this->raw;
  }

  
/*
     * @return Oid  result    */
NAN_METHOD(GitPackbuilder::Hash) {
  NanEscapableScope();

  const git_oid * result = git_packbuilder_hash(
ObjectWrap::Unwrap<GitPackbuilder>(args.This())->GetValue()
  );
  // null checks on pointers
  if (!result) {
    NodeGitPsueodoNanReturnEscapingValue(NanUndefined());
  }

  Handle<Value> to;
// start convert_to_v8 block
  
  if (result != NULL) {
    // GitOid result
       to = GitOid::New((void *)result, false);
   }
  else {
    to = NanNull();
  }

 // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
   
/*
   * @param Oid id
   * @param String name
     * @return Number  result    */
NAN_METHOD(GitPackbuilder::Insert) {
  NanEscapableScope();

  if (args.Length() == 0
    || (!args[0]->IsObject() && !args[0]->IsString())) {
    return NanThrowError("Oid id is required.");
  }

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

// start convert_from_v8 block
  const git_oid * from_id;
  if (args[0]->IsString()) {
    // Try and parse in a string to a git_oid
    String::Utf8Value oidString(args[0]->ToString());
    git_oid *oidOut = (git_oid *)malloc(sizeof(git_oid));

    if (git_oid_fromstr(oidOut, (const char *) strdup(*oidString)) != GIT_OK) {
      free(oidOut);

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

    from_id = oidOut;
  }
  else {
    from_id = ObjectWrap::Unwrap<GitOid>(args[0]->ToObject())->GetValue();
  }
// end convert_from_v8 block
// start convert_from_v8 block
  const char * from_name;

  String::Utf8Value name(args[1]->ToString());
  from_name = (const char *) strdup(*name);
// end convert_from_v8 block
  int result = git_packbuilder_insert(
ObjectWrap::Unwrap<GitPackbuilder>(args.This())->GetValue()
,from_id
,from_name
  );
  if (args[0]->IsString()) {
    free(&from_id);
  }

  Handle<Value> to;
// start convert_to_v8 block
     to = NanNew<Number>(result);
  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
   
/*
   * @param Oid id
     * @return Number  result    */
NAN_METHOD(GitPackbuilder::InsertCommit) {
  NanEscapableScope();

  if (args.Length() == 0
    || (!args[0]->IsObject() && !args[0]->IsString())) {
    return NanThrowError("Oid id is required.");
  }

// start convert_from_v8 block
  const git_oid * from_id;
  if (args[0]->IsString()) {
    // Try and parse in a string to a git_oid
    String::Utf8Value oidString(args[0]->ToString());
    git_oid *oidOut = (git_oid *)malloc(sizeof(git_oid));

    if (git_oid_fromstr(oidOut, (const char *) strdup(*oidString)) != GIT_OK) {
      free(oidOut);

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

    from_id = oidOut;
  }
  else {
    from_id = ObjectWrap::Unwrap<GitOid>(args[0]->ToObject())->GetValue();
  }
// end convert_from_v8 block
  int result = git_packbuilder_insert_commit(
ObjectWrap::Unwrap<GitPackbuilder>(args.This())->GetValue()
,from_id
  );
  if (args[0]->IsString()) {
    free(&from_id);
  }

  Handle<Value> to;
// start convert_to_v8 block
     to = NanNew<Number>(result);
  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
   
/*
   * @param Oid id
     * @return Number  result    */
NAN_METHOD(GitPackbuilder::InsertTree) {
  NanEscapableScope();

  if (args.Length() == 0
    || (!args[0]->IsObject() && !args[0]->IsString())) {
    return NanThrowError("Oid id is required.");
  }

// start convert_from_v8 block
  const git_oid * from_id;
  if (args[0]->IsString()) {
    // Try and parse in a string to a git_oid
    String::Utf8Value oidString(args[0]->ToString());
    git_oid *oidOut = (git_oid *)malloc(sizeof(git_oid));

    if (git_oid_fromstr(oidOut, (const char *) strdup(*oidString)) != GIT_OK) {
      free(oidOut);

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

    from_id = oidOut;
  }
  else {
    from_id = ObjectWrap::Unwrap<GitOid>(args[0]->ToObject())->GetValue();
  }
// end convert_from_v8 block
  int result = git_packbuilder_insert_tree(
ObjectWrap::Unwrap<GitPackbuilder>(args.This())->GetValue()
,from_id
  );
  if (args[0]->IsString()) {
    free(&from_id);
  }

  Handle<Value> to;
// start convert_to_v8 block
     to = NanNew<Number>(result);
  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
   
/*
     * @return Number  result    */
NAN_METHOD(GitPackbuilder::ObjectCount) {
  NanEscapableScope();

  uint32_t result = git_packbuilder_object_count(
ObjectWrap::Unwrap<GitPackbuilder>(args.This())->GetValue()
  );

  Handle<Value> to;
// start convert_to_v8 block
     to = NanNew<Number>(result);
  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
   
/*
   * @param Number n
     * @return Number  result    */
NAN_METHOD(GitPackbuilder::SetThreads) {
  NanEscapableScope();

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

// start convert_from_v8 block
  unsigned int from_n;
  from_n = (unsigned int)   args[0]->ToNumber()->Value();
// end convert_from_v8 block
  unsigned int result = git_packbuilder_set_threads(
ObjectWrap::Unwrap<GitPackbuilder>(args.This())->GetValue()
,from_n
  );

  Handle<Value> to;
// start convert_to_v8 block
     to = NanNew<Number>(result);
  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
   
/*
     * @return Number  result    */
NAN_METHOD(GitPackbuilder::Written) {
  NanEscapableScope();

  uint32_t result = git_packbuilder_written(
ObjectWrap::Unwrap<GitPackbuilder>(args.This())->GetValue()
  );

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