Changeset 281 for trunk/src/mesh


Ignore:
Timestamp:
Mar 7, 2006 1:48:05 PM (18 years ago)
Author:
cxsong
Message:

modified serialization/deserialization, added methods to implement the Serializable class interface.

Location:
trunk/src/mesh
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/mesh/grid1d.cc

    r271 r281  
    44
    55#include "grid1d.h"
    6 #include "byte_order.h"
    76
    87
     
    6867}
    6968
    70 // serialize object
    71 // 1 byte:   tag indicating if data is uuencoded
    72 // 1 byte:   tag indicating compression algorithm (N: NONE, Z: zlib)
    73 // 4 bytes:  number of points in array
    74 // rest:     data array (x x x...)
    75 //
    76 // TODO - handling Endianess
     69//
     70// serialize Grid1d object
     71// 16 bytes:    Header (including RV identifier, version, object type id)
     72//                              e.g., RV-A-FIELD, RV-A-MESH3D
     73// 4 bytes:     N: number of bytes in object name (to follow)
     74// N bytes:     object name (e.g., "output.grid(g1d))"
     75// 4 bytes:     number of points in array
     76// rest:        data array (x x x...)
    7777//
    7878char*
    79 RpGrid1d::serialize(int& nb, RP_ENCODE_ALG encodeFlag,
    80                                 RP_COMPRESSION compressFlag)
    81 {
    82         int npts = m_data.size();
    83         int nbytes = npts*sizeof(DataValType) + sizeof(int) + 2;
     79RpGrid1d::serialize(int& nb)
     80{
    8481        char* buf;
     82        int nbytes = numBytes();
    8583
    8684        // total length = tagEncode + tagCompress + num + array data
     
    9189        }
    9290
    93         serialize(buf, nbytes, encodeFlag, compressFlag);
     91        doSerialize(buf, nbytes);
     92
    9493        nb = nbytes;
    9594
     
    9796}
    9897
     98//
     99// serialize object data in Little Endian byte order
     100//
    99101RP_ERROR
    100 RpGrid1d::serialize(char* buf, int nbytes, RP_ENCODE_ALG encodeFlag,
    101                 RP_COMPRESSION compressFlag)
    102 {
    103         int npts = m_data.size();
    104 
    105         if (buf == NULL || (unsigned)nbytes < (npts*sizeof(DataValType)+sizeof(int)+2)) {
     102RpGrid1d::doSerialize(char* buf, int nbytes)
     103{
     104        // check buffer
     105        if (buf == NULL || nbytes < numBytes()) {
    106106                RpAppendErr("RpGrid1d::serialize: invalid buffer");
    107107                RpPrintErr();
    108108                return RP_ERR_INVALID_ARRAY;
    109109        }
    110        
    111         char* ptr = buf;
    112         ptr[0] = 'N'; // init to no-encoding
    113         switch(encodeFlag) {
    114                 case RP_UUENCODE:
    115                         ptr[0] = 'U';
    116                         break;
    117                 case RP_NO_ENCODING:
    118                 default:
    119                         break;
    120         }
    121 
    122         ptr[1] = 'N'; // init to no compression
    123         switch(compressFlag) {
    124                 case RP_ZLIB:
    125                         ptr[1] = 'Z';
    126                         break;
    127                 case RP_NO_COMPRESSION:
    128                 default:
    129                         break;
    130         }
    131         ptr += 2; // advance pointer
    132 
    133         // TODO encode, compression
    134         // compress()
    135 
    136         // write to stream buffer
    137         //memcpy((void*)ptr, (void*)&npts, sizeof(int));
     110
     111        char * ptr = buf;
     112       
     113        // prepare header
     114        // fill in blanks if version string is shorter than HEADER_SIZE
     115
     116        int len = strlen(Grid1d_current_version);
     117        std::string header(' ', HEADER_SIZE);
     118        header = Grid1d_current_version;
     119        header.append(HEADER_SIZE-len, ' ');
     120
     121        // copy header
     122        const char* cptr = header.c_str();
     123        ByteOrder<char>::OrderCopyArray(cptr, (char*)ptr, HEADER_SIZE);
     124        ptr += HEADER_SIZE;
     125
     126        // copy total number of bytes: nbytes
     127        int* iptr = (int*)ptr;
     128        ByteOrder<int>::OrderCopy(&nbytes, iptr);
     129        ptr += sizeof(int);
     130
     131        // copy length of name
     132        len = m_name.size();
     133        iptr = (int*)ptr;
     134        ByteOrder<int>::OrderCopy(&len, iptr);
     135        ptr += sizeof(int);
     136       
     137        // copy name as chars
     138        cptr = m_name.c_str();
     139        ByteOrder<char>::OrderCopyArray(cptr, (char*)ptr, len);
     140        ptr += len;
     141       
     142        // copy int (number of points)
     143        int npts = m_data.size();
    138144       
    139145        // copy int to byte stream in LE byte order
    140         int* iptr = (int*)ptr;
     146        iptr = (int*)ptr;
    141147        ByteOrder<int>::OrderCopy(&npts, iptr);
    142148        ptr += sizeof(int);
    143149
    144         // copy data to byte stream in LE byte order
    145         //memcpy((void*)ptr, (void*)&(m_data[0]), npts*sizeof(DataValType));
     150        // copy data
    146151       
    147152        DataValType* dptr = (DataValType*)ptr;
     
    154159RpGrid1d::deserialize(const char* buf)
    155160{
    156         int npts;
    157 
    158161        if (buf == NULL) {
    159162                RpAppendErr("RpGrid1d::deserialize: null buf pointer");
     
    162165        }
    163166
    164         // TODO: handle encoding, decompression
    165        
    166         buf += 2; // skip 1st 2 bytes
    167         int * iptr = (int*)buf;
     167        char* ptr = (char*)buf;
     168
     169        // read header
     170        char header[HEADER_SIZE];
     171        ByteOrder<char>::OrderCopyArray(ptr, header, HEADER_SIZE);
     172        filterTrailingBlanks(header, HEADER_SIZE);
     173
     174        ptr += HEADER_SIZE;
     175
     176        // read total number of bytes
     177        int* iptr = (int*)ptr;
     178        int nbytes;
     179        ByteOrder<int>::OrderCopy(iptr, &nbytes);
     180        ptr += sizeof(int);
     181       
     182        if (!strcmp(header, Grid1d_current_version) )
     183                return doDeserialize(ptr);
     184
     185        // deal with older versions
     186        return RP_FAILURE;
     187}
     188
     189//
     190// parse out the buffer,
     191// stripped off the version and total #bytes already.
     192//
     193RP_ERROR
     194RpGrid1d::doDeserialize(const char* buf)
     195{
     196        char* ptr = (char*)buf;
     197        int num;
     198
     199        // copy length of name
     200
     201        ByteOrder<int>::OrderCopy((int*)ptr, &num);
     202
     203        ptr += sizeof(int);
     204       
     205        // copy name as chars
     206        char* cstr = new char[num]; ;
     207        ByteOrder<char>::OrderCopyArray(ptr, cstr, num);
     208        filterTrailingBlanks(cstr, num);
     209        m_name.assign(cstr);
     210
     211        delete cstr;
     212
     213        ptr += num;
    168214
    169215        // read number of points
    170         //memcpy((void*)&npts, (void*)buf, sizeof(int));
    171 
    172         // copy int in buf to nptrs
    173         // use ByteOrder::Copy to swap bytes if on a big endian machine
     216        int* iptr = (int*)ptr;
     217        int npts;
    174218        ByteOrder<int>::OrderCopy(iptr, &npts);
    175         buf += sizeof(int);
     219        ptr += sizeof(int);
    176220
    177221        // set the array to be the right size
     
    179223                m_data.resize(npts);
    180224
    181         // straight copy
    182         //memcpy((void*)&(m_data[0]), (void*)buf, npts*sizeof(DataValType));
    183        
    184225        // copy points array - use ByteOrder copy
    185         DataValType* dptr = (DataValType*)buf;
     226        DataValType* dptr = (DataValType*)ptr;
    186227        ByteOrder<DataValType>::OrderCopyArray(dptr, &(m_data[0]), npts);
    187228
     
    189230}
    190231
     232//
     233// return pointer to data points
     234//
    191235DataValType*
    192 RpGrid1d::data()
     236RpGrid1d::getData()
     237{
     238        return &m_data[0];
     239}
     240
     241//
     242// return pointer to a copy of data points in consecutive memory locations
     243//
     244DataValType*
     245RpGrid1d::getDataCopy()
    193246{
    194247        int npts = numPoints();
     
    207260}
    208261
     262//
     263// mashalling object into xml string
     264//
    209265void
    210266RpGrid1d::xmlString(std::string& textString)
     
    226282}
    227283
     284//
     285// print the xml string from the object
     286//
    228287void
    229288RpGrid1d::print()
     
    233292        xmlString(str);
    234293
     294        printf("object name: %s", m_name.c_str());
    235295        printf("%s", str.c_str());
    236296}
    237297
    238 
    239 // TODO
    240 //int RpGrid1d::xmlPut() { };
    241 //int RpGrid1d::xmlGet() { };
    242 
     298//
     299// set object name from a charactor string
     300//
     301void
     302RpGrid1d::objectName(const char* str)
     303{
     304        m_name = str;
     305}
     306
     307//
     308// get object name
     309//
     310const char* RpGrid1d::objectName()
     311{
     312        return m_name.c_str();
     313}
     314
     315//
     316// get object type
     317//
     318const char* RpGrid1d::objectType()
     319{
     320        return RpObjectTypes[GRID1D];
     321}
     322
     323//
     324// return total number of bytes when mashalling out as bytes
     325//
     326int RpGrid1d::numBytes()
     327{
     328        int nbytes = HEADER_SIZE
     329                + sizeof(int) // total #bytes
     330                + sizeof(int) // #bytes in name
     331                + m_name.size()
     332                + sizeof(int) // #points in grid
     333                + m_data.size() * sizeof(DataValType);
     334
     335        return nbytes;
     336}
     337
  • trunk/src/mesh/grid1d.h

    r279 r281  
    77
    88#include <vector>
     9#include <string>
    910#include "serializable.h"
     11#include "byte_order.h"
    1012#include "util.h"
    11 
    12 #define GRID_1D_VERSION "RV-Grid1d-A"
     13#include "obj_types.h"
    1314
    1415typedef double DataValType;
     
    2223        RpGrid1d(const char* buf); // instantiate with byte stream
    2324
     25        virtual void objectName(const char* str);
     26        virtual const char* objectName();
     27        virtual const char* objectType();
     28
     29        // return number of bytes needed for serialization
     30        virtual int numBytes();
     31
     32        // return number of points in grid
     33        virtual int size() { return numPoints(); };
     34
     35
    2436        // add all points to grid
    2537        RP_ERROR addAllPoints(DataValType* val, int nitems);
     
    2941
    3042        // return number of points in grid
    31         int numPoints() { return m_data.size(); };
    32 
    33         int size() { return numPoints(); };
    34 
    35         // return number of bytes needed for serialization
    36         int numBytes();
    37 
    38         // return array of doubles - user must free memory
    39         // when not needed anymore
    40         DataValType* data();
    4143
    4244        // max num points that can be stored
    4345        int capacity() { return m_data.capacity(); }
    4446
     47
    4548        // change the size of the grid after grid is constructed
    4649        void resize(int npoints) { m_data.resize(npoints); }
    4750
     51        // return pointer to array of points
     52        DataValType* getData();
     53
     54        // return copy of data array - caller must free memory when
     55        // not done
     56        DataValType* getDataCopy();
     57
    4858        // serialize data
    49         // If no input parameters are provided, byte stream will be binary
    50         // without compression.
     59        // returns pointer to buffer
     60        // number of bytes (nbytes) set
    5161        //
    52         char * serialize(int& nbytes, RP_ENCODE_ALG eflag=RP_NO_ENCODING,
    53                          RP_COMPRESSION cflag=RP_NO_COMPRESSION);
     62        virtual char * serialize(int& nbytes);
     63        RP_ERROR doSerialize(char * buf, int nbytes);
    5464
    55         RP_ERROR serialize(char * buf, int nbytes,
    56                         RP_ENCODE_ALG eflag=RP_NO_ENCODING,
    57                         RP_COMPRESSION cflag=RP_NO_COMPRESSION);
    58 
     65        // deserialize data
    5966        RP_ERROR deserialize(const char* buf);
     67        RP_ERROR doDeserialize(const char* buf);
    6068
    6169        void xmlString(std::string& str);
     
    6573        virtual ~RpGrid1d() { };
    6674
    67         // TODO
    68         //virtual int xmlPut() { };
    69         //virtual int xmlGet() { };
     75protected:
     76        int numPoints() { return m_data.size(); };
    7077
    7178private:
     79        std::string m_name; // object name
    7280        vector<DataValType> m_data; // array of doubles
    7381};
Note: See TracChangeset for help on using the changeset viewer.