std::size_t encodeCar(Car &car, char *buffer, std::uint64_t offset, std::uint64_t bufferLength) { car.wrapForEncode(buffer, offset, bufferLength) .serialNumber(1234) .modelYear(2013) .available(BooleanType::T) .code(Model::A) .putVehicleCode(VEHICLE_CODE); for (std::uint64_t i = 0, size = car.someNumbersLength(); i < size; i++) { car.someNumbers(i, static_cast<std::int32_t>(i)); } car.extras().clear() .cruiseControl(true) .sportsPack(true) .sunRoof(false); car.engine() .capacity(2000) .numCylinders((short)4) .putManufacturerCode(MANUFACTURER_CODE); Car::FuelFigures& fuelFigures = car.fuelFiguresCount(3); fuelFigures .next().speed(30).mpg(35.9f) .putUsageDescription("Urban Cycle", 11); fuelFigures .next().speed(55).mpg(49.0f) .putUsageDescription("Combined Cycle", 14); fuelFigures .next().speed(75).mpg(40.0f) .putUsageDescription("Highway Cycle", 13); Car::PerformanceFigures &performanceFigures = car.performanceFiguresCount(2); performanceFigures.next() .octaneRating((short)95) .accelerationCount(3) .next().mph(30).seconds(4.0f) .next().mph(60).seconds(7.5f) .next().mph(100).seconds(12.2f); performanceFigures.next() .octaneRating((short)99) .accelerationCount(3) .next().mph(30).seconds(3.8f) .next().mph(60).seconds(7.1f) .next().mph(100).seconds(11.8f); car.putMake(MAKE, 5) .putModel(MODEL, 9) .putActivationCode("deadbeef", 8); return car.encodedLength(); }
TEST_F(CodeGenTest, shouldBeAbleToUseStdStringMethodsForEncode) { std::string vehicleCode(VEHICLE_CODE, Car::vehicleCodeLength()); std::string usageDesc1(FUEL_FIGURES_1_USAGE_DESCRIPTION, FUEL_FIGURES_1_USAGE_DESCRIPTION_LENGTH); std::string usageDesc2(FUEL_FIGURES_2_USAGE_DESCRIPTION, FUEL_FIGURES_2_USAGE_DESCRIPTION_LENGTH); std::string usageDesc3(FUEL_FIGURES_3_USAGE_DESCRIPTION, FUEL_FIGURES_3_USAGE_DESCRIPTION_LENGTH); std::string make(MAKE, MAKE_LENGTH); std::string model(MODEL, MODEL_LENGTH); std::string activationCode(ACTIVATION_CODE, ACTIVATION_CODE_LENGTH); char buffer[BUFFER_LEN]; std::uint64_t baseOffset = MessageHeader::encodedLength(); Car car; car.wrapForEncode(buffer, baseOffset, sizeof(buffer)); car.putVehicleCode(vehicleCode); car.fuelFiguresCount(FUEL_FIGURES_COUNT) .next().putUsageDescription(usageDesc1) .next().putUsageDescription(usageDesc2) .next().putUsageDescription(usageDesc3); Car::PerformanceFigures &perfFigs = car.performanceFiguresCount(PERFORMANCE_FIGURES_COUNT); perfFigs.next() .accelerationCount(ACCELERATION_COUNT).next().next().next(); perfFigs.next() .accelerationCount(ACCELERATION_COUNT).next().next().next(); car.putMake(make) .putModel(model) .putActivationCode(activationCode); EXPECT_EQ(car.encodedLength(), expectedCarSize); EXPECT_EQ(std::string(buffer + baseOffset + offsetVehicleCode, VEHICLE_CODE_LENGTH), vehicleCode); EXPECT_EQ(*(std::uint8_t *)(buffer + baseOffset + offsetUsageDesc1Length), FUEL_FIGURES_1_USAGE_DESCRIPTION_LENGTH); EXPECT_EQ(std::string(buffer + baseOffset + offsetUsageDesc1Data, FUEL_FIGURES_1_USAGE_DESCRIPTION_LENGTH), usageDesc1); EXPECT_EQ(*(std::uint8_t *)(buffer + baseOffset + offsetUsageDesc2Length), FUEL_FIGURES_2_USAGE_DESCRIPTION_LENGTH); EXPECT_EQ(std::string(buffer + baseOffset + offsetUsageDesc2Data, FUEL_FIGURES_2_USAGE_DESCRIPTION_LENGTH), usageDesc2); EXPECT_EQ(*(std::uint8_t *)(buffer + baseOffset + offsetUsageDesc3Length), FUEL_FIGURES_3_USAGE_DESCRIPTION_LENGTH); EXPECT_EQ(std::string(buffer + baseOffset + offsetUsageDesc3Data, FUEL_FIGURES_3_USAGE_DESCRIPTION_LENGTH), usageDesc3); EXPECT_EQ(*(std::uint8_t *)(buffer + baseOffset + offsetMakeLength), MAKE_LENGTH); EXPECT_EQ(std::string(buffer + baseOffset + offsetMakeData, MAKE_LENGTH), make); EXPECT_EQ(*(std::uint8_t *)(buffer + baseOffset + offsetModelLength), MODEL_LENGTH); EXPECT_EQ(std::string(buffer + baseOffset + offsetModelData, MODEL_LENGTH), model); EXPECT_EQ(*(std::uint8_t *)(buffer + baseOffset + offsetActivationCodeLength), ACTIVATION_CODE_LENGTH); EXPECT_EQ(std::string(buffer + baseOffset + offsetActivationCodeData, ACTIVATION_CODE_LENGTH), activationCode); }
void encodeCar(Car &car, char *buffer, int offset) { car.wrapForEncode(buffer, offset) .serialNumber(1234) .modelYear(2013) .available(BooleanType::TRUE) .code(Model::A) .putVehicleCode(VEHICLE_CODE); for (int i = 0, size = car.someNumbersLength(); i < size; i++) { car.someNumbers(i, i); } car.extras().clear() .cruiseControl(true) .sportsPack(true) .sunRoof(false); car.engine() .capacity(2000) .numCylinders((short)4) .putManufacturerCode(MANUFACTURER_CODE); car.fuelFiguresCount(3) .next().speed(30).mpg(35.9f) .next().speed(55).mpg(49.0f) .next().speed(75).mpg(40.0f); Car::PerformanceFigures &performanceFigures = car.performanceFiguresCount(2); performanceFigures.next() .octaneRating((short)95) .accelerationCount(3) .next().mph(30).seconds(4.0f) .next().mph(60).seconds(7.5f) .next().mph(100).seconds(12.2f); performanceFigures.next() .octaneRating((short)99) .accelerationCount(3) .next().mph(30).seconds(3.8f) .next().mph(60).seconds(7.1f) .next().mph(100).seconds(11.8f); car.putMake(MAKE, strlen(MAKE)); car.putModel(MODEL, strlen(MODEL)); }
virtual std::uint64_t encodeHdrAndCar() { MessageHeader hdr; Car car; hdr.wrap(m_buffer, 0, 0, sizeof(m_buffer)) .blockLength(Car::sbeBlockLength()) .templateId(Car::sbeTemplateId()) .schemaId(Car::sbeSchemaId()) .version(Car::sbeSchemaVersion()); car.wrapForEncode(m_buffer, hdr.encodedLength(), sizeof(m_buffer)) .serialNumber(SERIAL_NUMBER) .modelYear(MODEL_YEAR) .available(AVAILABLE) .code(CODE) .putVehicleCode(VEHICLE_CODE); for (std::uint64_t i = 0; i < Car::someNumbersLength(); i++) { car.someNumbers(i, static_cast<std::int32_t>(i)); } car.extras().clear() .cruiseControl(CRUISE_CONTROL) .sportsPack(SPORTS_PACK) .sunRoof(SUNROOF); car.engine() .capacity(engineCapacity) .numCylinders(engineNumCylinders) .putManufacturerCode(MANUFACTURER_CODE); Car::FuelFigures& fuelFigures = car.fuelFiguresCount(FUEL_FIGURES_COUNT); fuelFigures .next().speed(fuel1Speed).mpg(fuel1Mpg); fuelFigures.putUsageDescription( FUEL_FIGURES_1_USAGE_DESCRIPTION, static_cast<int>(strlen(FUEL_FIGURES_1_USAGE_DESCRIPTION))); fuelFigures .next().speed(fuel2Speed).mpg(fuel2Mpg); fuelFigures.putUsageDescription( FUEL_FIGURES_2_USAGE_DESCRIPTION, static_cast<int>(strlen(FUEL_FIGURES_2_USAGE_DESCRIPTION))); fuelFigures .next().speed(fuel3Speed).mpg(fuel3Mpg); fuelFigures.putUsageDescription( FUEL_FIGURES_3_USAGE_DESCRIPTION, static_cast<int>(strlen(FUEL_FIGURES_3_USAGE_DESCRIPTION))); Car::PerformanceFigures &perfFigs = car.performanceFiguresCount(PERFORMANCE_FIGURES_COUNT); perfFigs.next() .octaneRating(perf1Octane) .accelerationCount(ACCELERATION_COUNT) .next().mph(perf1aMph).seconds(perf1aSeconds) .next().mph(perf1bMph).seconds(perf1bSeconds) .next().mph(perf1cMph).seconds(perf1cSeconds); perfFigs.next() .octaneRating(perf2Octane) .accelerationCount(ACCELERATION_COUNT) .next().mph(perf2aMph).seconds(perf2aSeconds) .next().mph(perf2bMph).seconds(perf2bSeconds) .next().mph(perf2cMph).seconds(perf2cSeconds); car.putMake(MAKE, static_cast<int>(strlen(MAKE))); car.putModel(MODEL, static_cast<int>(strlen(MODEL))); car.putActivationCode(ACTIVATION_CODE, static_cast<int>(strlen(ACTIVATION_CODE))); return hdr.encodedLength() + car.encodedLength(); }