From 00b977eb40486f7e6057fca643db4c350e7f9822 Mon Sep 17 00:00:00 2001 From: Pavel Kirienko Date: Sat, 1 Feb 2014 19:00:05 +0400 Subject: [PATCH] Minor source reorganization; few dangerous C functions replaced with safer std:: alternatives --- libuavcan/include/uavcan/can_driver.hpp | 8 ++--- .../internal/{ => transport}/can_io.hpp | 1 - libuavcan/include/uavcan/system_clock.hpp | 2 ++ libuavcan/src/can_driver.cpp | 2 +- libuavcan/src/{ => transport}/can_io.cpp | 2 +- .../test/{can/frame.cpp => can_driver.cpp} | 1 + libuavcan/test/{can => }/common.hpp | 35 +++---------------- libuavcan/test/{ => transport}/can/io.cpp | 4 ++- .../test/{ => transport}/can/tx_queue.cpp | 28 ++++++++++++++- 9 files changed, 43 insertions(+), 40 deletions(-) rename libuavcan/include/uavcan/internal/{ => transport}/can_io.hpp (99%) rename libuavcan/src/{ => transport}/can_io.cpp (99%) rename libuavcan/test/{can/frame.cpp => can_driver.cpp} (97%) rename libuavcan/test/{can => }/common.hpp (59%) rename libuavcan/test/{ => transport}/can/io.cpp (99%) rename libuavcan/test/{ => transport}/can/tx_queue.cpp (91%) diff --git a/libuavcan/include/uavcan/can_driver.hpp b/libuavcan/include/uavcan/can_driver.hpp index 22fb397a7c..277795f23d 100644 --- a/libuavcan/include/uavcan/can_driver.hpp +++ b/libuavcan/include/uavcan/can_driver.hpp @@ -7,7 +7,7 @@ #include #include -#include +#include #include namespace uavcan @@ -31,7 +31,7 @@ struct CanFrame : id(0) , dlc(0) { - std::memset(data, 0, sizeof(data)); + std::fill(data, data + sizeof(data), 0); } CanFrame(uint32_t id, const uint8_t* data, unsigned int dlc) @@ -39,13 +39,13 @@ struct CanFrame , dlc(dlc) { assert(data && dlc <= 8); - std::memmove(this->data, data, dlc); + std::copy(data, data + dlc, this->data); } bool operator!=(const CanFrame& rhs) const { return !operator==(rhs); } bool operator==(const CanFrame& rhs) const { - return (id == rhs.id) && (dlc == rhs.dlc) && (memcmp(data, rhs.data, dlc) == 0); + return (id == rhs.id) && (dlc == rhs.dlc) && std::equal(data, data + dlc, rhs.data); } bool isExtended() const { return id & FLAG_EFF; } diff --git a/libuavcan/include/uavcan/internal/can_io.hpp b/libuavcan/include/uavcan/internal/transport/can_io.hpp similarity index 99% rename from libuavcan/include/uavcan/internal/can_io.hpp rename to libuavcan/include/uavcan/internal/transport/can_io.hpp index c56c3fd69c..20883d23e9 100644 --- a/libuavcan/include/uavcan/internal/can_io.hpp +++ b/libuavcan/include/uavcan/internal/transport/can_io.hpp @@ -6,7 +6,6 @@ #pragma once #include -#include #include #include #include diff --git a/libuavcan/include/uavcan/system_clock.hpp b/libuavcan/include/uavcan/system_clock.hpp index c5fb62db28..af34fc0121 100644 --- a/libuavcan/include/uavcan/system_clock.hpp +++ b/libuavcan/include/uavcan/system_clock.hpp @@ -18,6 +18,8 @@ namespace uavcan class ISystemClock { public: + virtual ~ISystemClock() { } + /** * Monototic system clock in microseconds. * This shall never jump during UTC timestamp adjustments; the base time is irrelevant. diff --git a/libuavcan/src/can_driver.cpp b/libuavcan/src/can_driver.cpp index 02389615e9..338a8680d2 100644 --- a/libuavcan/src/can_driver.cpp +++ b/libuavcan/src/can_driver.cpp @@ -20,7 +20,7 @@ std::string CanFrame::toString(StringRepresentation mode) const char buf[50]; char* wpos = buf, *epos = buf + sizeof(buf); - memset(buf, 0, sizeof(buf)); + std::fill(buf, buf + sizeof(buf), 0); if (id & FLAG_EFF) { diff --git a/libuavcan/src/can_io.cpp b/libuavcan/src/transport/can_io.cpp similarity index 99% rename from libuavcan/src/can_io.cpp rename to libuavcan/src/transport/can_io.cpp index 7bc08d0001..1cf4f7aa5d 100644 --- a/libuavcan/src/can_io.cpp +++ b/libuavcan/src/transport/can_io.cpp @@ -5,7 +5,7 @@ #include #include -#include +#include #include namespace uavcan diff --git a/libuavcan/test/can/frame.cpp b/libuavcan/test/can_driver.cpp similarity index 97% rename from libuavcan/test/can/frame.cpp rename to libuavcan/test/can_driver.cpp index 22ce460da6..47c2140097 100644 --- a/libuavcan/test/can/frame.cpp +++ b/libuavcan/test/can_driver.cpp @@ -4,6 +4,7 @@ #include #include "common.hpp" +#include TEST(CanFrame, FrameProperties) { diff --git a/libuavcan/test/can/common.hpp b/libuavcan/test/common.hpp similarity index 59% rename from libuavcan/test/can/common.hpp rename to libuavcan/test/common.hpp index 1808f3a279..1d3cce8ede 100644 --- a/libuavcan/test/can/common.hpp +++ b/libuavcan/test/common.hpp @@ -1,10 +1,12 @@ /* - * Copyright (C) 2014 Pavel Kirienko + * Copyright (C) 2014 */ #pragma once -#include +#include +#include +#include class SystemClockMock : public uavcan::ISystemClock { @@ -47,32 +49,3 @@ static uavcan::CanFrame makeFrame(uint32_t id, const std::string& str_data, Fram id |= (type == EXT) ? uavcan::CanFrame::FLAG_EFF : 0; return uavcan::CanFrame(id, reinterpret_cast(str_data.c_str()), str_data.length()); } - -namespace -{ - -int getQueueLength(uavcan::CanTxQueue& queue) -{ - const uavcan::CanTxQueue::Entry* p = queue.peek(); - int length = 0; - while (p) - { - length++; - p = p->getNextListNode(); - } - return length; -} - -bool isInQueue(uavcan::CanTxQueue& queue, const uavcan::CanFrame& frame) -{ - const uavcan::CanTxQueue::Entry* p = queue.peek(); - while (p) - { - if (frame == p->frame) - return true; - p = p->getNextListNode(); - } - return false; -} - -} diff --git a/libuavcan/test/can/io.cpp b/libuavcan/test/transport/can/io.cpp similarity index 99% rename from libuavcan/test/can/io.cpp rename to libuavcan/test/transport/can/io.cpp index 83d2868d0e..c66dbfe053 100644 --- a/libuavcan/test/can/io.cpp +++ b/libuavcan/test/transport/can/io.cpp @@ -5,7 +5,9 @@ #include #include #include -#include "common.hpp" +#include +#include "../../common.hpp" + class CanIfaceMock : public uavcan::ICanIface { diff --git a/libuavcan/test/can/tx_queue.cpp b/libuavcan/test/transport/can/tx_queue.cpp similarity index 91% rename from libuavcan/test/can/tx_queue.cpp rename to libuavcan/test/transport/can/tx_queue.cpp index 6386dcad86..3ccc8f8b85 100644 --- a/libuavcan/test/can/tx_queue.cpp +++ b/libuavcan/test/transport/can/tx_queue.cpp @@ -3,7 +3,33 @@ */ #include -#include "common.hpp" +#include +#include "../../common.hpp" + + +static int getQueueLength(uavcan::CanTxQueue& queue) +{ + const uavcan::CanTxQueue::Entry* p = queue.peek(); + int length = 0; + while (p) + { + length++; + p = p->getNextListNode(); + } + return length; +} + +static bool isInQueue(uavcan::CanTxQueue& queue, const uavcan::CanFrame& frame) +{ + const uavcan::CanTxQueue::Entry* p = queue.peek(); + while (p) + { + if (frame == p->frame) + return true; + p = p->getNextListNode(); + } + return false; +} TEST(CanTxQueue, Qos) {