source: trunk/src/mesh/grid1d_reg.cc @ 221

Last change on this file since 221 was 185, checked in by cxsong, 18 years ago

moved to rappture/src/mesh

File size: 2.0 KB
Line 
1//
2// class for a regular 1D grid,
3// defined by  a starting point and delta increment.
4//
5//
6
7#include "grid1d_reg.h"
8
9RpGrid1d_reg::RpGrid1d_reg(const char* buf)
10{
11        deserialize(buf);
12}
13
14
15//
16// serialize object
17// 4 Bytes:  number of points in grid
18// 8 Bytes:  start value
19// 8 Bytes:  delta
20//
21// Returns: pointer to byte stream
22// Side effect: caller must release memory
23//
24// TODO: handling 64-bit systems
25//
26char *
27RpGrid1d_reg::serialize()
28{
29        // buffer size
30        char * buf = (char*) malloc(sizeof(int) + 2*sizeof(double) + 2);
31
32        if (buf == NULL) {
33                RpAppendErr("RpGrid1d::serialize: malloc failed");
34                RpPrintErr();
35                return buf;
36        }       
37
38        char* ptr = &(buf[2]); // skip 2 bytes (encode/compression flag)
39
40        memcpy((void*)ptr, (void*)&m_npoints, sizeof(int));
41        ptr += sizeof(int);
42        memcpy((void*)ptr, (void*)&m_start, sizeof(double));
43        ptr += sizeof(double);
44        memcpy((void*)ptr, (void*)&m_delta, sizeof(double));
45
46        return buf;
47}
48
49//
50// deserialize byte stream
51//
52// Returns: error code (RP_SUCCESS or )
53// Side effect: buf is NOT deleted. Caller must release buf.
54//
55int
56RpGrid1d_reg::deserialize(const char* buf)
57{
58        int num;
59        double vstart, vdelta;
60
61        // check for null pointer
62        if (buf == NULL) {
63                //RpAppendErr("RpGrid1d::deserialize: null buf pointer");
64                //RpPrintErr();
65                return RP_ERR_NULL_PTR;
66        }
67
68        buf += 2; // skip 2 bytes (encode/compression) TODO
69
70        memcpy((void*)&num, (void*)&buf, sizeof(int));
71        buf += sizeof(int);
72        memcpy((void*)&vstart, (void*)buf, sizeof(double));
73        buf += sizeof(double);
74        memcpy((void*)&vdelta, (void*)buf, sizeof(double));
75
76        // TODO
77        // if (ByteOrder::IsBigEndian()) {
78        //      flip
79        // }
80       
81        // little endian
82        m_npoints = num;
83        m_start = vstart;
84        m_delta = vdelta;
85
86        return RP_SUCCESS;
87}
88
89//
90// access data elements
91//
92// Returns: pointer to an array of doubles
93//
94double *
95RpGrid1d_reg::data()
96{
97        m_data.resize(m_npoints);
98
99        // expand array
100        for (int i=0; i<m_npoints; i++)
101                m_data[i] = m_start + i * m_delta;
102
103        return &m_data[0];
104}
105
106
107// TODO
108//virtual int xmlPut() { };
109//virtual int xmlGet() { };
110
Note: See TracBrowser for help on using the repository browser.