// 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/buf.h"
#include "nodegit_wrapper.cc"

  #include "../include/git_buf_converter.h"
 
#include <iostream>

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

  GitBuf::~GitBuf() {
    // 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 GitBuf::InitializeComponent(v8::Local<v8::Object> target, nodegit::Context *nodegitContext) {
    Nan::HandleScope scope;

    v8::Local<v8::External> nodegitExternal = Nan::New<v8::External>(nodegitContext);
    v8::Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(JSNewFunction, nodegitExternal);

    tpl->InstanceTemplate()->SetInternalFieldCount(2);
    tpl->SetClassName(Nan::New("Buf").ToLocalChecked());

          Nan::SetPrototypeMethod(tpl, "containsNul", ContainsNul, nodegitExternal);
            Nan::SetPrototypeMethod(tpl, "dispose", Dispose, nodegitExternal);
            Nan::SetPrototypeMethod(tpl, "grow", Grow, nodegitExternal);
            Nan::SetPrototypeMethod(tpl, "isBinary", IsBinary, nodegitExternal);
            Nan::SetPrototypeMethod(tpl, "set", Set, nodegitExternal);
           Nan::SetPrototypeMethod(tpl, "ptr", Ptr, nodegitExternal);
         Nan::SetPrototypeMethod(tpl, "asize", Asize, nodegitExternal);
         Nan::SetPrototypeMethod(tpl, "size", Size, nodegitExternal);
  
    InitializeTemplate(tpl);

    v8::Local<Function> constructor_template = Nan::GetFunction(tpl).ToLocalChecked();
    nodegitContext->SaveToPersistent("GitBuf::Template", constructor_template);
    Nan::Set(target, Nan::New("Buf").ToLocalChecked(), constructor_template);
  }

  
/*
     * @return Number  result    */
NAN_METHOD(GitBuf::ContainsNul) {
  Nan::EscapableHandleScope scope;


  git_error_clear();

  { // lock master scope start
    nodegit::LockMaster lockMaster(
      /*asyncAction: */false
            ,
              Nan::ObjectWrap::Unwrap<GitBuf>(info.This())->GetValue()
    );

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


      v8::Local<v8::Value> v8ConversionSlot;
// start convert_to_v8 block
     v8ConversionSlot = Nan::New<Number>( result);
  // end convert_to_v8 block
        return info.GetReturnValue().Set(scope.Escape(v8ConversionSlot));
  }
}
   
/*
     */
NAN_METHOD(GitBuf::Dispose) {
  Nan::EscapableHandleScope scope;


  git_error_clear();

  { // lock master scope start
    nodegit::LockMaster lockMaster(
      /*asyncAction: */false
            ,
              Nan::ObjectWrap::Unwrap<GitBuf>(info.This())->GetValue()
    );

    git_buf_dispose(
          Nan::ObjectWrap::Unwrap<GitBuf>(info.This())->GetValue()
    );

      return info.GetReturnValue().Set(scope.Escape(Nan::Undefined()));
  }
}
   
/*
   * @param Number target_size
     */
NAN_METHOD(GitBuf::Grow) {
  Nan::EscapableHandleScope scope;

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

// start convert_from_v8 block
  size_t from_target_size;
      from_target_size = (size_t)   info[0].As<v8::Number>()->Value();
// end convert_from_v8 block

  git_error_clear();

  { // lock master scope start
    nodegit::LockMaster lockMaster(
      /*asyncAction: */false
            ,
              Nan::ObjectWrap::Unwrap<GitBuf>(info.This())->GetValue()
    );

 int result =     git_buf_grow(
          Nan::ObjectWrap::Unwrap<GitBuf>(info.This())->GetValue()
,          from_target_size
    );

      if (result != GIT_OK) {

        if (git_error_last()) {
          return Nan::ThrowError(git_error_last()->message);
        } else {
          return Nan::ThrowError("Unknown Error");
        }
      } // lock master scope end
      return info.GetReturnValue().Set(scope.Escape(Nan::Undefined()));
  }
}
   
/*
     * @return Number  result    */
NAN_METHOD(GitBuf::IsBinary) {
  Nan::EscapableHandleScope scope;


  git_error_clear();

  { // lock master scope start
    nodegit::LockMaster lockMaster(
      /*asyncAction: */false
            ,
              Nan::ObjectWrap::Unwrap<GitBuf>(info.This())->GetValue()
    );

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


      v8::Local<v8::Value> v8ConversionSlot;
// start convert_to_v8 block
     v8ConversionSlot = Nan::New<Number>( result);
  // end convert_to_v8 block
        return info.GetReturnValue().Set(scope.Escape(v8ConversionSlot));
  }
}
   
/*
   * @param Buffer data
   * @param Number datalen
     */
NAN_METHOD(GitBuf::Set) {
  Nan::EscapableHandleScope scope;

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

  if (info.Length() == 1 || !info[1]->IsNumber()) {
    return Nan::ThrowError("Number datalen is required.");
  }

// start convert_from_v8 block
  const void * from_data = NULL;

  from_data = Buffer::Data(Nan::To<v8::Object>(info[0]).ToLocalChecked());
// end convert_from_v8 block
// start convert_from_v8 block
  size_t from_datalen;
      from_datalen = (size_t)   info[1].As<v8::Number>()->Value();
// end convert_from_v8 block

  git_error_clear();

  { // lock master scope start
    nodegit::LockMaster lockMaster(
      /*asyncAction: */false
            ,
              Nan::ObjectWrap::Unwrap<GitBuf>(info.This())->GetValue()
            ,
              from_data
    );

 int result =     git_buf_set(
          Nan::ObjectWrap::Unwrap<GitBuf>(info.This())->GetValue()
,          from_data
,          from_datalen
    );

      if (result != GIT_OK) {

        if (git_error_last()) {
          return Nan::ThrowError(git_error_last()->message);
        } else {
          return Nan::ThrowError("Unknown Error");
        }
      } // lock master scope end
      return info.GetReturnValue().Set(scope.Escape(Nan::Undefined()));
  }
}
       // start field block
    NAN_METHOD(GitBuf::Ptr) {
      v8::Local<v8::Value> v8ConversionSlot;

            char *
           ptr =
          Nan::ObjectWrap::Unwrap<GitBuf>(info.This())->GetValue()->ptr;
 // start convert_to_v8 block
  if (ptr){
       v8ConversionSlot = Nan::New<v8::String>(ptr).ToLocalChecked();
   }
  else {
    v8ConversionSlot = Nan::Null();
  }

  // end convert_to_v8 block
      info.GetReturnValue().Set(v8ConversionSlot);
    }
    // end field block
     // start field block
    NAN_METHOD(GitBuf::Asize) {
      v8::Local<v8::Value> v8ConversionSlot;

            size_t
           asize =
          Nan::ObjectWrap::Unwrap<GitBuf>(info.This())->GetValue()->asize;
 // start convert_to_v8 block
     v8ConversionSlot = Nan::New<Number>( asize);
  // end convert_to_v8 block
      info.GetReturnValue().Set(v8ConversionSlot);
    }
    // end field block
     // start field block
    NAN_METHOD(GitBuf::Size) {
      v8::Local<v8::Value> v8ConversionSlot;

            size_t
           size =
          Nan::ObjectWrap::Unwrap<GitBuf>(info.This())->GetValue()->size;
 // start convert_to_v8 block
     v8ConversionSlot = Nan::New<Number>( size);
  // end convert_to_v8 block
      info.GetReturnValue().Set(v8ConversionSlot);
    }
    // end field block
  // force base class template instantiation, to make sure we get all the
// methods, statics, etc.
template class NodeGitWrapper<GitBufTraits>;
 