Skip to content

Commit e100fcb

Browse files
committed
[C++] Support resetting group count to current index to allow for streaming operations where the group count is difficult to determine upfront but can be limited and reset down to current index value when group is complete.
1 parent fef3fa9 commit e100fcb

File tree

2 files changed

+39
-14
lines changed

2 files changed

+39
-14
lines changed

sbe-samples/src/main/cpp/GeneratedStubExample.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ std::size_t encodeCar(Car &car, char *buffer, std::uint64_t offset, std::uint64_
126126
return car.encodedLength();
127127
}
128128

129-
130129
const char *format(BooleanType::Value value)
131130
{
132131
if (value == BooleanType::T)
@@ -176,8 +175,12 @@ const char *format(bool value)
176175
}
177176

178177
std::size_t decodeCar(
179-
Car &car, char *buffer, std::uint64_t offset, std::uint64_t actingBlockLength,
180-
std::uint64_t actingVersion, std::uint64_t bufferLength)
178+
Car &car,
179+
char *buffer,
180+
std::uint64_t offset,
181+
std::uint64_t actingBlockLength,
182+
std::uint64_t actingVersion,
183+
std::uint64_t bufferLength)
181184
{
182185
car.wrapForDecode(buffer, offset, actingBlockLength, actingVersion, bufferLength);
183186

@@ -252,7 +255,7 @@ std::size_t decodeCar(
252255

253256
char tmp[1024];
254257
std::uint64_t bytesCopied = engine.getFuel(tmp, sizeof(tmp));
255-
std::cout << "\ncar.engine.effifiency=" << (int)engine.efficiency();
258+
std::cout << "\ncar.engine.efficiency=" << (int)engine.efficiency();
256259
std::cout << "\ncar.engine.boosterEnabled=" << format(engine.boosterEnabled());
257260
std::cout << "\ncar.engine.fuelLength=" << bytesCopied;
258261
std::cout << "\ncar.engine.fuel=" << std::string(tmp, bytesCopied);

sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/cpp/CppGenerator.java

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -192,15 +192,16 @@ private static void generateGroupClassHeader(
192192
final int blockLength = tokens.get(index).encodedLength();
193193
final Token blockLengthToken = Generators.findFirst("blockLength", tokens, index);
194194
final Token numInGroupToken = Generators.findFirst("numInGroup", tokens, index);
195-
final String cppTypeForBlockLength = cppTypeName(blockLengthToken.encoding().primitiveType());
196-
final String cppTypeForNumInGroup = cppTypeName(numInGroupToken.encoding().primitiveType());
195+
final String cppTypeBlockLength = cppTypeName(blockLengthToken.encoding().primitiveType());
196+
final String cppTypeNumInGroup = cppTypeName(numInGroupToken.encoding().primitiveType());
197197

198198
new Formatter(sb).format("\n" +
199199
indent + "class %1$s\n" +
200200
indent + "{\n" +
201201
indent + "private:\n" +
202202
indent + " char *m_buffer = nullptr;\n" +
203203
indent + " std::uint64_t m_bufferLength = 0;\n" +
204+
indent + " std::uint64_t m_initialPosition = 0;\n" +
204205
indent + " std::uint64_t *m_positionPtr = nullptr;\n" +
205206
indent + " std::uint64_t m_blockLength = 0;\n" +
206207
indent + " std::uint64_t m_count = 0;\n" +
@@ -228,8 +229,9 @@ private static void generateGroupClassHeader(
228229
indent + " m_bufferLength = bufferLength;\n" +
229230
indent + " m_blockLength = dimensions.blockLength();\n" +
230231
indent + " m_count = dimensions.numInGroup();\n" +
231-
indent + " m_index = std::numeric_limits<std::uint64_t>::max();\n" +
232+
indent + " m_index = 0;\n" +
232233
indent + " m_actingVersion = actingVersion;\n" +
234+
indent + " m_initialPosition = *pos;\n" +
233235
indent + " m_positionPtr = pos;\n" +
234236
indent + " *m_positionPtr = *m_positionPtr + %1$d;\n" +
235237
indent + " }\n",
@@ -262,14 +264,18 @@ private static void generateGroupClassHeader(
262264
indent + " %7$s dimensions(buffer, *pos, bufferLength, actingVersion);\n" +
263265
indent + " dimensions.blockLength((%1$s)%2$d);\n" +
264266
indent + " dimensions.numInGroup((%3$s)count);\n" +
265-
indent + " m_index = std::numeric_limits<std::uint64_t>::max();\n" +
267+
indent + " m_index = 0;\n" +
266268
indent + " m_count = count;\n" +
267269
indent + " m_blockLength = %2$d;\n" +
268270
indent + " m_actingVersion = actingVersion;\n" +
271+
indent + " m_initialPosition = *pos;\n" +
269272
indent + " m_positionPtr = pos;\n" +
270273
indent + " *m_positionPtr = *m_positionPtr + %4$d;\n" +
271274
indent + " }\n",
272-
cppTypeForBlockLength, blockLength, cppTypeForNumInGroup, dimensionHeaderLength,
275+
cppTypeBlockLength,
276+
blockLength,
277+
cppTypeNumInGroup,
278+
dimensionHeaderLength,
273279
minCheck,
274280
numInGroupToken.encoding().applicableMaxValue().longValue(),
275281
dimensionsClassName);
@@ -311,25 +317,41 @@ private static void generateGroupClassHeader(
311317

312318
indent + " SBE_NODISCARD inline bool hasNext() const SBE_NOEXCEPT\n" +
313319
indent + " {\n" +
314-
indent + " return m_index + 1 < m_count;\n" +
320+
indent + " return m_index < m_count;\n" +
315321
indent + " }\n\n" +
316322

317323
indent + " inline %3$s &next()\n" +
318324
indent + " {\n" +
325+
indent + " if (m_index >= m_count)\n" +
326+
indent + " {\n" +
327+
indent + " throw std::runtime_error(\"index >= count [E108]\");\n" +
328+
indent + " }\n" +
319329
indent + " m_offset = *m_positionPtr;\n" +
320330
indent + " if (SBE_BOUNDS_CHECK_EXPECT(((m_offset + m_blockLength) > m_bufferLength), false))\n" +
321331
indent + " {\n" +
322-
indent + " throw std::runtime_error(\"" +
323-
"buffer too short to support next group index [E108]\");\n" +
332+
indent + " throw std::runtime_error(\"buffer too short for next group index [E108]\");\n" +
324333
indent + " }\n" +
325334
indent + " *m_positionPtr = m_offset + m_blockLength;\n" +
326335
indent + " ++m_index;\n\n" +
327336

328337
indent + " return *this;\n" +
329338
indent + " }\n",
330-
dimensionHeaderLength, blockLength, formatClassName(groupName));
339+
dimensionHeaderLength,
340+
blockLength,
341+
formatClassName(groupName));
342+
343+
sb.append("\n")
344+
.append(indent).append(" inline std::uint64_t resetCountToIndex() SBE_NOEXCEPT\n")
345+
.append(indent).append(" {\n")
346+
.append(indent).append(" m_count = m_index;\n")
347+
.append(indent).append(" ").append(dimensionsClassName)
348+
.append(" dimensions(m_buffer, m_initialPosition, m_bufferLength, m_actingVersion);\n")
349+
.append(indent).append(" dimensions.numInGroup((").append(cppTypeNumInGroup).append(")m_count);\n")
350+
.append(indent).append(" return m_count;\n")
351+
.append(indent).append(" }\n");
331352

332-
sb.append(indent).append("#if __cplusplus < 201103L\n")
353+
sb.append("\n")
354+
.append(indent).append("#if __cplusplus < 201103L\n")
333355
.append(indent).append(" template<class Func> inline void forEach(Func& func)\n")
334356
.append(indent).append(" {\n")
335357
.append(indent).append(" while (hasNext())\n")

0 commit comments

Comments
 (0)