virtual std::uint64_t encodeHdrAndMsg()
    {
        MessageHeader hdr;
        Msg msg;

        hdr.wrap(m_buffer, 0, 0, sizeof(m_buffer))
            .blockLength(Msg::sbeBlockLength())
            .templateId(Msg::sbeTemplateId())
            .schemaId(Msg::sbeSchemaId())
            .version(Msg::sbeSchemaVersion());

        msg.wrapForEncode(m_buffer, hdr.encodedLength(), sizeof(m_buffer));

        msg.structure()
            .enumOne(EnumOne::Value10)
            .zeroth(42)
            .setOne().clear().bit0(false).bit16(true).bit26(false);

        msg.structure()
            .inner()
                .first(101)
                .second(202);

        return hdr.encodedLength() + msg.encodedLength();
    }
    std::uint64_t encodeHdrAndMsg()
    {
        MessageHeader hdr;
        TestMessage1 msg;

        hdr.wrap(m_buffer, 0, 0, sizeof(m_buffer))
            .blockLength(TestMessage1::sbeBlockLength())
            .templateId(TestMessage1::sbeTemplateId())
            .schemaId(TestMessage1::sbeSchemaId())
            .version(TestMessage1::sbeSchemaVersion());

        msg.wrapForEncode(m_buffer, hdr.encodedLength(), sizeof(m_buffer));

        TestMessage1::Entries &entries = msg.entriesCount(2);

        entries.next()
            .tagGroup1(10)
            .tagGroup2(20);

        entries.next()
            .tagGroup1(30)
            .tagGroup2(40);

        return hdr.encodedLength() + msg.encodedLength();
    }
    std::uint64_t encodeHdrAndMsg()
    {
        MessageHeader hdr;
        MsgName msg;

        hdr.wrap(m_buffer, 0, 0, sizeof(m_buffer))
            .blockLength(MsgName::sbeBlockLength())
            .templateId(MsgName::sbeTemplateId())
            .schemaId(MsgName::sbeSchemaId())
            .version(MsgName::sbeSchemaVersion());

        msg.wrapForEncode(m_buffer, hdr.encodedLength(), sizeof(m_buffer));

        msg.field1(187);
        msg.field2().clear()
            .choice1(true);

        MsgName::GrName &grp = msg.grNameCount(2);

        grp.next()
           .grField1(10)
           .grField2(20);

        grp.next()
           .grField1(30)
           .grField2(40);

        return hdr.encodedLength() + msg.encodedLength();
    }
int main(int argc, const char* argv[])
{
    char buffer[2048];
    MessageHeader hdr;
    Car car;

    std::size_t encodeHdrLength = encodeHdr(hdr, car, buffer, 0, sizeof(buffer));
    std::size_t encodeMsgLength = encodeCar(car, buffer, hdr.encodedLength(), sizeof(buffer));

    cout << "Encoded Lengths are " << encodeHdrLength << " + " << encodeMsgLength << endl;

    std::size_t decodeHdrLength = decodeHdr(hdr, buffer, 0, sizeof(buffer));
    std::size_t decodeMsgLength = decodeCar(car, buffer, hdr.encodedLength(), hdr.blockLength(), hdr.version(), sizeof(buffer));

    cout << "Decoded Lengths are " << decodeHdrLength << " + " << decodeMsgLength << endl;

    if (encodeHdrLength != decodeHdrLength)
    {
        cerr << "Encode/Decode header lengths do not match\n";
        return EXIT_FAILURE;
    }

    if (encodeMsgLength != decodeMsgLength)
    {
        cerr << "Encode/Decode message lengths do not match\n";
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}
std::size_t decodeHdr(MessageHeader &hdr, char *buffer, std::uint64_t offset, std::uint64_t bufferLength)
{
    hdr.wrap(buffer, offset, messageHeaderVersion, bufferLength);

    // decode the header
    cout << "messageHeader.blockLength=" << hdr.blockLength() << endl;
    cout << "messageHeader.templateId=" << hdr.templateId() << endl;
    cout << "messageHeader.schemaId=" << hdr.schemaId() << endl;
    cout << "messageHeader.schemaVersion=" << hdr.version() << endl;
    cout << "messageHeader.encodedLength=" << hdr.encodedLength() << endl;

    return hdr.encodedLength();
}
    static std::uint64_t encodeHdr(MessageHeader& hdr)
    {
        hdr.blockLength(Car::sbeBlockLength())
        .templateId(Car::sbeTemplateId())
        .schemaId(Car::sbeSchemaId())
        .version(Car::sbeSchemaVersion());

        return hdr.encodedLength();
    }
std::size_t encodeHdr(MessageHeader &hdr, Car &car, char *buffer, std::uint64_t offset, std::uint64_t bufferLength)
{
    // encode the header
    hdr.wrap(buffer, offset, messageHeaderVersion, bufferLength)
       .blockLength(Car::sbeBlockLength())
       .templateId(Car::sbeTemplateId())
       .schemaId(Car::sbeSchemaId())
       .version(Car::sbeSchemaVersion());

    return hdr.encodedLength();
}
    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();
    }
TEST_F(CodeGenTest, shouldbeAbleUseOnStackCodecsAndGroupForEach)
{
    char buffer[BUFFER_LEN];
    MessageHeader hdr(buffer, sizeof(buffer), 0);
    Car car(buffer + hdr.encodedLength(), sizeof(buffer) - hdr.encodedLength());

    std::uint64_t hdrSz = encodeHdr(hdr);
    std::uint64_t carSz = encodeCar(car);

    EXPECT_EQ(hdrSz, expectedHeaderSize);
    EXPECT_EQ(carSz, expectedCarSize);

    const MessageHeader hdrDecoder(buffer, hdrSz, 0);

    EXPECT_EQ(hdrDecoder.blockLength(), Car::sbeBlockLength());
    EXPECT_EQ(hdrDecoder.templateId(), Car::sbeTemplateId());
    EXPECT_EQ(hdrDecoder.schemaId(), Car::sbeSchemaId());
    EXPECT_EQ(hdrDecoder.version(), Car::sbeSchemaVersion());
    EXPECT_EQ(hdrDecoder.encodedLength(), expectedHeaderSize);

    Car carDecoder(buffer + hdrDecoder.encodedLength(), carSz, hdrDecoder.blockLength(), hdrDecoder.version());
    CallbacksForEach cbs;

    Car::FuelFigures &fuelFigures = carDecoder.fuelFigures();
    EXPECT_EQ(fuelFigures.count(), FUEL_FIGURES_COUNT);

#if __cplusplus >= 201103L
    fuelFigures.forEach([&](Car::FuelFigures &figures)
    {
        cbs.countOfFuelFigures++;

        char tmp[256];
        figures.getUsageDescription(tmp, sizeof(tmp));
    });
#else
    fuelFigures.forEach(cbs);
#endif

    Car::PerformanceFigures &performanceFigures = carDecoder.performanceFigures();
    EXPECT_EQ(performanceFigures.count(), PERFORMANCE_FIGURES_COUNT);

#if __cplusplus >= 201103L
    performanceFigures.forEach([&](Car::PerformanceFigures&figures)
    {
        Car::PerformanceFigures::Acceleration acceleration = figures.acceleration();

        cbs.countOfPerformanceFigures++;
        acceleration.forEach([&](Car::PerformanceFigures::Acceleration&)
        {
            cbs.countOfAccelerations++;
        });
    });
#else
    performanceFigures.forEach(cbs);
#endif

    EXPECT_EQ(cbs.countOfFuelFigures, FUEL_FIGURES_COUNT);
    EXPECT_EQ(cbs.countOfPerformanceFigures, PERFORMANCE_FIGURES_COUNT);
    EXPECT_EQ(cbs.countOfAccelerations, ACCELERATION_COUNT * PERFORMANCE_FIGURES_COUNT);

    char tmp[256];

    EXPECT_EQ(carDecoder.getMake(tmp, sizeof(tmp)), MAKE_LENGTH);
    EXPECT_EQ(std::string(tmp, MAKE_LENGTH), MAKE);

    EXPECT_EQ(carDecoder.getModel(tmp, sizeof(tmp)), MODEL_LENGTH);
    EXPECT_EQ(std::string(tmp, MODEL_LENGTH), MODEL);

    EXPECT_EQ(carDecoder.getMake(tmp, sizeof(tmp)), ACTIVATION_CODE_LENGTH);
    EXPECT_EQ(std::string(tmp, ACTIVATION_CODE_LENGTH), ACTIVATION_CODE);

    EXPECT_EQ(carDecoder.encodedLength(), expectedCarSize);
}