Skip to content

Commit 0780535

Browse files
committed
Merge pull request aeron-io#222 from objectcomputing/master
Improve C++ code.
2 parents 3c1d10e + 586db7a commit 0780535

File tree

20 files changed

+881
-571
lines changed

20 files changed

+881
-571
lines changed

examples/cpp98/SbeExample.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
using namespace std;
2525
using namespace baseline;
2626

27-
#if defined(WIN32)
27+
#if defined(WIN32) || defined(_WIN32)
2828
# define snprintf _snprintf
2929
#endif /* WIN32 */
3030

examples/cpp98/SbeOtfDecoder.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
#if defined(WIN32)
16+
#if defined(WIN32) || defined(_WIN32)
1717
#include <sys/types.h>
1818
#include <sys/stat.h>
1919
#include <stdio.h>
@@ -319,7 +319,7 @@ int main(int argc, char * const argv[])
319319
CarCallbacks carCbs(listener);
320320
char *buffer = NULL;
321321
int length = 0, ch, justHeader = 0;
322-
#if defined(WIN32)
322+
#if defined(WIN32) || defined(_WIN32)
323323
int optind = 1;
324324

325325
if (strcmp(argv[optind], "-?") == 0)

main/cpp/otf_api/Ir.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ struct Ir::Impl
5151
::uint32_t serializedTokenSize;
5252
};
5353

54-
#if !defined(WIN32)
54+
#if !defined(WIN32) && !defined(_WIN32)
5555
const int Ir::INVALID_ID;
5656
const ::uint32_t Ir::VARIABLE_SIZE;
5757
#endif /* WIN32 */
@@ -180,7 +180,7 @@ ::uint64_t Ir::validValue() const
180180
break;
181181

182182
default:
183-
throw "do not know validValue primitiveType";
183+
throw std::runtime_error("do not know validValue primitiveType [E109]");
184184
break;
185185
}
186186
}
@@ -207,7 +207,7 @@ ::uint64_t Ir::choiceValue() const
207207
break;
208208

209209
default:
210-
throw "do not know choice primitiveType";
210+
throw std::runtime_error("do not know choice primitiveType [E110]");
211211
break;
212212
}
213213
}

main/cpp/otf_api/IrCollection.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#ifndef _IR_COLLECTION_H_
1717
#define _IR_COLLECTION_H_
1818

19-
#if defined(WIN32)
19+
#if defined(WIN32) || defined(_WIN32)
2020
#include <sys/types.h>
2121
#include <sys/stat.h>
2222
#include <io.h>

main/cpp/otf_api/Listener.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,20 @@
2424
* builtins for GCC. MSVC has similar ones.
2525
*/
2626

27-
#if defined(WIN32)
27+
// Some GCC versions do not include __builtin_bswap16
28+
// The compiler optimizes the code below into a single ROL instruction so the intrinsic is not really needed
29+
#define BYTESWAP16(v) (((v) >> 8) | ((v) << 8))
30+
31+
#if defined(WIN32) || defined(_WIN32)
2832
#define BSWAP16(b,v) ((b == Ir::SBE_LITTLE_ENDIAN) ? (v) : _byteswap_ushort((::uint16_t)v))
2933
#define BSWAP32(b,v) ((b == Ir::SBE_LITTLE_ENDIAN) ? (v) : _byteswap_ulong((::uint32_t)v))
3034
#define BSWAP64(b,v) ((b == Ir::SBE_LITTLE_ENDIAN) ? (v) : _byteswap_uint64((::uint64_t)v))
3135
#elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
32-
#define BSWAP16(b,v) ((b == Ir::SBE_LITTLE_ENDIAN) ? (v) : __builtin_bswap16((::uint16_t)v))
36+
#define BSWAP16(b,v) ((b == Ir::SBE_LITTLE_ENDIAN) ? (v) : BYTESWAP16((::uint16_t)v))
3337
#define BSWAP32(b,v) ((b == Ir::SBE_LITTLE_ENDIAN) ? (v) : __builtin_bswap32((::uint32_t)v))
3438
#define BSWAP64(b,v) ((b == Ir::SBE_LITTLE_ENDIAN) ? (v) : __builtin_bswap64((::uint64_t)v))
3539
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
36-
#define BSWAP16(b,v) ((b == Ir::SBE_BIG_ENDIAN) ? (v) : __builtin_bswap16((::uint16_t)v))
40+
#define BSWAP16(b,v) ((b == Ir::SBE_BIG_ENDIAN) ? (v) : BYTESWAP16((::uint16_t)v))
3741
#define BSWAP32(b,v) ((b == Ir::SBE_BIG_ENDIAN) ? (v) : __builtin_bswap32((::uint32_t)v))
3842
#define BSWAP64(b,v) ((b == Ir::SBE_BIG_ENDIAN) ? (v) : __builtin_bswap64((::uint64_t)v))
3943
#else
@@ -44,11 +48,11 @@ using namespace sbe::on_the_fly;
4448
using ::std::cout;
4549
using ::std::endl;
4650

47-
#if !defined(WIN32)
51+
#if defined(WIN32) || defined(_WIN32)
52+
#define snprintf _snprintf
53+
#else
4854
const ::int32_t Field::INVALID_ID;
4955
const int Field::FIELD_INDEX;
50-
#else
51-
#define snprintf _snprintf
5256
#endif /* WIN32 */
5357

5458
Listener::Listener() : onNext_(NULL), onError_(NULL), onCompleted_(NULL),
@@ -112,14 +116,14 @@ int Listener::subscribe(OnNext *onNext,
112116
{
113117
char message[1024];
114118

115-
::snprintf(message, sizeof(message)-1, "no IR found for message with templateId=%ld and version=%ld", templateId_, templateVersion_);
119+
::snprintf(message, sizeof(message)-1, "no IR found for message with templateId=%ld and version=%ld [E117]", templateId_, templateVersion_);
116120
onError_->onError(Error(message));
117121
result = -1;
118122
}
119123
}
120124
else if (onError_ != NULL)
121125
{
122-
onError_->onError(Error("template ID and/or version not found in header"));
126+
onError_->onError(Error("template ID and/or version not found in header [E118]"));
123127
result = -1;
124128
}
125129
}

main/cpp/sbe/sbe.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
#include <string.h>
2020
#include <stdint.h>
21+
#include <limits.h>
22+
#include <stdexcept>
2123

2224
/*
2325
* Types used by C++ codec. Might have to be platform specific at some stage.
@@ -39,7 +41,7 @@ namespace sbe {
3941
/*
4042
* Define some byte ordering macros
4143
*/
42-
#if defined(WIN32)
44+
#if defined(WIN32) || defined(_WIN32)
4345
#define SBE_BIG_ENDIAN_ENCODE_16(v) _byteswap_ushort(v)
4446
#define SBE_BIG_ENDIAN_ENCODE_32(v) _byteswap_ulong(v)
4547
#define SBE_BIG_ENDIAN_ENCODE_64(v) _byteswap_uint64(v)
@@ -66,7 +68,7 @@ namespace sbe {
6668

6769
#if defined(SBE_NO_BOUNDS_CHECK)
6870
#define SBE_BOUNDS_CHECK_EXPECT(exp,c) (false)
69-
#elif defined(WIN32)
71+
#elif defined(WIN32) || defined(_WIN32)
7072
#define SBE_BOUNDS_CHECK_EXPECT(exp,c) (exp)
7173
#else
7274
#define SBE_BOUNDS_CHECK_EXPECT(exp,c) (__builtin_expect(exp,c))

main/cpp/uk_co_real_logic_sbe_ir_generated/ByteOrderCodec.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class ByteOrderCodec
4040
case 255: return NULL_VALUE;
4141
}
4242

43-
throw "unknown value for enum ByteOrderCodec";
43+
throw std::runtime_error("unknown value for enum ByteOrderCodec [E111]");
4444
}
4545
};
4646
}

main/cpp/uk_co_real_logic_sbe_ir_generated/FrameCodec.hpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ class FrameCodec
107107
{
108108
if (SBE_BOUNDS_CHECK_EXPECT((position > bufferLength_), 0))
109109
{
110-
throw "buffer too short";
110+
throw std::runtime_error("buffer too short [E101]");
111111
}
112112
position_ = position;
113113
}
@@ -157,17 +157,17 @@ class FrameCodec
157157

158158
static const sbe_int32_t irIdNullValue()
159159
{
160-
return -2147483648;
160+
return INT_MIN; // -2147483648;
161161
}
162162

163163
static const sbe_int32_t irIdMinValue()
164164
{
165-
return -2147483647;
165+
return INT_MIN + 1; // -2147483647;
166166
}
167167

168168
static const sbe_int32_t irIdMaxValue()
169169
{
170-
return 2147483647;
170+
return INT_MAX; // 2147483647;
171171
}
172172

173173
sbe_int32_t irId(void) const
@@ -211,17 +211,17 @@ class FrameCodec
211211

212212
static const sbe_int32_t irVersionNullValue()
213213
{
214-
return -2147483648;
214+
return INT_MIN; // -2147483648;
215215
}
216216

217217
static const sbe_int32_t irVersionMinValue()
218218
{
219-
return -2147483647;
219+
return INT_MIN + 1; // -2147483647;
220220
}
221221

222222
static const sbe_int32_t irVersionMaxValue()
223223
{
224-
return 2147483647;
224+
return INT_MAX; // 2147483647;
225225
}
226226

227227
sbe_int32_t irVersion(void) const
@@ -265,17 +265,17 @@ class FrameCodec
265265

266266
static const sbe_int32_t schemaVersionNullValue()
267267
{
268-
return -2147483648;
268+
return INT_MIN; // -2147483648;
269269
}
270270

271271
static const sbe_int32_t schemaVersionMinValue()
272272
{
273-
return -2147483647;
273+
return INT_MIN + 1; // -2147483647;
274274
}
275275

276276
static const sbe_int32_t schemaVersionMaxValue()
277277
{
278-
return 2147483647;
278+
return INT_MAX; // 2147483647;
279279
}
280280

281281
sbe_int32_t schemaVersion(void) const

main/cpp/uk_co_real_logic_sbe_ir_generated/MessageHeader.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class MessageHeader
3232
{
3333
if (SBE_BOUNDS_CHECK_EXPECT((offset > (bufferLength - 8)), 0))
3434
{
35-
throw "buffer too short for flyweight";
35+
throw std::runtime_error("buffer too short for flyweight [E112]");
3636
}
3737
buffer_ = buffer;
3838
offset_ = offset;

main/cpp/uk_co_real_logic_sbe_ir_generated/PresenceCodec.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class PresenceCodec
4242
case 255: return NULL_VALUE;
4343
}
4444

45-
throw "unknown value for enum PresenceCodec";
45+
throw std::runtime_error("unknown value for enum PresenceCodec [E113]");
4646
}
4747
};
4848
}

0 commit comments

Comments
 (0)