1717#include < string>
1818
1919#include < stdio.h>
20+ #include < inttypes.h>
21+ #include < iomanip>
2022
2123#include " baseline/MessageHeader.h"
2224#include " baseline/Car.h"
@@ -34,7 +36,7 @@ const char *MAKE = "Honda";
3436const char *MODEL = " Civic VTi" ;
3537const int messageHeaderVersion = 0 ;
3638
37- std::size_t encodeHdr (MessageHeader &hdr, Car &car, char *buffer, int offset, int bufferLength)
39+ std::size_t encodeHdr (MessageHeader &hdr, Car &car, char *buffer, std:: uint64_t offset, std:: uint64_t bufferLength)
3840{
3941 // encode the header
4042 hdr.wrap (buffer, offset, messageHeaderVersion, bufferLength)
@@ -43,24 +45,24 @@ std::size_t encodeHdr(MessageHeader &hdr, Car &car, char *buffer, int offset, in
4345 .schemaId (Car::sbeSchemaId ())
4446 .version (Car::sbeSchemaVersion ());
4547
46- return static_cast <std:: size_t >( hdr.size () );
48+ return hdr.encodedLength ( );
4749}
4850
49- std::size_t decodeHdr (MessageHeader &hdr, char *buffer, int offset, int bufferLength)
51+ std::size_t decodeHdr (MessageHeader &hdr, char *buffer, std:: uint64_t offset, std:: uint64_t bufferLength)
5052{
5153 hdr.wrap (buffer, offset, messageHeaderVersion, bufferLength);
5254
5355 // decode the header
5456 cout << " messageHeader.blockLength=" << hdr.blockLength () << endl;
5557 cout << " messageHeader.templateId=" << hdr.templateId () << endl;
5658 cout << " messageHeader.schemaId=" << hdr.schemaId () << endl;
57- cout << " messageHeader.schemaVersion=" << ( sbe_uint32_t ) hdr.version () << endl;
58- cout << " messageHeader.size =" << hdr.size () << endl;
59+ cout << " messageHeader.schemaVersion=" << hdr.version () << endl;
60+ cout << " messageHeader.encodedLength =" << hdr.encodedLength () << endl;
5961
60- return static_cast <std:: size_t >( hdr.size () );
62+ return hdr.encodedLength ( );
6163}
6264
63- std::size_t encodeCar (Car &car, char *buffer, int offset, int bufferLength)
65+ std::size_t encodeCar (Car &car, char *buffer, std:: uint64_t offset, std:: uint64_t bufferLength)
6466{
6567 car.wrapForEncode (buffer, offset, bufferLength)
6668 .serialNumber (1234 )
@@ -69,9 +71,9 @@ std::size_t encodeCar(Car &car, char *buffer, int offset, int bufferLength)
6971 .code (Model::A)
7072 .putVehicleCode (VEHICLE_CODE);
7173
72- for (int i = 0 , size = car.someNumbersLength (); i < size; i++)
74+ for (std:: uint64_t i = 0 , size = car.someNumbersLength (); i < size; i++)
7375 {
74- car.someNumbers (i, i );
76+ car.someNumbers (i, static_cast <std:: int32_t >(i) );
7577 }
7678
7779 car.extras ().clear ()
@@ -114,36 +116,13 @@ std::size_t encodeCar(Car &car, char *buffer, int offset, int bufferLength)
114116 .next ().mph (60 ).seconds (7 .1f )
115117 .next ().mph (100 ).seconds (11 .8f );
116118
117- car.putMake (MAKE, static_cast < int >(:: strlen (MAKE)) );
118- car.putModel (MODEL, static_cast < int >(:: strlen (MODEL)) );
119+ car.putMake (MAKE, 5 );
120+ car.putModel (MODEL, 9 );
119121 car.putActivationCode (" deadbeef" , 8 );
120122
121- return static_cast <std:: size_t >( car.size () );
123+ return car.encodedLength ( );
122124}
123125
124- const char *format (int value)
125- {
126- static char buffer[1024 ];
127-
128- snprintf (buffer, sizeof (buffer) - 1 , " %d" , value);
129- return buffer;
130- }
131-
132- const char *format (char value)
133- {
134- static char buffer[3 ];
135-
136- snprintf (buffer, sizeof (buffer) - 1 , " %c" , value);
137- return buffer;
138- }
139-
140- const char *format (double value)
141- {
142- static char buffer[80 ];
143-
144- snprintf (buffer, sizeof (buffer) - 1 , " %g" , value);
145- return buffer;
146- }
147126
148127const char *format (BooleanType::Value value)
149128{
@@ -181,122 +160,128 @@ const char *format(bool value)
181160 }
182161}
183162
184- std::size_t decodeCar (Car &car, char *buffer, int offset, int actingBlockLength, int actingVersion, int bufferLength)
163+ std::size_t decodeCar (
164+ Car &car, char *buffer, std::uint64_t offset, std::uint64_t actingBlockLength,
165+ std::uint64_t actingVersion, std::uint64_t bufferLength)
185166{
186167 car.wrapForDecode (buffer, offset, actingBlockLength, actingVersion, bufferLength);
187- std::string sb;
188-
189- sb.append (" \n car.serialNumberId=" ).append (format (Car::serialNumberId ()));
190- sb.append (" \n car.modelYearId=" ).append (format (Car::modelYearId ()));
191- sb.append (" \n car.availableId=" ).append (format (Car::availableId ()));
192- sb.append (" \n car.codeId=" ).append (format (Car::codeId ()));
193- sb.append (" \n car.someNumbersId=" ).append (format (Car::someNumbersId ()));
194- sb.append (" \n car.vehicleCodeId=" ).append (format (Car::vehicleCodeId ()));
195- sb.append (" \n car.extrasId=" ).append (format (Car::extrasId ()));
196- sb.append (" \n car.engineId=" ).append (format (Car::engineId ()));
197- sb.append (" \n car.fuelFiguresId=" ).append (format (Car::fuelFiguresId ()));
198- sb.append (" \n car.fuelFigures.speedId=" ).append (format (Car::FuelFigures::speedId ()));
199- sb.append (" \n car.fuelFigures.mpgId=" ).append (format (Car::FuelFigures::mpgId ()));
200- sb.append (" \n car.fuelFigures.usageDescriptionId=" ).append (format (Car::FuelFigures::usageDescriptionId ()));
201- sb.append (" \n car.performanceFiguresId=" ).append (format (Car::performanceFiguresId ()));
202- sb.append (" \n car.performanceFigures.octaneRatingId=" ).append (format (Car::PerformanceFigures::octaneRatingId ()));
203- sb.append (" \n car.performanceFigures.accelerationId=" ).append (format (Car::PerformanceFigures::accelerationId ()));
204- sb.append (" \n car.performanceFigures.acceleration.mphId=" ).append (format (Car::PerformanceFigures::Acceleration::mphId ()));
205- sb.append (" \n car.performanceFigures.acceleration.secondsId=" ).append (format (Car::PerformanceFigures::Acceleration::secondsId ()));
206- sb.append (" \n car.makeId=" ).append (format (Car::makeId ()));
207- sb.append (" \n car.makeCharacterEncoding=" ).append (Car::makeCharacterEncoding ());
208- sb.append (" \n car.modelId=" ).append (format (Car::modelId ()));
209- sb.append (" \n car.modelCharacterEncoding=" ).append (Car::modelCharacterEncoding ());
210- sb.append (" \n car.activationCodeId=" ).append (format (Car::activationCodeId ()));
211- sb.append (" \n car.activationCodeCharacterEncoding=" ).append (Car::activationCodeCharacterEncoding ());
212-
213- sb.append (" \n " );
214-
215- sb.append (" \n car.serialNumber=" ).append (format ((int )car.serialNumber ()));
216- sb.append (" \n car.modelYear=" ).append (format ((int )car.modelYear ()));
217- sb.append (" \n car.available=" ).append (format (car.available ()));
218- sb.append (" \n car.code=" ).append (format (car.code ()));
219-
220- sb.append (" \n car.someNumbers=" );
221- for (int i = 0 , size = Car::someNumbersLength (); i < size; i++)
168+
169+ std::cout.setf (std::ios::fixed);
170+
171+ std::cout << " \n car.serialNumberId=" << Car::serialNumberId ();
172+ std::cout << " \n car.modelYearId=" << Car::modelYearId ();
173+ std::cout << " \n car.availableId=" << Car::availableId ();
174+ std::cout << " \n car.codeId=" << Car::codeId ();
175+ std::cout << " \n car.someNumbersId=" << Car::someNumbersId ();
176+ std::cout << " \n car.vehicleCodeId=" << Car::vehicleCodeId ();
177+ std::cout << " \n car.extrasId=" << Car::extrasId ();
178+ std::cout << " \n car.engineId=" << Car::engineId ();
179+ std::cout << " \n car.fuelFiguresId=" << Car::fuelFiguresId ();
180+ std::cout << " \n car.fuelFigures.speedId=" << Car::FuelFigures::speedId ();
181+ std::cout << " \n car.fuelFigures.mpgId=" << Car::FuelFigures::mpgId ();
182+ std::cout << " \n car.fuelFigures.usageDescriptionId=" << Car::FuelFigures::usageDescriptionId ();
183+ std::cout << " \n car.performanceFiguresId=" << Car::performanceFiguresId ();
184+ std::cout << " \n car.performanceFigures.octaneRatingId=" << Car::PerformanceFigures::octaneRatingId ();
185+ std::cout << " \n car.performanceFigures.accelerationId=" << Car::PerformanceFigures::accelerationId ();
186+ std::cout << " \n car.performanceFigures.acceleration.mphId=" << Car::PerformanceFigures::Acceleration::mphId ();
187+ std::cout << " \n car.performanceFigures.acceleration.secondsId=" << Car::PerformanceFigures::Acceleration::secondsId ();
188+ std::cout << " \n car.makeId=" << Car::makeId ();
189+ std::cout << " \n car.makeCharacterEncoding=" << Car::makeCharacterEncoding ();
190+ std::cout << " \n car.modelId=" << Car::modelId ();
191+ std::cout << " \n car.modelCharacterEncoding=" << Car::modelCharacterEncoding ();
192+ std::cout << " \n car.activationCodeId=" << Car::activationCodeId ();
193+ std::cout << " \n car.activationCodeCharacterEncoding=" << Car::activationCodeCharacterEncoding ();
194+
195+ std::cout << " \n " ;
196+
197+ std::cout << " \n car.serialNumber=" << car.serialNumber ();
198+ std::cout << " \n car.modelYear=" << car.modelYear ();
199+ std::cout << " \n car.available=" << format (car.available ());
200+ std::cout << " \n car.code=" << format (car.code ());
201+
202+ std::cout << " \n car.someNumbers=" ;
203+ std::string separator (" " );
204+ for (std::uint64_t i = 0 ; i < Car::someNumbersLength (); i++)
222205 {
223- sb.append (format (car.someNumbers (i))).append (" , " );
206+ std::cout << separator << car.someNumbers (i);
207+ separator = " , " ;
224208 }
225209
226- sb. append ( " \n car.vehicleCodeLength= " ). append ( format (( int )car. vehicleCodeLength ())) ;
227- sb. append ( " \n car.vehicleCode= " ) ;
228- for (int i = 0 , size = Car::vehicleCodeLength (); i < size ; i++)
210+ std::cout << " \n car.vehicleCode= " ;
211+ separator = " " ;
212+ for (std:: uint64_t i = 0 ; i < Car::vehicleCodeLength (); i++)
229213 {
230- sb.append (format ((char )car.vehicleCode (i)));
214+ std::cout << separator << car.vehicleCode (i);
215+ separator = " , " ;
231216 }
232217
233218 OptionalExtras &extras = car.extras ();
234- sb. append ( " \n car.extras.cruiseControl=" ). append ( format (extras.cruiseControl () ));
235- sb. append ( " \n car.extras.sportsPack=" ). append ( format (extras.sportsPack () ));
236- sb. append ( " \n car.extras.sunRoof=" ). append ( format (extras.sunRoof () ));
219+ std::cout << " \n car.extras.cruiseControl=" << format (extras.cruiseControl ());
220+ std::cout << " \n car.extras.sportsPack=" << format (extras.sportsPack ());
221+ std::cout << " \n car.extras.sunRoof=" << format (extras.sunRoof ());
237222
238- sb. append ( " \n car.discountedModel=" ). append ( format (car.discountedModel () ));
223+ std::cout << " \n car.discountedModel=" << format (car.discountedModel ());
239224
240225 Engine &engine = car.engine ();
241- sb.append (" \n car.engine.capacity=" ).append (format ((int )engine.capacity ()));
242- sb.append (" \n car.engine.numCylinders=" ).append (format ((int )engine.numCylinders ()));
243- sb.append (" \n car.engine.maxRpm=" ).append (format ((int )engine.maxRpm ()));
244- sb.append (" \n car.engine.manufacturerCodeLength=" ).append (format ((int )engine.manufacturerCodeLength ()));
245- sb.append (" \n car.engine.manufacturerCode=" );
246- for (int i = 0 , size = Engine::manufacturerCodeLength (); i < size; i++)
226+ std::cout << " \n car.engine.capacity=" << (int )engine.capacity ();
227+ std::cout << " \n car.engine.numCylinders=" << (int )engine.numCylinders ();
228+ std::cout << " \n car.engine.maxRpm=" << (int )engine.maxRpm ();
229+ std::cout << " \n car.engine.manufacturerCodeLength=" << (int )engine.manufacturerCodeLength ();
230+ std::cout << " \n car.engine.manufacturerCode=" ;
231+ separator = " " ;
232+ for (std::uint64_t i = 0 ; i < Engine::manufacturerCodeLength (); i++)
247233 {
248- sb.append (format ((char )engine.manufacturerCode (i)));
234+ std::cout << separator << engine.manufacturerCode (i);
235+ separator = " , " ;
249236 }
250237
251238 char tmp[1024 ];
252- int bytesCopied = engine.getFuel (tmp, sizeof (tmp));
253- sb. append ( " \n car.engine.fuelLength=" ). append ( format ( bytesCopied)) ;
254- sb. append ( " \n car.engine.fuel=" ). append (tmp, bytesCopied);
239+ std:: uint64_t bytesCopied = engine.getFuel (tmp, sizeof (tmp));
240+ std::cout << " \n car.engine.fuelLength=" << bytesCopied;
241+ std::cout << " \n car.engine.fuel=" << std::string (tmp, bytesCopied);
255242
256243 Car::FuelFigures &fuelFigures = car.fuelFigures ();
257244 while (fuelFigures.hasNext ())
258245 {
259246 fuelFigures.next ();
260- sb. append ( " \n car.fuelFigures.speed=" ). append ( format (( int )fuelFigures.speed ()) );
261- sb. append ( " \n car.fuelFigures.mpg=" ). append ( format (( double )fuelFigures.mpg ()) );
247+ std::cout << " \n car.fuelFigures.speed=" << ( int )fuelFigures.speed ();
248+ std::cout << " \n car.fuelFigures.mpg=" << std::setprecision ( 1 ) << ( double )fuelFigures.mpg ();
262249
263- sb. append ( " \n car.fuelFigures.usageDescriptionLength= " ). append ( format (( int ) fuelFigures.usageDescriptionLength ()) );
264- bytesCopied = fuelFigures.getUsageDescription (tmp, sizeof (tmp) );
265- sb. append ( " \n car.fuelFigures.usageDescription=" ). append (tmp, bytesCopied) ;
250+ std::string usageDesc = fuelFigures.getUsageDescriptionAsString ( );
251+ std::cout << " \n car. fuelFigures.usageDescriptionLength= " << usageDesc. length ( );
252+ std::cout << " \n car.fuelFigures.usageDescription=" << usageDesc ;
266253 }
267254
268255 Car::PerformanceFigures &performanceFigures = car.performanceFigures ();
269256 while (performanceFigures.hasNext ())
270257 {
271258 performanceFigures.next ();
272- sb. append ( " \n car.performanceFigures.octaneRating=" ). append ( format (( int )performanceFigures.octaneRating ()) );
259+ std::cout << " \n car.performanceFigures.octaneRating=" << (std:: uint64_t )performanceFigures.octaneRating ();
273260
274261 Car::PerformanceFigures::Acceleration &acceleration = performanceFigures.acceleration ();
275262 while (acceleration.hasNext ())
276263 {
277264 acceleration.next ();
278- sb. append ( " \n car.performanceFigures.acceleration.mph=" ). append ( format (( int ) acceleration.mph ()) );
279- sb. append ( " \n car.performanceFigures.acceleration.seconds=" ). append ( format (( double ) acceleration.seconds ()) );
265+ std::cout << " \n car.performanceFigures.acceleration.mph=" << acceleration.mph ();
266+ std::cout << " \n car.performanceFigures.acceleration.seconds=" << std::setprecision ( 1 ) << acceleration.seconds ();
280267 }
281268 }
282269
283270 bytesCopied = car.getMake (tmp, sizeof (tmp));
284- sb. append ( " \n car.makeLength=" ). append ( format (( int ) bytesCopied)) ;
285- sb. append ( " \n car.make=" ). append (tmp, bytesCopied);
271+ std::cout << " \n car.makeLength=" << bytesCopied;
272+ std::cout << " \n car.make=" << std::string (tmp, bytesCopied);
286273
287274 bytesCopied = car.getModel (tmp, sizeof (tmp));
288- sb. append ( " \n car.modelLength=" ). append ( format (( int ) bytesCopied)) ;
289- sb. append ( " \n car.model=" ). append (tmp, bytesCopied);
275+ std::cout << " \n car.modelLength=" << bytesCopied;
276+ std::cout << " \n car.model=" << std::string (tmp, bytesCopied);
290277
291278 bytesCopied = car.getActivationCode (tmp, sizeof (tmp));
292- sb.append (" \n car.activationCodeLength=" ).append (format ((int )bytesCopied));
293- sb.append (" \n car.activationCode=" ).append (tmp, bytesCopied);
294-
295- sb.append (" \n car.size=" ).append (format ((int )car.size ()));
279+ std::cout << " \n car.activationCodeLength=" << bytesCopied;
280+ std::cout << " \n car.activationCode=" << std::string (tmp, bytesCopied);
296281
297- cout << sb << endl ;
282+ std:: cout << " \n car.encodedLength= " << ( int )car. encodedLength () << " \n " ;
298283
299- return static_cast <std:: size_t >( car.size () );
284+ return car.encodedLength ( );
300285}
301286
302287int main (int argc, const char * argv[])
@@ -306,12 +291,12 @@ int main(int argc, const char* argv[])
306291 Car car;
307292
308293 std::size_t encodeHdrLength = encodeHdr (hdr, car, buffer, 0 , sizeof (buffer));
309- std::size_t encodeMsgLength = encodeCar (car, buffer, hdr.size (), sizeof (buffer));
294+ std::size_t encodeMsgLength = encodeCar (car, buffer, hdr.encodedLength (), sizeof (buffer));
310295
311296 cout << " Encoded Lengths are " << encodeHdrLength << " + " << encodeMsgLength << endl;
312297
313298 std::size_t decodeHdrLength = decodeHdr (hdr, buffer, 0 , sizeof (buffer));
314- std::size_t decodeMsgLength = decodeCar (car, buffer, hdr.size (), hdr.blockLength (), hdr.version (), sizeof (buffer));
299+ std::size_t decodeMsgLength = decodeCar (car, buffer, hdr.encodedLength (), hdr.blockLength (), hdr.version (), sizeof (buffer));
315300
316301 cout << " Decoded Lengths are " << decodeHdrLength << " + " << decodeMsgLength << endl;
317302
0 commit comments