Changeset 249


Ignore:
Timestamp:
Mar 1, 2006 1:07:04 PM (18 years ago)
Author:
cxsong
Message:

modified serialization functions

Location:
trunk/src/mesh
Files:
8 edited

Legend:

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

    r244 r249  
    9191
    9292        char * buf = new char[nbytes];
    93         serialize(buf, nbytes);
     93        if (buf != NULL)
     94                serialize(buf);
     95
    9496        return buf;
    9597}
    9698
    9799RP_ERROR
    98 RpElement::serialize(char* buf, int buflen)
    99 {
    100         int nbytes = this->numBytes();
    101 
    102         if (buf == NULL || buflen < (signed)(nbytes+2*sizeof(int))) {
    103                 RpAppendErr("RpNode::serialize: invalid buffer");
     100RpElement::serialize(char* buf)
     101{
     102        if (buf == NULL) {
     103                RpAppendErr("RpNode::serialize: null buffer");
    104104                RpPrintErr();
    105                 return RP_ERR_INVALID_ARRAY;
     105                return RP_ERR_NULL_PTR;
    106106        }
    107107
     
    123123}
    124124
     125//
     126// serialize only the nodes info
     127// returns pointer to next char in buf
     128//
     129char*
     130RpElement::serializeNodes(char* buf)
     131{
     132        int nbytes = m_nodes.size() * sizeof(int);
     133
     134        if (buf == NULL) {
     135                RpAppendErr("RpNode::serialize: null buffer");
     136                RpPrintErr();
     137                return buf;
     138        }
     139
     140        // copy all nodes
     141        memcpy((void *)buf, (void *)&(m_nodes[0]), nbytes);
     142
     143        return (char*)(buf+nbytes);
     144}
     145
    125146RP_ERROR
    126 RpElement::deserialize(const char* buf)
     147RpElement::deserializeNodes(const char* buf, int numNodes)
    127148{
    128149        if (buf == NULL) {
     
    132153        }
    133154
     155        char* ptr = (char*)buf;
     156
     157        for (int i=0; i < numNodes; i++) {
     158                memcpy((void *)&(m_nodes[i]), (void *)ptr, sizeof(int));
     159                ptr += sizeof(int);
     160        }
     161
     162        return RP_SUCCESS;
     163}
     164
     165RP_ERROR
     166RpElement::deserialize(const char* buf)
     167{
     168        if (buf == NULL) {
     169                RpAppendErr("RpElement::deserialize: null buffer");
     170                RpPrintErr();
     171                return RP_ERR_NULL_PTR;
     172        }
     173
    134174        char * ptr = (char*)buf;
    135175
  • trunk/src/mesh/element.h

    r245 r249  
    4343        // id(int), numNodes(int), list of ids(int *)
    4444        char* serialize();
    45         RP_ERROR serialize(char* buf, int buflen);
     45        char* serializeNodes(char* buf);
     46        RP_ERROR serialize(char* buf);
     47        RP_ERROR deserializeNodes(const char* buf, int numNodes);
    4648        RP_ERROR deserialize(const char* buf);
    4749
  • trunk/src/mesh/mesh.cc

    r246 r249  
    253253}
    254254
     255//
     256// total number bytes in mesh object
     257//
     258// NOTE: this doesn't count the id
     259//
     260int RpMesh3d::numBytes()
     261{
     262        // numberOfNodes + all nodes + numberOfElements + numNodesInElement + nodes in elements
     263
     264        int nnie = (*m_elemList)[0].numNodes();
     265        int num = 3*sizeof(int) + m_numNodes*3*sizeof(int) + m_numElements*nnie*sizeof(int);
     266
     267        return num;
     268}
     269
    255270// serialization
    256271char*
    257 RpMesh3d::serialize()
    258 {
    259         return NULL;
     272RpMesh3d::serialize(int& numbytes)
     273{
     274        int nbytes = this->numBytes();
     275
     276        char * buf = new char[nbytes];
     277        if (buf != NULL) {
     278                serialize(buf, nbytes);
     279                numbytes = nbytes;
     280        }
     281
     282        return buf;
    260283}
    261284
     
    263286RpMesh3d::serialize(char* buf, int buflen)
    264287{
     288        int nbytes = this->numBytes();
     289        int nnie = (*m_elemList)[0].numNodes();
     290        int i;
     291
     292        if (buf == NULL || buflen < nbytes) {
     293                RpAppendErr("RpMesh3d::serialize: invalid buffer");
     294                RpPrintErr();
     295                return RP_ERR_INVALID_ARRAY;
     296        }
     297
     298        char *ptr = buf;
     299
     300        // write number of nodes
     301        memcpy((void *)ptr, (void *)&m_numNodes, sizeof(int));
     302        ptr += sizeof(int);
     303
     304        // write all nodes
     305        for (i=0; i < m_numNodes; i++) {
     306                m_nodeList[i].serialize(ptr);
     307                ptr += 3* sizeof(int);
     308        }
     309
     310        // write number of elements
     311        memcpy((void *)ptr, (void *)&m_numElements, sizeof(int));
     312        ptr += sizeof(int);
     313
     314        // write number of nodes in each element
     315        memcpy((void *)ptr, (void *)&nnie, sizeof(int));
     316        ptr += sizeof(int);
     317
     318        // write all elements
     319        for (i=0; i < m_numElements; i++) {
     320                (*m_elemList)[i].serializeNodes(ptr);
     321                ptr += nnie * sizeof(int);
     322        }
    265323
    266324        return RP_SUCCESS;
     
    270328RpMesh3d::deserialize(const char* buf)
    271329{
     330        int i;
     331
     332        if (buf == NULL) {
     333                RpAppendErr("RpMesh3d::deserialize: null buffer");
     334                RpPrintErr();
     335                return RP_ERR_INVALID_ARRAY;
     336        }
     337
     338        char *ptr = (char*)buf;
     339        int nnie;
     340
     341        // read number of nodes
     342        memcpy((void *)&m_numNodes, (void*)ptr, sizeof(int));
     343        ptr += sizeof(int);
     344
     345        // read all nodes
     346        for (i=0; i < m_numNodes; i++) {
     347                m_nodeList[i].deserialize(ptr);
     348                m_nodeList[i].id(i);
     349                ptr += 3* sizeof(int);
     350        }
     351
     352        // read number of elements
     353        memcpy((void *)&m_numElements, (void*)ptr, sizeof(int));
     354        ptr += sizeof(int);
     355
     356        // number of nodes in each element
     357        memcpy((void *)&nnie, (void *)ptr, sizeof(int));
     358        ptr += sizeof(int);
     359
     360        // read all elements
     361        for (i=0; i < m_numElements; i++) {
     362                (*m_elemList)[i].deserializeNodes(ptr, nnie);
     363                (*m_elemList)[i].id(i);
     364                ptr += nnie * sizeof(int);
     365        }
     366
    272367        return RP_SUCCESS;
    273368}
  • trunk/src/mesh/mesh.h

    r246 r249  
    4646
    4747        // serialization
    48         char* serialize();
     48        char* serialize(int& numbytes);
    4949        RP_ERROR serialize(char* buf, int buflen);
    5050        RP_ERROR deserialize(const char* buf);
     
    7070        vector<RpNode3d> m_nodeList; // list of node objects
    7171        vector<RpElement> * m_elemList; // ptr to a list of RpElement objects
     72
     73        int numBytes();
    7274};
    7375
  • trunk/src/mesh/node2d.cc

    r212 r249  
    1616
    1717        char * buf = new char[len];
    18         serialize(buf, len);
     18        serialize(buf);
    1919
    2020        return buf;
     
    3232//
    3333RP_ERROR
    34 RpNode2d::serialize(char* buf, int buflen)
     34RpNode2d::serialize(char* buf)
    3535{
    36         if (buf == NULL || buflen < NodeDim*((signed)sizeof(int))) {
     36        if (buf == NULL) {
    3737                RpAppendErr("RpElement::deserialize: null buffer");
    3838                RpPrintErr();
  • trunk/src/mesh/node2d.h

    r247 r249  
    3838
    3939        char* serialize();
    40         RP_ERROR serialize(char* buf, int buflen);
     40        RP_ERROR serialize(char* buf);
    4141        RP_ERROR deserialize(const char* buf);
    4242
  • trunk/src/mesh/node3d.cc

    r230 r249  
    1515
    1616        char * buf = new char[len];
    17         serialize(buf, len);
     17        serialize(buf);
    1818
    1919        return buf;
     
    2222//
    2323// actual function that does the serialization
     24//     serialization order:
     25//      x y z x y z ...
    2426//
    2527// Input:
     
    3133//
    3234RP_ERROR
    33 RpNode3d::serialize(char* buf, int buflen)
     35RpNode3d::serialize(char* buf)
    3436{
    35         if (buf == NULL || buflen < 3*((signed)sizeof(int))) {
     37        if (buf == NULL) {
    3638                RpAppendErr("RpElement::deserialize: null buffer");
    3739                RpPrintErr();
  • trunk/src/mesh/node3d.h

    r247 r249  
    4545
    4646        char* serialize();
    47         RP_ERROR serialize(char* buf, int buflen);
     47        RP_ERROR serialize(char* buf);
    4848        RP_ERROR deserialize(const char* buf);
    4949
Note: See TracChangeset for help on using the changeset viewer.