/****************************************************************************
 *   gobject.h : 
 *   		 Abstract base class for GFC objects that can be persistant.
 *
 *   Change Log :
 *		 Feb. 26 1998 Initial version,  Liujian Qian
 *
 *   $Id: gobject.h,v 1.18 1999/02/14 03:14:24 qian Exp $
 ***************************************************************************/
#ifndef _GOBJECT_H_
#define _GOBJECT_H_
#include <string.h>
#include "gdatapipe.h"
#include "gcxx.h"

/**
 * @internal
 *  for universal object ID
 */
struct GOid
{
    uint8	v[8];	
    GOid() {for(int i=0; i<8; i++) v[i] = 0;}
};

/**
 * Abstract base class for all the GFC serializable classes.
   
 */
class GObject 
{
    int		ref_count;	//not used
    GOid	oid;		//not used

public:

    /**
     */
    virtual NOP		~GObject() {  }
    /**
     * initialize the object.
     */
    virtual Result  	init		(void) {return GOk;}    

    /**
     * returns the persistent data size.
     * Persistent size of an object means the sum of
     * all data members that need to be stored to disk
     * in order to restore the current object state at
     * a later time. 
     */
    virtual uint32   	getPSize	() const {return 0;}      

    /**
     * inputs member data from an ascii stream
     */
    virtual Result  	input     (const char*  ) {return GOk;} 

    /**
     * output interesting member data to an ASCII stream.
     * the returning string "out" is GFC's property and
     * should not be freed; copy this string if necessary.
     */
    virtual Result 	output    (char*& ) {return GOk;} 

    /**
     * pack data into a binary data pipe; this is the serializing
     * function that makes the data member of interest persistent.
     */
    virtual Result	pack	   (DataPipe& ) {return GOk;}; 

    /**
     * unpack data from a binary data pipe; 
     * the pipe may contain 
     * data of a record  just returned from underlying DBMS.
     * this will gurantee lossless restore of the data members 
     * previoulsy {\bf pack}'ed.
     */
    virtual Result      unpack      (DataPipe& ) {return GOk; } 

    /**
     * set the object Identifier; the oid is an 8-byte array.
     * caller should have taken care of the byte-order; this
     * method merely memcpys parameter array byte-wise.
     */
    void		setOid	    (const char* o){ }   

    /**
     * return the object identifier as an 8-byte array.
     */
    const char*	        getOid	    (void) const {return 0;}
    
    /**
     * serializes object 'a' into the pipe.
     * Starts at pipe's current position 
     * and advance the cursor accordingly. This function
     * will call the correct version of {\bf pack()}
     * method based on the dynamic type of the object
     * being passed in. The user of the data pipe doesn't
     * have to concern the exact type of the ADT object
     * being stored into the pipe
     */
    friend DataPipe& 	 operator<<(DataPipe& p, GObject& a);

    /**
     * fetchs serialized memeber data from the pipe's current
     * cursor; and advance the cursor accordingly. This
     * function calls the correct version of <b> unpack() </b>
     * method based on the dynamci type of the object being
     * passed in. Note this and <b> store() </b> are 
     * global functions.
     */
    friend DataPipe& 	 operator>>(DataPipe& p, GObject& a);
};



/* these are deprecated. Use swab* macros instead */
inline void flipShort(void*);
inline void flipInt(void* );
inline void flipDouble(void* );
inline void flipShort(short*, short*);
inline void flipInt(int*, int*);
inline void flipDouble(double*, double*);


// swap the two bytes of an short integer
inline void flipShort(void* i)
{
    *(uint16*)i = swab16(*(uint16*)i);
}

inline void flipShort(short *in, short* out)
{
    *out = swab16(*in);
}

// swap the byte order of an integer
inline void flipInt(void* i)
{
    *(uint32*)i = swab32(*(uint32*)i);
}

inline void flipInt(int* in, int* out)
{
    *out = swab32(*in);
}

// swap the byte order of an double
inline void flipDouble(void* i)
{
    unsigned char t, *j=(unsigned char*)i;
    t = j[7]; j[7] = j[0]; j[0] = t;
    t = j[6]; j[6] = j[1]; j[1] = t;
    t = j[5]; j[5] = j[2]; j[2] = t;
    t = j[4]; j[4] = j[3]; j[3] = t;
    //*(uint64*)i = swab64(*(uint64*)i); 
}

inline void flipDouble(double* in, double* out)
{
    register char* i=(char*)in, *o=(char*)out;
    o[0]=i[7],o[1]=i[6],o[2]=i[5],o[3]=i[4];
    o[4]=i[3],o[5]=i[2],o[6]=i[1],o[7]=i[0];

    //*(uint64*)out = swab64(*(uint64*)in);  
}

struct GEndian
{
    static int8	big;
};



#endif





Documentation generated by lqian@lqian-sun on Wed Jul 14 09:36:10 EDT 1999