Example #1
0
void *Object_new(size_t size, Object proto, char *description)
{
    assert(description != NULL);

    // setup the default functions in case they aren't set
    if(!proto.init) proto.init = Object_init;
    if(!proto.describe) proto.describe = Object_describe;
    if(!proto.destroy) proto.destroy = Object_destroy;
    if(!proto.attack) proto.attack = Object_attack;
    if(!proto.move) proto.move = Object_move;

    // this seems weird, but we can make a struct of one size,
    // then point a different pointer at it to "cast" it
    Object *el = calloc(1, size);
    assert(el != NULL);
    *el = proto;

    // copy the description over
    el->description = strdup(description);

    // initialize it with whatever init we were given
    if(!el->init(el)) {
        // looks like it didn't initialize properly
        el->destroy(el);
        return NULL;
    } else {
        // all done, we made an object of any type
        assert(el != NULL);
        return el;
    }
}
Example #2
0
void *Object_new(size_t size, Object proto, char *description)
{
	assert(description);
	assert(size > 0);
	//setup default functions in case they aren't already.set. 
	// proto should have some functions already set
	if(!proto.init) proto.init = Object_init;
	if(!proto.describe) proto.describe = Object_describe;
	if(!proto.destroy) proto.destroy = Object_destroy;
	if(!proto.attack) proto.attack = Object_attack;
	if(!proto.move) proto.move = Object_move;

	// calloc incurs a performance overhead over malloc
	// by setting things to zero
	Object *el = calloc(1, size);
	if(!el) {
		printf("Error: Could not allocation object\n");
		exit(1);	
	}
	*el = proto; // is this a case of struct copy
	
	el->description = strdup(description);
	if(!el->description) {
		printf("Could not duplicate string\n");
		exit(1);
	}
	
	if(!el->init(el)) {
		el->destroy(el);
		return NULL;
	} else {
		assert(el);
		return el;
	}
}
Example #3
0
void *Object_new(size_t size, Object proto, char *description)
{
    // setup the default
    if(!proto.init) proto.init = Object_init;
    if(!proto.describe) proto.describe = Object_describe;
    if(!proto.destroy) proto.destroy = Object_destroy;
    if(!proto.attack) proto.attack = Object_attack;
    if(!proto.move) proto.move = Object_move;
    //    if(!proto.get) proto.get = Object_get;
    
    // truque: faz um calloc e na hora de derreferenciar ele faz um cast automatico!!!
    Object *el = calloc(1, size);
    *el = proto;

    el->description = strdup(description);

    //  verifica se ou a função da classe Object abstrata ou a nova implementada tem como retorno 
    //  da função init o valor 1
    if(!el->init(el)) {
        // se entrar aqui ou é pau de alocação ou a função init esta mal escrita
        el->destroy(el);
        return NULL;
    } else {
        // finalmente criado e inicializando um object  
        // a função retorna ponteiro pra void mas na verdade é um Object!
        return el;
    }
}
Example #4
0
// See object.h for a long discussion of this. We allocate memory and assign
// methods to our Object here. The size is needed because we may be casting
// to a different, possibly larger, type in the calling context.
// Note that the proto input is copied by value (although the entries are all
// pointers, so the data is shared).
void *Object_new(size_t size, Object proto, char *description) {
    // this code lets us be lazy when defining subtypes and leave all the
    // methods we aren't subtyping as null.
    if (!proto.init) proto.init = Object_init;
    if (!proto.describe) proto.describe = Object_describe;
    if (!proto.destroy) proto.destroy = Object_destroy;
    if (!proto.attack) proto.attack = Object_attack;
    if (!proto.move) proto.move = Object_move;

    // calloc is the same as malloc except
    //   (1) it takes a replication number and a size, not just a size, and
    //   (2) it zeros out the allocated bytes
    Object *obj = calloc(1, size);
    // the online code is missing this check, but I'm pretty sure the
    // `*obj = proto` is unsafe if we don't check.
    if (!obj) { printf("Error: memory error\n"); exit(1); }

    *obj = proto;  // this is a copy by value of all the pointers from proto
    obj->description = strdup(description); // copy the description
    if (!obj->init(obj)) { // init returns a failure code
        obj->destroy(obj);
        return NULL;
    } // note: Zed prefers to always have an else; this is more javascript-y
    return obj;
}
Example #5
0
// Will also be executed when we do
// NEW(Object, "description)
// When we call the function we automatically create an
// Object called proto used in the implementation
// Always use the NEW() since it will call the Object we 
// create proto, if we would name it differnlty we would fail.
void *Object_new(size_t size, Object proto, char *description)
{
	// setup the default functions in case they aren't set
	// these are function pointers
	if(!proto.init) proto.init = Object_init;
	if(!proto.describe) proto.describe = Object_describe;
	if(!proto.destroy) proto.destroy = Object_destroy;
	if(!proto.attack) proto.attack = Object_attack;
	if(!proto.move) proto.move = Object_move;

	// Alloc space the size of the Object
	Object *el = calloc(1, size);
	*el = proto;

	// strdup will alloc memory, so we need to free it
	el->description = strdup(description);

	if(!el->init(el)) {
		// looks like it didn't initialize properly
		el->destroy(el);
		return NULL;
	} else {
		// good, we made an object of any type
		return el;
	}
}
Example #6
0
void *Object_new(size_t size, Object proto, char *description)
{
  // Setup default functions in case they are not set
  if(!proto.init) proto.init = Object_init;
  if(!proto.describe) proto.describe = Object_describe;
  if(!proto.destroy) proto.destroy = Object_destroy;
  if(!proto.attack) proto.attack = Object_attack;
  if(!proto.move) proto.move = Object_move;

  // Make a struct of one size then point a different pointer at
  // to "cast" it
  Object *el = calloc(1, size);
  *el = proto;

  // Copy the description over
  el->description = strdup(description);

  // Initialize it with whatever init we were given
  if(!el->init(el)) {
    // Then it did not initialize properly
    el->destroy(el);
    return NULL;
  } else {
    // Done we created an object of any type
    return el;
  }
}
Example #7
0
void *Object_new(size_t size, Object proto, char *description)
{
  // Setup default functions in case they aren't set
  if(!proto.init) proto.init = Object_init;
  if(!proto.describe) proto.describe = Object_describe;
  if(!proto.destroy) proto.destroy = Object_destroy;
  if(!proto.attack) proto.attack = Object_attack;
  if(!proto.move) proto.move = Object_move;

  // this seems weird, but we can make a struct of one size,
  // then point a different pointer to it to "cast" it
  Object *el = calloc(1, size);
  *el = proto;

  // copy the description over
  el->description = strdup(description);

  if(!el->init(el)) {
    el->destroy(el);
    return NULL;
  } else {
    // all done, we made an object of any type
    return el;
  }
}
Example #8
0
void *Object_new(size_t size, Object proto, char *description)
{
	//setup the default functions in case they aren't set already
	if(!proto.init) proto.init = Object_init;
	if(!proto.describe) proto.describe = Object_describe;
	if(!proto.destroy) proto.destroy = Object_destroy;
	if(!proto.attack) proto.attack = Object_attack;
	if(!proto.move) proto.move = Object_move;

	//make a struct of one size
	//then point a different pointer at it to "cast" it
	Object *el = calloc(1, size);
	*el = proto;

	//Copy the description over
	el->description = strdup(description);

	//initialize it with whatever init we were given
	if(!el->init(el))
	{
		el->destroy(el);
		return NULL;
	}
	else
	{
		//all done, we made an object of any type
		return el;
	}
}
Example #9
0
void decrementReferenceCount(void * self)
{
    assert(self != NULL);

    /*Cast memory location to Object*/
    Object * object = self;

    /* Decrement reference counter. Destroy if reference count is zero*/
    if(object->referenceCount != NULL) {
        object->referenceCount--;
        if (object->referenceCount < 1) {
            object->destroy(object);
        }
    }
}
Example #10
0
void *ObjectNew(size_t size, Object proto, char *description){
  if(!proto.init) proto.init = ObjectInit;
  if(!proto.describe) proto.describe = ObjectDescribe;
  if(!proto.destroy) proto.destroy = ObjectDestroy;
  if(!proto.attack) proto.attack = ObjectAttack;
  if(!proto.move) proto.move = ObjectMove;

  Object *el = calloc(1, size);
  *el = proto;
 
  el->description = strdup(description);

  if(!el->init(el)){
    el->destroy(el);
    return NULL;
  } else {
    return el;
  }
}
Example #11
0
void *Object_new(size_t size, Object proto, char *description) {
  if(!proto.init) { proto.init = Object_init; }
  if(!proto.describe) { proto.describe = Object_describe; }
  if(!proto.destroy) { proto.destroy = Object_destroy; }
  if(!proto.attack) { proto.attack = Object_attack; }
  if(!proto.move) { proto.move = Object_move; }

  // make a struct of one size
  // then point a different pointer at it to "cast" itHighlightingOnLoad
  Object *el = calloc(1, size);
  *el = proto;

  // copy the desc over
  el->description = strdup(description);

  // initialize it with whatever init we were given
  if(!el->init(el)) {
    el->destroy(el);;
    return NULL;
  } else {
    return el;
  }
}
Example #12
0
void *Object_new(size_t size, Object proto, char *description)
{
  // setup the default functions in case they arent set
  if(!proto.init) proto.init = Object_init;
  if(!proto.describe) proto.describe = Object_describe;
  if(!proto.destroy) proto.destroy = Object_destroy;
  if(!proto.attack) proto.attack = Object_attack;
  if(!proto.move) proto.move = Object_move;

  Object *el = calloc(1,size);
  *el = proto;

  el->description = strdup(description);

  // initialize with whatever int was given
  if(!el->init(el)) {
    // looks like it didn't initializr properly
    el->destroy(el);
    return NULL;
  } else {
    return el;
  }
}
Example #13
0
void *Object_new(size_t size, Object proto, char *description)
{
  if(!proto.init) proto.init = Object_init;
  if(!proto.describe) proto.describe = Object_describe;
  if(!proto.destroy) proto.destroy = Object_destroy;
  if(!proto.attack) proto.attack = Object_attack;
  if(!proto.move) proto.move = Object_move;

  Object *el = calloc(1, size);
  assert(el != NULL);
  *el = proto;

  assert(description != NULL);
  el->description = strdup(description);

  if (!el->init(el)) {
    el->destroy(el);
    return NULL;
  } else {
    assert(el != NULL);
    return el;
  }
}
Example #14
0
void *Object_new(size_t size, Object proto, char *description)
{
    // Set up the default functions if they aren't set
    if (!proto.init) proto.init = Object_init;
    if (!proto.describe) proto.describe = Object_describe;
    if (!proto.destroy) proto.destroy = Object_destroy;
    if (!proto.attack) proto.attack = Object_attack;
    if (!proto.move) proto.move = Object_move;

    // Struct of one size, point to a different pointer at it to "cast" it
    Object *el = calloc(1, size);
    *el = proto;

    // Copy the description over
    el->description = strdup(description);

    // Initialize it with whatever init we were given
    if (!el->init(el)) {
        el->destroy(el);
        return NULL;
    } else {
        return el;
    }
}