// This is a generated file, modify: generate/templates/templates/class_content.cc

#include <nan.h>
#include <string.h>

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

#include "../include/nodegit.h"
#include "../include/lock_master.h"
#include "../include/functions/copy.h"
#include "../include/refspec.h"
#include "nodegit_wrapper.cc"
#include "../include/async_libgit2_queue_worker.h"


#include <iostream>

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

GitRefspec::~GitRefspec()
{
  // this will cause an error if you have a non-self-freeing object that also needs
  // to save values. Since the object that will eventually free the object has no
  // way of knowing to free these values.
}

void GitRefspec::InitializeComponent(v8::Local<v8::Object> target)
{
  Nan::HandleScope scope;

  v8::Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(JSNewFunction);

  tpl->InstanceTemplate()->SetInternalFieldCount(1);
  tpl->SetClassName(Nan::New("Refspec").ToLocalChecked());

  Nan::SetPrototypeMethod(tpl, "direction", Direction);
  Nan::SetPrototypeMethod(tpl, "dst", Dst);
  Nan::SetPrototypeMethod(tpl, "dstMatches", DstMatches);
  Nan::SetPrototypeMethod(tpl, "force", Force);
  Nan::SetPrototypeMethod(tpl, "src", Src);
  Nan::SetPrototypeMethod(tpl, "srcMatches", SrcMatches);

  InitializeTemplate(tpl);

  v8::Local<Function> _constructor_template = Nan::GetFunction(tpl).ToLocalChecked();
  constructor_template.Reset(_constructor_template);
  Nan::Set(target, Nan::New("Refspec").ToLocalChecked(), _constructor_template);
}


/*
     * @return Number  result    */
NAN_METHOD(GitRefspec::Direction)
{
  Nan::EscapableHandleScope scope;


  giterr_clear();

  {
    LockMaster lockMaster(/*asyncAction: */false,    Nan::ObjectWrap::Unwrap<GitRefspec>(info.This())->GetValue()
                         );

    git_direction result = git_refspec_direction(
                             Nan::ObjectWrap::Unwrap<GitRefspec>(info.This())->GetValue()
                           );


    v8::Local<v8::Value> to;
// start convert_to_v8 block
    to = Nan::New<Number>( result);
    // end convert_to_v8 block
    return info.GetReturnValue().Set(scope.Escape(to));
  }
}

/*
     * @return String  result    */
NAN_METHOD(GitRefspec::Dst)
{
  Nan::EscapableHandleScope scope;


  giterr_clear();

  {
    LockMaster lockMaster(/*asyncAction: */false,    Nan::ObjectWrap::Unwrap<GitRefspec>(info.This())->GetValue()
                         );

    const char * result = git_refspec_dst(
                            Nan::ObjectWrap::Unwrap<GitRefspec>(info.This())->GetValue()
                          );

    // null checks on pointers
    if (!result) {
      return info.GetReturnValue().Set(scope.Escape(Nan::Undefined()));
    }

    v8::Local<v8::Value> to;
// start convert_to_v8 block
    if (result) {
      to = Nan::New<String>(result).ToLocalChecked();
    } else {
      to = Nan::Null();
    }

    // end convert_to_v8 block
    return info.GetReturnValue().Set(scope.Escape(to));
  }
}

/*
   * @param String refname
     * @return Number  result    */
NAN_METHOD(GitRefspec::DstMatches)
{
  Nan::EscapableHandleScope scope;

  if (info.Length() == 0 || !info[0]->IsString()) {
    return Nan::ThrowError("String refname is required.");
  }

// start convert_from_v8 block
  const char * from_refname = NULL;

  String::Utf8Value refname(info[0]->ToString());
  // malloc with one extra byte so we can add the terminating null character C-strings expect:
  from_refname = (const char *) malloc(refname.length() + 1);
  // copy the characters from the nodejs string into our C-string (used instead of strdup or strcpy because nulls in
  // the middle of strings are valid coming from nodejs):
  memcpy((void *)from_refname, *refname, refname.length());
  // ensure the final byte of our new string is null, extra casts added to ensure compatibility with various C types
  // used in the nodejs binding generation:
  memset((void *)(((char *)from_refname) + refname.length()), 0, 1);
// end convert_from_v8 block

  giterr_clear();

  {
    LockMaster lockMaster(/*asyncAction: */false,    Nan::ObjectWrap::Unwrap<GitRefspec>(info.This())->GetValue()
                                           ,    from_refname
                         );

    int result = git_refspec_dst_matches(
                   Nan::ObjectWrap::Unwrap<GitRefspec>(info.This())->GetValue()
                   ,  from_refname
                 );


    v8::Local<v8::Value> to;
// start convert_to_v8 block
    to = Nan::New<Number>( result);
    // end convert_to_v8 block
    return info.GetReturnValue().Set(scope.Escape(to));
  }
}

/*
     * @return Number  result    */
NAN_METHOD(GitRefspec::Force)
{
  Nan::EscapableHandleScope scope;


  giterr_clear();

  {
    LockMaster lockMaster(/*asyncAction: */false,    Nan::ObjectWrap::Unwrap<GitRefspec>(info.This())->GetValue()
                         );

    int result = git_refspec_force(
                   Nan::ObjectWrap::Unwrap<GitRefspec>(info.This())->GetValue()
                 );


    v8::Local<v8::Value> to;
// start convert_to_v8 block
    to = Nan::New<Number>( result);
    // end convert_to_v8 block
    return info.GetReturnValue().Set(scope.Escape(to));
  }
}

/*
     * @return String  result    */
NAN_METHOD(GitRefspec::Src)
{
  Nan::EscapableHandleScope scope;


  giterr_clear();

  {
    LockMaster lockMaster(/*asyncAction: */false,    Nan::ObjectWrap::Unwrap<GitRefspec>(info.This())->GetValue()
                         );

    const char * result = git_refspec_src(
                            Nan::ObjectWrap::Unwrap<GitRefspec>(info.This())->GetValue()
                          );

    // null checks on pointers
    if (!result) {
      return info.GetReturnValue().Set(scope.Escape(Nan::Undefined()));
    }

    v8::Local<v8::Value> to;
// start convert_to_v8 block
    if (result) {
      to = Nan::New<String>(result).ToLocalChecked();
    } else {
      to = Nan::Null();
    }

    // end convert_to_v8 block
    return info.GetReturnValue().Set(scope.Escape(to));
  }
}

/*
   * @param String refname
     * @return Number  result    */
NAN_METHOD(GitRefspec::SrcMatches)
{
  Nan::EscapableHandleScope scope;

  if (info.Length() == 0 || !info[0]->IsString()) {
    return Nan::ThrowError("String refname is required.");
  }

// start convert_from_v8 block
  const char * from_refname = NULL;

  String::Utf8Value refname(info[0]->ToString());
  // malloc with one extra byte so we can add the terminating null character C-strings expect:
  from_refname = (const char *) malloc(refname.length() + 1);
  // copy the characters from the nodejs string into our C-string (used instead of strdup or strcpy because nulls in
  // the middle of strings are valid coming from nodejs):
  memcpy((void *)from_refname, *refname, refname.length());
  // ensure the final byte of our new string is null, extra casts added to ensure compatibility with various C types
  // used in the nodejs binding generation:
  memset((void *)(((char *)from_refname) + refname.length()), 0, 1);
// end convert_from_v8 block

  giterr_clear();

  {
    LockMaster lockMaster(/*asyncAction: */false,    Nan::ObjectWrap::Unwrap<GitRefspec>(info.This())->GetValue()
                                           ,    from_refname
                         );

    int result = git_refspec_src_matches(
                   Nan::ObjectWrap::Unwrap<GitRefspec>(info.This())->GetValue()
                   ,  from_refname
                 );


    v8::Local<v8::Value> to;
// start convert_to_v8 block
    to = Nan::New<Number>( result);
    // end convert_to_v8 block
    return info.GetReturnValue().Set(scope.Escape(to));
  }
}
// force base class template instantiation, to make sure we get all the
// methods, statics, etc.
template class NodeGitWrapper<GitRefspecTraits>;
