Debian Codegraph @

gobi-api/Gobi_2011-11-28-1533/Core/ProtocolBuffer.cpp

raw
1 /*===========================================================================
2 FILE:
3 ProtocolBuffer.cpp
4
5 DESCRIPTION:
6 Generic protocol structures and affliated methods
7
8 PUBLIC CLASSES AND METHODS:
9 sProtocolBuffer
10 Simple struct to represent a protocol buffer using a reference counted
11 (shared) buffer, this allows us to use in in several places without
12 copying it once in each place. A few base services are provided
13 but the main purpose is to provide a class to inherit off of for
14 specific protocols
15
16 Copyright (c) 2011, Code Aurora Forum. All rights reserved.
17
18 Redistribution and use in source and binary forms, with or without
19 modification, are permitted provided that the following conditions are met:
20 * Redistributions of source code must retain the above copyright
21 notice, this list of conditions and the following disclaimer.
22 * Redistributions in binary form must reproduce the above copyright
23 notice, this list of conditions and the following disclaimer in the
24 documentation and/or other materials provided with the distribution.
25 * Neither the name of Code Aurora Forum nor
26 the names of its contributors may be used to endorse or promote
27 products derived from this software without specific prior written
28 permission.
29
30
31 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
32 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
35 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
36 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
37 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
39 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 POSSIBILITY OF SUCH DAMAGE.
42 ===========================================================================*/
43
44 //---------------------------------------------------------------------------
45 // Include Files
46 //---------------------------------------------------------------------------
47 #include "StdAfx.h"
48 #include "ProtocolBuffer.h"
49
50 //---------------------------------------------------------------------------
51 // Definitions
52 //---------------------------------------------------------------------------
53
54 /*=========================================================================*/
55 // sProtocolBuffer Methods
56 /*=========================================================================*/
57
58 /*===========================================================================
59 METHOD:
60 sProtocolBuffer (Public Method)
61
62 DESCRIPTION:
63 Constructor (default)
64
65 RETURN VALUE:
66 None
67 ===========================================================================*/
68 sProtocolBuffer::sProtocolBuffer()
69 : mpData( 0 ),
70 mbValid( false )
71 {
72 // Object is currently invalid
73 mTimestamp = EMPTY_TIME;
74 }
75
76 /*===========================================================================
77 METHOD:
78 sProtocolBuffer (Public Method)
79
80 DESCRIPTION:
81 Constructor (parameterized)
82
83 PARAMETERS:
84 pBuffer [ I ] - Shareable buffer that contains the DIAG data
85
86 RETURN VALUE:
87 None
88 ===========================================================================*/
89 sProtocolBuffer::sProtocolBuffer( sSharedBuffer * pBuffer )
90 : mpData( 0 ),
91 mbValid( false )
92 {
93 mTimestamp = EMPTY_TIME;
94
95 time_t rawtime;
96 time( &rawtime );
97 tm * timestamp = localtime( &rawtime );
98 if (timestamp != 0)
99 {
100 mTimestamp = *timestamp;
101 }
102
103 if (mpData != 0 && mpData->IsValid() == true)
104 {
105 mpData->Release();
106 mpData = 0;
107 }
108
109 mpData = pBuffer;
110 if (mpData != 0 && mpData->IsValid() == true)
111 {
112 mpData->AddRef();
113 }
114 else
115 {
116 mpData = 0;
117 }
118
119 // NOTE: Derived classes need to call their own validation method
120 // in their constructors since the override might try to access
121 // data that is not yet in place
122 sProtocolBuffer::Validate();
123 }
124
125 /*===========================================================================
126 METHOD:
127 sProtocolBuffer (Public Method)
128
129 DESCRIPTION:
130 Copy constructor
131
132 PARAMETERS:
133 copyThis [ I ] - sProtocolBuffer to base the new one on
134
135 RETURN VALUE:
136 None
137 ===========================================================================*/
138 sProtocolBuffer::sProtocolBuffer( const sProtocolBuffer & copyThis )
139 : mpData( copyThis.mpData ),
140 mTimestamp( copyThis.mTimestamp ),
141 mbValid( copyThis.mbValid )
142 {
143 // Bump reference count for shared buffer
144 if (mpData != 0 && mpData->IsValid() == true)
145 {
146 mpData->AddRef();
147 }
148 else
149 {
150 mpData = 0;
151 mbValid = false;
152 }
153 }
154
155 /*===========================================================================
156 METHOD:
157 operator = (Public Method)
158
159 DESCRIPTION:
160 Assignment operator
161
162 PARAMETERS:
163 copyThis [ I ] - sProtocolBuffer to base the new one on
164
165 RETURN VALUE:
166 sProtocolBuffer &
167 ===========================================================================*/
168 sProtocolBuffer & sProtocolBuffer::operator = ( const sProtocolBuffer & copyThis )
169 {
170 // Do we already have data?
171 if (mpData != 0)
172 {
173 // Is it different than what we are duplicating?
174 if (mpData != copyThis.mpData)
175 {
176 // Yes, release our current buffer
177 mpData->Release();
178 }
179 }
180
181 mpData = copyThis.mpData;
182 mTimestamp = copyThis.mTimestamp;
183 mbValid = copyThis.mbValid;
184
185 // Bump reference count for shared buffer
186 if (mpData != 0 && mpData->IsValid() == true)
187 {
188 mpData->AddRef();
189 }
190 else
191 {
192 mpData = 0;
193 mbValid = false;
194 }
195
196 return *this;
197 }
198
199 /*===========================================================================
200 METHOD:
201 ~sProtocolBuffer (Public Method)
202
203 DESCRIPTION:
204 Destructor
205
206 RETURN VALUE:
207 None
208 ===========================================================================*/
209 sProtocolBuffer::~sProtocolBuffer()
210 {
211 if (mpData != 0 && mpData->IsValid() == true)
212 {
213 mpData->Release();
214 mpData = 0;
215 }
216 else if (mpData != 0)
217 {
218 ASSERT( 0 );
219 }
220
221 mbValid = false;
222 }