aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc Schink <swo-dev@marcschink.de>2017-06-30 17:33:20 +0200
committerMarc Schink <swo-dev@marcschink.de>2017-07-11 10:27:06 +0200
commit34b69a76bea79945ed320d8dd78c487a8d5c5e79 (patch)
treeba0b5035d8fa611ff0c920e6d9771ecc3bd4e8c9
parentf2ad738832bfa502baed7a19b9b181ca9690eb09 (diff)
C++: Use C++11 style enums
-rw-r--r--bindings/cxx/context.cpp12
-rw-r--r--bindings/cxx/exceptiontrace.cpp8
-rw-r--r--bindings/cxx/extension.cpp8
-rw-r--r--bindings/cxx/libswocxx.h100
-rw-r--r--bindings/cxx/localtimestamp.cpp12
-rw-r--r--bindings/cxx/packet.cpp4
-rw-r--r--bindings/python/swopy.i72
7 files changed, 133 insertions, 83 deletions
diff --git a/bindings/cxx/context.cpp b/bindings/cxx/context.cpp
index 6061186..e9e891b 100644
--- a/bindings/cxx/context.cpp
+++ b/bindings/cxx/context.cpp
@@ -49,7 +49,7 @@ Context::~Context(void)
libswo_exit(_context);
}
-enum LogLevel Context::get_log_level(void) const
+LogLevel Context::get_log_level(void) const
{
int ret;
enum libswo_log_level level;
@@ -59,10 +59,10 @@ enum LogLevel Context::get_log_level(void) const
if (ret != LIBSWO_OK)
throw Error(ret);
- return static_cast<enum LogLevel>(level);
+ return static_cast<LogLevel>(level);
}
-void Context::set_log_level(enum LogLevel level)
+void Context::set_log_level(LogLevel level)
{
int ret;
@@ -132,7 +132,7 @@ static int log_callback(struct libswo_context *ctx,
message.assign(tmp, length);
free(tmp);
- ret = helper->callback(static_cast<enum LogLevel>(level), message,
+ ret = helper->callback(static_cast<LogLevel>(level), message,
helper->user_data);
return ret;
@@ -232,11 +232,11 @@ static int packet_callback(struct libswo_context *ctx,
return ret;
}
-void Context::decode(uint32_t flags)
+void Context::decode(DecoderFlags flags)
{
int ret;
- ret = libswo_decode(_context, flags);
+ ret = libswo_decode(_context, static_cast<uint32_t>(flags));
if (ret != LIBSWO_OK)
throw Error(ret);
diff --git a/bindings/cxx/exceptiontrace.cpp b/bindings/cxx/exceptiontrace.cpp
index 6cf522a..4e3327b 100644
--- a/bindings/cxx/exceptiontrace.cpp
+++ b/bindings/cxx/exceptiontrace.cpp
@@ -40,7 +40,7 @@ uint16_t ExceptionTrace::get_exception(void) const
return _packet.exctrace.exception;
}
-enum ExceptionTraceFunction ExceptionTrace::get_function(void) const
+ExceptionTraceFunction ExceptionTrace::get_function(void) const
{
return static_cast<ExceptionTraceFunction>(_packet.exctrace.function);
}
@@ -53,13 +53,13 @@ const std::string ExceptionTrace::to_string(void) const
ss << ", function = ";
switch (get_function()) {
- case EXCTRACE_FUNC_ENTER:
+ case ExceptionTraceFunction::ENTER:
ss << "enter";
break;
- case EXCTRACE_FUNC_EXIT:
+ case ExceptionTraceFunction::EXIT:
ss << "exit";
break;
- case EXCTRACE_FUNC_RETURN:
+ case ExceptionTraceFunction::RETURN:
ss << "return";
break;
default:
diff --git a/bindings/cxx/extension.cpp b/bindings/cxx/extension.cpp
index 840f210..cee86af 100644
--- a/bindings/cxx/extension.cpp
+++ b/bindings/cxx/extension.cpp
@@ -34,9 +34,9 @@ Extension::Extension(const union libswo_packet *packet)
_packet = *packet;
}
-enum ExtensionSource Extension::get_source(void) const
+ExtensionSource Extension::get_source(void) const
{
- return static_cast<enum ExtensionSource>(_packet.ext.source);
+ return static_cast<ExtensionSource>(_packet.ext.source);
}
uint32_t Extension::get_value(void) const
@@ -51,10 +51,10 @@ const std::string Extension::to_string(void) const
ss << "Extension (source = ";
switch (get_source()) {
- case EXT_SRC_ITM:
+ case ExtensionSource::ITM:
ss << "ITM";
break;
- case EXT_SRC_HW:
+ case ExtensionSource::HW:
ss << "HW";
break;
default:
diff --git a/bindings/cxx/libswocxx.h b/bindings/cxx/libswocxx.h
index 0418f7f..0b6ca20 100644
--- a/bindings/cxx/libswocxx.h
+++ b/bindings/cxx/libswocxx.h
@@ -24,6 +24,7 @@
#include <string>
#include <vector>
#include <stdexcept>
+#include <type_traits>
#include <libswo/libswo.h>
@@ -32,53 +33,54 @@ namespace libswo
using namespace std;
-enum LogLevel {
- LOG_LEVEL_NONE = LIBSWO_LOG_LEVEL_NONE,
- LOG_LEVEL_ERROR = LIBSWO_LOG_LEVEL_ERROR,
- LOG_LEVEL_WARNING = LIBSWO_LOG_LEVEL_WARNING,
- LOG_LEVEL_INFO = LIBSWO_LOG_LEVEL_INFO,
- LOG_LEVEL_DEBUG = LIBSWO_LOG_LEVEL_DEBUG
+enum class LogLevel {
+ NONE = LIBSWO_LOG_LEVEL_NONE,
+ ERROR = LIBSWO_LOG_LEVEL_ERROR,
+ WARNING = LIBSWO_LOG_LEVEL_WARNING,
+ INFO = LIBSWO_LOG_LEVEL_INFO,
+ DEBUG = LIBSWO_LOG_LEVEL_DEBUG
};
-enum PacketType {
- PACKET_TYPE_UNKNOWN = LIBSWO_PACKET_TYPE_UNKNOWN,
- PACKET_TYPE_SYNC = LIBSWO_PACKET_TYPE_SYNC,
- PACKET_TYPE_OVERFLOW = LIBSWO_PACKET_TYPE_OVERFLOW,
- PACKET_TYPE_LTS = LIBSWO_PACKET_TYPE_LTS,
- PACKET_TYPE_GTS1 = LIBSWO_PACKET_TYPE_GTS1,
- PACKET_TYPE_GTS2 = LIBSWO_PACKET_TYPE_GTS2,
- PACKET_TYPE_EXT = LIBSWO_PACKET_TYPE_EXT,
- PACKET_TYPE_INST = LIBSWO_PACKET_TYPE_INST,
- PACKET_TYPE_HW = LIBSWO_PACKET_TYPE_HW,
- PACKET_TYPE_DWT_EVTCNT = LIBSWO_PACKET_TYPE_DWT_EVTCNT,
- PACKET_TYPE_DWT_EXCTRACE = LIBSWO_PACKET_TYPE_DWT_EXCTRACE,
- PACKET_TYPE_DWT_PC_SAMPLE = LIBSWO_PACKET_TYPE_DWT_PC_SAMPLE,
- PACKET_TYPE_DWT_PC_VALUE = LIBSWO_PACKET_TYPE_DWT_PC_VALUE,
- PACKET_TYPE_DWT_ADDR_OFFSET = LIBSWO_PACKET_TYPE_DWT_ADDR_OFFSET,
- PACKET_TYPE_DWT_DATA_VALUE = LIBSWO_PACKET_TYPE_DWT_DATA_VALUE
+enum class PacketType {
+ UNKNOWN = LIBSWO_PACKET_TYPE_UNKNOWN,
+ SYNC = LIBSWO_PACKET_TYPE_SYNC,
+ OF = LIBSWO_PACKET_TYPE_OVERFLOW,
+ LTS = LIBSWO_PACKET_TYPE_LTS,
+ GTS1 = LIBSWO_PACKET_TYPE_GTS1,
+ GTS2 = LIBSWO_PACKET_TYPE_GTS2,
+ EXT = LIBSWO_PACKET_TYPE_EXT,
+ INST = LIBSWO_PACKET_TYPE_INST,
+ HW = LIBSWO_PACKET_TYPE_HW,
+ DWT_EVTCNT = LIBSWO_PACKET_TYPE_DWT_EVTCNT,
+ DWT_EXCTRACE = LIBSWO_PACKET_TYPE_DWT_EXCTRACE,
+ DWT_PC_SAMPLE = LIBSWO_PACKET_TYPE_DWT_PC_SAMPLE,
+ DWT_PC_VALUE = LIBSWO_PACKET_TYPE_DWT_PC_VALUE,
+ DWT_ADDR_OFFSET = LIBSWO_PACKET_TYPE_DWT_ADDR_OFFSET,
+ DWT_DATA_VALUE = LIBSWO_PACKET_TYPE_DWT_DATA_VALUE
};
-enum LocalTimestampRelation {
- LTS_REL_SYNC = LIBSWO_LTS_REL_SYNC,
- LTS_REL_TS = LIBSWO_LTS_REL_TS,
- LTS_REL_SRC = LIBSWO_LTS_REL_SRC,
- LTS_REL_BOTH = LIBSWO_LTS_REL_BOTH
+enum class LocalTimestampRelation {
+ SYNC = LIBSWO_LTS_REL_SYNC,
+ TS = LIBSWO_LTS_REL_TS,
+ SRC = LIBSWO_LTS_REL_SRC,
+ BOTH = LIBSWO_LTS_REL_BOTH
};
-enum ExtensionSource {
- EXT_SRC_ITM = LIBSWO_EXT_SRC_ITM,
- EXT_SRC_HW = LIBSWO_EXT_SRC_HW
+enum class ExtensionSource {
+ ITM = LIBSWO_EXT_SRC_ITM,
+ HW = LIBSWO_EXT_SRC_HW
};
-enum ExceptionTraceFunction {
- EXCTRACE_FUNC_RESERVED = LIBSWO_EXCTRACE_FUNC_RESERVED,
- EXCTRACE_FUNC_ENTER = LIBSWO_EXCTRACE_FUNC_ENTER,
- EXCTRACE_FUNC_EXIT = LIBSWO_EXCTRACE_FUNC_EXIT,
- EXCTRACE_FUNC_RETURN = LIBSWO_EXCTRACE_FUNC_RETURN
+enum class ExceptionTraceFunction {
+ RESERVED = LIBSWO_EXCTRACE_FUNC_RESERVED,
+ ENTER = LIBSWO_EXCTRACE_FUNC_ENTER,
+ EXIT = LIBSWO_EXCTRACE_FUNC_EXIT,
+ RETURN = LIBSWO_EXCTRACE_FUNC_RETURN
};
-enum DecoderFlags {
- DF_EOS = LIBSWO_DF_EOS
+enum class DecoderFlags {
+ NONE = 0,
+ EOS = LIBSWO_DF_EOS
};
class LIBSWO_API Error : public exception
@@ -96,7 +98,7 @@ class LIBSWO_API Packet
public:
virtual ~Packet(void) = 0;
- enum PacketType get_type(void) const;
+ PacketType get_type(void) const;
size_t get_size(void) const;
virtual const string to_string(void) const = 0;
protected:
@@ -112,7 +114,7 @@ public:
};
typedef int (*DecoderCallback)(const Packet &packet, void *user_data);
-typedef int (*LogCallback)(enum LogLevel level, const std::string &message,
+typedef int (*LogCallback)(LogLevel level, const std::string &message,
void *user_data);
class LIBSWO_API Unknown : public PayloadPacket
@@ -148,7 +150,7 @@ public:
LocalTimestamp(const struct libswo_packet_lts *packet);
LocalTimestamp(const union libswo_packet *packet);
- enum LocalTimestampRelation get_relation() const;
+ LocalTimestampRelation get_relation() const;
uint32_t get_value(void) const;
const string to_string(void) const;
@@ -184,7 +186,7 @@ public:
Extension(const struct libswo_packet_ext *packet);
Extension(const union libswo_packet *packet);
- enum ExtensionSource get_source(void) const;
+ ExtensionSource get_source(void) const;
uint32_t get_value(void) const;
const string to_string(void) const;
@@ -239,7 +241,7 @@ public:
ExceptionTrace(const union libswo_packet *packet);
uint16_t get_exception(void) const;
- enum ExceptionTraceFunction get_function(void) const;
+ ExceptionTraceFunction get_function(void) const;
const string to_string(void) const;
};
@@ -314,8 +316,8 @@ public:
Context(uint8_t *buffer, size_t buffer_size);
~Context(void);
- enum LogLevel get_log_level(void) const;
- void set_log_level(enum LogLevel level);
+ LogLevel get_log_level(void) const;
+ void set_log_level(LogLevel level);
string get_log_domain(void) const;
void set_log_domain(const string &domain);
@@ -324,7 +326,7 @@ public:
void set_callback(DecoderCallback callback, void *user_data = NULL);
void feed(const uint8_t *data, size_t length);
- void decode(uint32_t flags = 0);
+ void decode(DecoderFlags flags = DecoderFlags::NONE);
private:
struct libswo_context *_context;
DecoderCallbackHelper _decoder_callback;
@@ -345,6 +347,14 @@ public:
static string get_library_string(void);
};
+inline DecoderFlags operator|(DecoderFlags lhs, DecoderFlags rhs)
+{
+ using type = std::underlying_type<DecoderFlags>::type;
+
+ return static_cast<DecoderFlags>(static_cast<type>(lhs) |
+ static_cast<type>(rhs));
+}
+
}
#endif /* LIBSWOCXX_H */
diff --git a/bindings/cxx/localtimestamp.cpp b/bindings/cxx/localtimestamp.cpp
index cd594f8..7a92de4 100644
--- a/bindings/cxx/localtimestamp.cpp
+++ b/bindings/cxx/localtimestamp.cpp
@@ -39,9 +39,9 @@ uint32_t LocalTimestamp::get_value(void) const
return _packet.lts.value;
}
-enum LocalTimestampRelation LocalTimestamp::get_relation(void) const
+LocalTimestampRelation LocalTimestamp::get_relation(void) const
{
- return static_cast<enum LocalTimestampRelation>(_packet.lts.relation);
+ return static_cast<LocalTimestampRelation>(_packet.lts.relation);
}
const std::string LocalTimestamp::to_string(void) const
@@ -51,16 +51,16 @@ const std::string LocalTimestamp::to_string(void) const
ss << "Local timestamp (relation = ";
switch (get_relation()) {
- case LTS_REL_SYNC:
+ case LocalTimestampRelation::SYNC:
ss << "synchronous";
break;
- case LTS_REL_TS:
+ case LocalTimestampRelation::TS:
ss << "timestamp delayed";
break;
- case LTS_REL_SRC:
+ case LocalTimestampRelation::SRC:
ss << "data delayed";
break;
- case LTS_REL_BOTH:
+ case LocalTimestampRelation::BOTH:
ss << "data and timestamp delayed";
break;
default:
diff --git a/bindings/cxx/packet.cpp b/bindings/cxx/packet.cpp
index 3f26755..78d2b97 100644
--- a/bindings/cxx/packet.cpp
+++ b/bindings/cxx/packet.cpp
@@ -26,9 +26,9 @@ Packet::~Packet(void)
{
}
-enum PacketType Packet::get_type(void) const
+PacketType Packet::get_type(void) const
{
- return static_cast<enum PacketType>(_packet.type);
+ return static_cast<PacketType>(_packet.type);
}
size_t Packet::get_size(void) const
diff --git a/bindings/python/swopy.i b/bindings/python/swopy.i
index 527ae9a..c4c42a1 100644
--- a/bindings/python/swopy.i
+++ b/bindings/python/swopy.i
@@ -57,6 +57,43 @@ static int swig_exception_code(int code)
}
%}
+%rename(LOG_LEVEL_NONE) libswo::LogLevel::NONE;
+%rename(LOG_LEVEL_ERROR) libswo::LogLevel::ERROR;
+%rename(LOG_LEVEL_WARNING) libswo::LogLevel::WARNING;
+%rename(LOG_LEVEL_INFO) libswo::LogLevel::INFO;
+%rename(LOG_LEVEL_DEBUG) libswo::LogLevel::DEBUG;
+
+%rename(PACKET_TYPE_UNKNOWN) libswo::PacketType::UNKNOWN;
+%rename(PACKET_TYPE_SYNC) libswo::PacketType::SYNC;
+%rename(PACKET_TYPE_OF) libswo::PacketType::OF;
+%rename(PACKET_TYPE_LTS) libswo::PacketType::LTS;
+%rename(PACKET_TYPE_GTS1) libswo::PacketType::GTS1;
+%rename(PACKET_TYPE_GTS2) libswo::PacketType::GTS2;
+%rename(PACKET_TYPE_EXT) libswo::PacketType::EXT;
+%rename(PACKET_TYPE_INST) libswo::PacketType::INST;
+%rename(PACKET_TYPE_HW) libswo::PacketType::HW;
+%rename(PACKET_TYPE_DWT_EVTCNT) libswo::PacketType::DWT_EVTCNT;
+%rename(PACKET_TYPE_DWT_EXCTRACE) libswo::PacketType::DWT_EXCTRACE;
+%rename(PACKET_TYPE_DWT_PC_SAMPLE) libswo::PacketType::DWT_PC_SAMPLE;
+%rename(PACKET_TYPE_DWT_PC_VALUE) libswo::PacketType::DWT_PC_VALUE;
+%rename(PACKET_TYPE_DWT_ADDR_OFFSET) libswo::PacketType::DWT_ADDR_OFFSET;
+%rename(PACKET_TYPE_DWT_DATA_VALUE) libswo::PacketType::DWT_DATA_VALUE;
+
+%rename(LTS_SYNC) libswo::LocalTimestampRelation::SYNC;
+%rename(LTS_TS) libswo::LocalTimestampRelation::TS;
+%rename(LTS_SRC) libswo::LocalTimestampRelation::SRC;
+%rename(LTS_BOTH) libswo::LocalTimestampRelation::BOTH;
+
+%rename(EXT_ITM) libswo::ExtensionSource::ITM;
+%rename(EXT_HW) libswo::ExtensionSource::HW;
+
+%rename(EXC_RESERVED) libswo::ExceptionTraceFunction::RESERVED;
+%rename(EXC_ENTER) libswo::ExceptionTraceFunction::ENTER;
+%rename(EXC_EXIT) libswo::ExceptionTraceFunction::EXIT;
+%rename(EXC_RETURN) libswo::ExceptionTraceFunction::RETURN;
+
+%rename(DF_EOS) libswo::DecoderFlags::EOS;
+
%rename(__str__) libswo::Packet::to_string;
%pybuffer_binary(const uint8_t *data, size_t length)
@@ -82,6 +119,8 @@ void libswo::Context::feed(const uint8_t *data, size_t length);
%rename libswo::Context _Context;
%{
+#include <type_traits>
+
#include "Context.h"
Context::Context(size_t buffer_size) :
@@ -105,7 +144,8 @@ static int log_callback(libswo::LogLevel level, const std::string &message,
PyObject *message_obj;
PyObject *res;
- level_obj = PyLong_FromUnsignedLong(level);
+ using type = std::underlying_type<libswo::LogLevel>::type;
+ level_obj = PyLong_FromUnsignedLong(static_cast<type>(level));
if (!level_obj)
return LIBSWO_ERR;
@@ -163,57 +203,57 @@ static PyObject *packet_object(const libswo::Packet &packet)
tmp = (void *)&packet;
switch (packet.get_type()) {
- case libswo::PACKET_TYPE_UNKNOWN:
+ case libswo::PacketType::UNKNOWN:
ret = SWIG_NewPointerObj(tmp, SWIGTYPE_p_libswo__Unknown, 0);
break;
- case libswo::PACKET_TYPE_SYNC:
+ case libswo::PacketType::SYNC:
ret = SWIG_NewPointerObj(tmp,
SWIGTYPE_p_libswo__Synchronization, 0);
break;
- case libswo::PACKET_TYPE_OVERFLOW:
+ case libswo::PacketType::OF:
ret = SWIG_NewPointerObj(tmp, SWIGTYPE_p_libswo__Overflow, 0);
break;
- case libswo::PACKET_TYPE_LTS:
+ case libswo::PacketType::LTS:
ret = SWIG_NewPointerObj(tmp,
SWIGTYPE_p_libswo__LocalTimestamp, 0);
break;
- case libswo::PACKET_TYPE_GTS1:
+ case libswo::PacketType::GTS1:
ret = SWIG_NewPointerObj(tmp,
SWIGTYPE_p_libswo__GlobalTimestamp1, 0);
break;
- case libswo::PACKET_TYPE_GTS2:
+ case libswo::PacketType::GTS2:
ret = SWIG_NewPointerObj(tmp,
SWIGTYPE_p_libswo__GlobalTimestamp2, 0);
break;
- case libswo::PACKET_TYPE_EXT:
+ case libswo::PacketType::EXT:
ret = SWIG_NewPointerObj(tmp, SWIGTYPE_p_libswo__Extension, 0);
break;
- case libswo::PACKET_TYPE_INST:
+ case libswo::PacketType::INST:
ret = SWIG_NewPointerObj(tmp,
SWIGTYPE_p_libswo__Instrumentation, 0);
break;
- case libswo::PACKET_TYPE_HW:
+ case libswo::PacketType::HW:
ret = SWIG_NewPointerObj(tmp, SWIGTYPE_p_libswo__Hardware, 0);
break;
- case libswo::PACKET_TYPE_DWT_EVTCNT:
+ case libswo::PacketType::DWT_EVTCNT:
ret = SWIG_NewPointerObj(tmp, SWIGTYPE_p_libswo__EventCounter,
0);
break;
- case libswo::PACKET_TYPE_DWT_EXCTRACE:
+ case libswo::PacketType::DWT_EXCTRACE:
ret = SWIG_NewPointerObj(tmp,
SWIGTYPE_p_libswo__ExceptionTrace, 0);
break;
- case libswo::PACKET_TYPE_DWT_PC_SAMPLE:
+ case libswo::PacketType::DWT_PC_SAMPLE:
ret = SWIG_NewPointerObj(tmp, SWIGTYPE_p_libswo__PCSample, 0);
break;
- case libswo::PACKET_TYPE_DWT_PC_VALUE:
+ case libswo::PacketType::DWT_PC_VALUE:
ret = SWIG_NewPointerObj(tmp, SWIGTYPE_p_libswo__PCValue, 0);
break;
- case libswo::PACKET_TYPE_DWT_ADDR_OFFSET:
+ case libswo::PacketType::DWT_ADDR_OFFSET:
ret = SWIG_NewPointerObj(tmp,
SWIGTYPE_p_libswo__AddressOffset, 0);
break;
- case libswo::PACKET_TYPE_DWT_DATA_VALUE:
+ case libswo::PacketType::DWT_DATA_VALUE:
ret = SWIG_NewPointerObj(tmp, SWIGTYPE_p_libswo__DataValue, 0);
break;
default: