/*
Copyright (c) 2013-2016 Gustav Hartvigsson

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#include "baseobject.h"
#include "Callback.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>


/* ---------------------------
 * Concrete method definitions
 * These are implemented towards the end of this file.
 * ---------------------------
 */
void
method_base_free (SObject * self);

int
method_base_ref (SObject * self);

int
method_base_unref (SObject * self);

int
method_base_get_refcount (SObject * self);

char *
method_base_to_string (SObject * self);

/* -----------------
 * Helper functions...
 * -----------------
 */
void
s_object_set_free_method (SObject * self, MethodFunc method) {
  SObjectClass * klass = s_object_get_class (self);
  klass->free = method;
}


void
s_object_set_ref_method (SObject * self,  MethodFuncInt method) {
  SObjectClass * klass = s_object_get_class (self);
  klass->ref = method;
}


void
s_object_set_unref_method (SObject * self,  MethodFuncInt method) {
  SObjectClass * klass = s_object_get_class (self);
  klass->unref = method;
}


void
s_object_set_get_refcount_method (SObject * self,  MethodFuncInt method) {
  SObjectClass * klass = s_object_get_class (self);
  klass->get_refcount = method;
}


void
s_object_set_to_string_method (SObject * self, ToStringFunc method) {
  SObjectClass * klass = s_object_get_class (self);
  klass->to_string = method;
}

void
s_object_set_deinitialize_method (SObject * self,  MethodFunc method) {
  SObjectClass * klass = s_object_get_class (self);
  klass->deinitialize = method;
}

void
s_object_initialize (SObject * self, const schar * name) {
  self->name = s_string_new (name);

  self->refcount = 1;

  self->callbacks = s_map_new (COMPFUNC (s_string_is_equal),
                               HASH_FUNC (sdbm_hash),
                               FREEFUNC (s_free),
                               FREEFUNC (s_callback_entry_free));

  s_object_set_free_method (self, method_base_free);
  s_object_set_ref_method (self, method_base_ref);
  s_object_set_unref_method (self, method_base_unref);
  s_object_set_get_refcount_method (self, method_base_get_refcount);
  s_object_set_to_string_method (self, method_base_to_string);
}

SObject *
s_object_new () {
  s_warn_print ("s_object_new () should never be used in production code");

  SObject * self = s_malloc (sizeof (SObject));
  //allocate the class definition of the object.
  self->base_class = s_malloc (sizeof(SObjectClass));
  //initialize it.
  s_object_initialize (self, "SObject");
  return self;
}

void
s_object_free (SObject * self) {
  s_free (self->name);
  s_map_free (self->callbacks, TRUE);
  SObjectClass * klass = s_object_get_class (self);
  klass->free (self);
}

SObjectClass *
s_object_get_class (SObject * self) {
  return self->base_class;
}

void
s_object_set_class (SObject * self, SObjectClass * klass) {
  self->base_class = klass;
}

sint
s_object_unref (SObject * self) {
  unsigned int ret = self->base_class->unref (self);
  return ret;
}

int
s_object_ref (SObject * self) {
  unsigned int ret = self->base_class->ref (self);
  return ret;
}

/**
 * This function returns the current reference count without chaning it.
 */
int
s_object_get_refcount (SObject * self) {
  unsigned int ret = self->base_class->get_refcount (self);
  return ret;
}

char *
s_object_to_string (SObject * self) {
  return self->base_class->to_string (self);
}

/* -----------------
 * Contrete methods.
 * -----------------
 */

void
method_base_free (SObject * self) {
  s_free (self->base_class);
  s_free (self);
}

int
method_base_ref (SObject * self) {
  self->refcount = self->refcount + 1;
  return self->refcount;
}

int
method_base_unref (SObject * self) {
  self->refcount = self->refcount - 1;
  if (self->refcount == 0) {
    s_object_free (self);
    return 0;
  }
  return self->refcount;
}

int
method_base_get_refcount (SObject * self) {
  return self->refcount;
}

char *
method_base_to_string (SObject * self) {
  char * ret_string = s_string_new_fmt ("(SObject, References: %d)",
                                        self->base_class->get_refcount(self));
  return ret_string;
}


