/*
 *   COMPONENT_NAME: somx
 *
 *   ORIGINS: 27
 *
 *
 *   25H7912  (C)  COPYRIGHT International Business Machines Corp. 1992,1996  
 *   All Rights Reserved
 *   Licensed Materials - Property of IBM
 *   US Government Users Restricted Rights - Use, duplication or
 *   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
 */

#ifndef lint
static char *sccsid = "@(#) 2.13.1.1 src/somples.os2/somd/c/animal/danimal.c, somples.somd, som3.0 12/2/96 16:59:22 [12/6/96 09:29:13]";
#endif

/*
 *
 * DISCLAIMER OF WARRANTIES.
 * The following [enclosed] code is sample code created by IBM
 * Corporation. This sample code is not part of any standard or IBM
 * product and is provided to you solely for the purpose of assisting
 * you in the development of your applications.  The code is provided
 * "AS IS". IBM MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT
 * NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE, REGARDING THE FUNCTION OR PERFORMANCE OF
 * THIS CODE.  IBM shall not be liable for any damages arising out of
 * your use of the sample code, even if they have been advised of the
 * possibility of such damages.
 *
 * DISTRIBUTION.
 * This sample code can be freely distributed, copied, altered, and
 * incorporated into other software, provided that it bears the above
 * Copyright notice and DISCLAIMER intact.
 */
/*
 *  This file was generated by the SOM Compiler and Emitter Framework.
 *  Generated using: 
 *      SOM Emitter emitctm: 2.42
 */

#ifndef SOM_Module_danimal_Source
#define SOM_Module_danimal_Source
#endif
#define dAnimal_Class_Source

#include <stdio.h>
#include <string.h>
#include "dloc.h"
#include "danimal.ih"


/*
 * Name of animal.
 */

SOM_Scope void  SOMLINK _set_name(dAnimal somSelf,  Environment *ev, 
                                  string name)
{
    dAnimalData *somThis = dAnimalGetData(somSelf);
    dAnimalMethodDebug("dAnimal","_set_name");

    if (_name) SOMFree(_name);
    _name = (string) SOMMalloc(strlen(name) + 1);
    strcpy(_name, name);

}

/*
 * Type of animal.
 */

SOM_Scope void  SOMLINK _set_type(dAnimal somSelf,  Environment *ev, 
                                  string type)
{
    dAnimalData *somThis = dAnimalGetData(somSelf);
    dAnimalMethodDebug("dAnimal","_set_type");

    if (_type) SOMFree(_type);
    _type = (string) SOMMalloc(strlen(type) + 1);
    strcpy(_type, type);

}

/*
 * Food animal likes to eat.
 */

SOM_Scope void  SOMLINK _set_food(dAnimal somSelf,  Environment *ev, 
                                  string food)
{
    dAnimalData *somThis = dAnimalGetData(somSelf);
    dAnimalMethodDebug("dAnimal","_set_food");

    if (_food) SOMFree(_food);
    _food = (string) SOMMalloc(strlen(food) + 1);
    strcpy(_food, food);

}

/*
 * Work area for display method.
 */

SOM_Scope void  SOMLINK _set_buff(dAnimal somSelf,  Environment *ev, 
                                  string buff)
{
    dAnimalData *somThis = dAnimalGetData(somSelf);
    dAnimalMethodDebug("dAnimal","_set_buff");

    if (_buff) SOMFree(_buff);
    _buff = (string) SOMMalloc(strlen(buff) + 1);
    strcpy(_buff, buff);

}

/*
 * Returns description of animal.
 */

SOM_Scope string  SOMLINK display(dAnimal somSelf,  Environment *ev)
{
    string bp;

    dAnimalData *somThis = dAnimalGetData(somSelf);
    dAnimalMethodDebug("dAnimal","display");

    bp = _buff;
    sprintf(bp, "\nI am a %s.\n", _type);
    bp += strlen(bp);
    sprintf(bp, "My name is %s.\n", _name);
    bp += strlen(bp);
    sprintf(bp, "I like to eat %s.\n", _food);
    bp += strlen(bp);
    sprintf(bp, "I live in %s, %s.\n", __get_city(_loc, ev), __get_state(_loc, ev));
    return _buff;
}

SOM_Scope void SOMLINK somDefaultInit(dAnimal somSelf, somInitCtrl* ctrl)
{
    dAnimalData *somThis; /* set in BeginInitializer */
    Environment *ev;
    somInitCtrl globalCtrl;
    somBooleanVector myMask;
    dAnimalMethodDebug("dAnimal","somDefaultInit");
    dAnimal_BeginInitializer_somDefaultInit;

    dAnimal_Init_SOMObject_somDefaultInit(somSelf, ctrl);

    ev = somGetGlobalEnvironment();
    __set_name(somSelf, ev, "unknown name");
    __set_type(somSelf, ev, "unknown type");
    __set_food(somSelf, ev, "unknown food");
    _loc = dLocationNew();
    _buff = SOMMalloc(2048);

}


SOM_Scope void SOMLINK somDestruct(dAnimal somSelf, octet doFree, 
                                   somDestructCtrl* ctrl)
{
    dAnimalData *somThis; /* set in BeginDestructor */
    somDestructCtrl globalCtrl;
    somBooleanVector myMask;
    dAnimalMethodDebug("dAnimal","somDestruct");
    dAnimal_BeginDestructor;

    if (_name) SOMFree(_name);
    if (_type) SOMFree(_type);
    if (_food) SOMFree(_food);
    if (_buff) SOMFree(_buff);
    _somFree(_loc);

    dAnimal_EndDestructor;
}



/*
 * Creates and returns an instance of the input class name.
 * The input class name must be a subclass of dAnimal or an exception
 * will be raised.
 */

SOM_Scope dAnimal  SOMLINK create_animal(AnimalFactory somSelf, 
                                          Environment *ev, string className)
{
    SOMClass class;
    dAnimal newObj;
    somId temp_id;    

    /* AnimalFactoryData *somThis = AnimalFactoryGetData(somSelf); */
    AnimalFactoryMethodDebug("AnimalFactory","create_animal");

    /* get class object for input class name */
    temp_id = somIdFromString(className);
    class = _somFindClass(SOMClassMgrObject, temp_id, 0, 0);
    SOMFree(temp_id);

    /* verify that class is a subclass of dAnimal */
    if (_somDescendedFrom(class, _dAnimal)) {
        somPrintf("Creating instance of dAnimal, class = %s\n", className);
        newObj = _somNew(class);
    }
    else {
/*        exception = (InvalidClassName *) SOMMalloc(sizeof(InvalidClassName));*/
	somSetException(ev, USER_EXCEPTION, ex_InvalidClassName, InvalidClassName);
    }

    return newObj;
}
