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

#include "../include/refspec.h"

#include <iostream>

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

GitRefspec::GitRefspec(git_refspec *raw, bool selfFreeing)
{
  this->raw = raw;
  this->selfFreeing = selfFreeing;
}

GitRefspec::~GitRefspec()
{
}

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

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

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

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

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

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

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

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

  NanReturnValue(args.This());
}

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

git_refspec *GitRefspec::GetValue()
{
  return this->raw;
}

git_refspec **GitRefspec::GetRefValue()
{
  return &this->raw;
}


/*
     * @return Number  result    */
NAN_METHOD(GitRefspec::Direction)
{
  NanEscapableScope();

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

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

/*
     * @return String  result    */
NAN_METHOD(GitRefspec::Dst)
{
  NanEscapableScope();

  const char * result = git_refspec_dst(
                          ObjectWrap::Unwrap<GitRefspec>(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);
}

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

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

// start convert_from_v8 block
  const char * from_refname;

  String::Utf8Value refname(args[0]->ToString());
  from_refname = (const char *) strdup(*refname);
// end convert_from_v8 block
  int result = git_refspec_dst_matches(
                 ObjectWrap::Unwrap<GitRefspec>(args.This())->GetValue()
                 ,from_refname
               );

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

/*
     * @return Number  result    */
NAN_METHOD(GitRefspec::Force)
{
  NanEscapableScope();

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

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

/*
     * @return String  result    */
NAN_METHOD(GitRefspec::Src)
{
  NanEscapableScope();

  const char * result = git_refspec_src(
                          ObjectWrap::Unwrap<GitRefspec>(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);
}

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

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

// start convert_from_v8 block
  const char * from_refname;

  String::Utf8Value refname(args[0]->ToString());
  from_refname = (const char *) strdup(*refname);
// end convert_from_v8 block
  int result = git_refspec_src_matches(
                 ObjectWrap::Unwrap<GitRefspec>(args.This())->GetValue()
                 ,from_refname
               );

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