From 7608e4ca08abb2ab11f8d771a25aee05e45d2169 Mon Sep 17 00:00:00 2001 From: Pavel Kirienko Date: Sun, 4 May 2014 23:13:38 +0400 Subject: [PATCH] Functions from reimplemented in libuavcan --- libuavcan/include/uavcan/driver/can.hpp | 6 +- .../helpers/component_status_manager.hpp | 2 +- libuavcan/include/uavcan/marshal/array.hpp | 10 +- libuavcan/include/uavcan/node/scheduler.hpp | 8 +- .../include/uavcan/node/service_client.hpp | 6 +- libuavcan/include/uavcan/time.hpp | 6 +- .../include/uavcan/util/compile_time.hpp | 105 ++++++++++++++++++ .../include/uavcan/util/lazy_constructor.hpp | 7 +- libuavcan/src/driver/uc_can.cpp | 2 +- libuavcan/src/marshal/uc_bit_stream.cpp | 4 +- libuavcan/src/marshal/uc_float_spec.cpp | 2 +- libuavcan/src/node/uc_generic_publisher.cpp | 4 +- libuavcan/src/node/uc_scheduler.cpp | 2 +- libuavcan/src/transport/uc_frame.cpp | 14 +-- .../src/transport/uc_transfer_buffer.cpp | 10 +- .../src/transport/uc_transfer_listener.cpp | 2 +- .../src/transport/uc_transfer_receiver.cpp | 4 +- .../src/transport/uc_transfer_sender.cpp | 2 +- libuavcan/src/uc_dynamic_memory.cpp | 2 +- 19 files changed, 152 insertions(+), 46 deletions(-) diff --git a/libuavcan/include/uavcan/driver/can.hpp b/libuavcan/include/uavcan/driver/can.hpp index 9ab7ed517d..4b468eb1d2 100644 --- a/libuavcan/include/uavcan/driver/can.hpp +++ b/libuavcan/include/uavcan/driver/can.hpp @@ -34,7 +34,7 @@ struct UAVCAN_EXPORT CanFrame : id(0) , dlc(0) { - std::fill(data, data + MaxDataLen, 0); + fill(data, data + MaxDataLen, 0); } CanFrame(uint32_t arg_id, const uint8_t* arg_data, uint8_t data_len) @@ -43,13 +43,13 @@ struct UAVCAN_EXPORT CanFrame { assert(arg_data != NULL); assert(data_len == dlc); - (void)std::copy(arg_data, arg_data + dlc, this->data); + (void)copy(arg_data, arg_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) && std::equal(data, data + dlc, rhs.data); + return (id == rhs.id) && (dlc == rhs.dlc) && equal(data, data + dlc, rhs.data); } bool isExtended() const { return id & FlagEFF; } diff --git a/libuavcan/include/uavcan/helpers/component_status_manager.hpp b/libuavcan/include/uavcan/helpers/component_status_manager.hpp index 8de7299ae5..1731466059 100644 --- a/libuavcan/include/uavcan/helpers/component_status_manager.hpp +++ b/libuavcan/include/uavcan/helpers/component_status_manager.hpp @@ -45,7 +45,7 @@ public: StatusCode result = 0; for (unsigned i = 0; i < NumComponents; i++) { - result = std::max(result, status_array[i]); + result = max(result, status_array[i]); } return result; } diff --git a/libuavcan/include/uavcan/marshal/array.hpp b/libuavcan/include/uavcan/marshal/array.hpp index b9178cc59e..2963829339 100644 --- a/libuavcan/include/uavcan/marshal/array.hpp +++ b/libuavcan/include/uavcan/marshal/array.hpp @@ -148,7 +148,7 @@ private: { if (ArrayMode != ArrayModeDynamic) { - std::fill(data_, data_ + MaxSize, U()); + fill(data_, data_ + MaxSize, U()); } } template void initialize(...) { } @@ -192,7 +192,7 @@ public: template bool operator<(const R& rhs) const { - return std::lexicographical_compare(begin(), end(), rhs.begin(), rhs.end()); + return ::uavcan::lexicographical_compare(begin(), end(), rhs.begin(), rhs.end()); } typedef ValueType* iterator; @@ -437,11 +437,11 @@ class UAVCAN_EXPORT Array : public ArrayImpl } else if (this->size() == MaxSize) { - (void)std::copy(this->begin(), this->end(), it); + (void)::uavcan::copy(this->begin(), this->end(), it); } else { - std::fill_n(it, MaxSize, 0); + ::uavcan::fill_n(it, MaxSize, 0); } } @@ -628,7 +628,7 @@ public: using namespace std; // For snprintf() const int ret = snprintf(reinterpret_cast(ptr), max_size + 1, format, value); - for (int i = 0; i < std::min(ret, int(max_size)); i++) + for (int i = 0; i < min(ret, int(max_size)); i++) { Base::grow(); } diff --git a/libuavcan/include/uavcan/node/scheduler.hpp b/libuavcan/include/uavcan/node/scheduler.hpp index 88d92332bd..fa99083b59 100644 --- a/libuavcan/include/uavcan/node/scheduler.hpp +++ b/libuavcan/include/uavcan/node/scheduler.hpp @@ -99,16 +99,16 @@ public: MonotonicDuration getDeadlineResolution() const { return deadline_resolution_; } void setDeadlineResolution(MonotonicDuration res) { - res = std::min(res, MonotonicDuration::fromMSec(MaxDeadlineResolutionMs)); - res = std::max(res, MonotonicDuration::fromMSec(MinDeadlineResolutionMs)); + res = min(res, MonotonicDuration::fromMSec(MaxDeadlineResolutionMs)); + res = max(res, MonotonicDuration::fromMSec(MinDeadlineResolutionMs)); deadline_resolution_ = res; } MonotonicDuration getCleanupPeriod() const { return cleanup_period_; } void setCleanupPeriod(MonotonicDuration period) { - period = std::min(period, MonotonicDuration::fromMSec(MaxCleanupPeriodMs)); - period = std::max(period, MonotonicDuration::fromMSec(MinCleanupPeriodMs)); + period = min(period, MonotonicDuration::fromMSec(MaxCleanupPeriodMs)); + period = max(period, MonotonicDuration::fromMSec(MinCleanupPeriodMs)); cleanup_period_ = period; } }; diff --git a/libuavcan/include/uavcan/node/service_client.hpp b/libuavcan/include/uavcan/node/service_client.hpp index d79631fda8..27ab81d2af 100644 --- a/libuavcan/include/uavcan/node/service_client.hpp +++ b/libuavcan/include/uavcan/node/service_client.hpp @@ -168,11 +168,11 @@ public: MonotonicDuration getRequestTimeout() const { return request_timeout_; } void setRequestTimeout(MonotonicDuration timeout) { - timeout = std::max(timeout, getMinRequestTimeout()); - timeout = std::min(timeout, getMaxRequestTimeout()); + timeout = max(timeout, getMinRequestTimeout()); + timeout = min(timeout, getMaxRequestTimeout()); publisher_.setTxTimeout(timeout); - request_timeout_ = std::max(timeout, publisher_.getTxTimeout()); // No less than TX timeout + request_timeout_ = max(timeout, publisher_.getTxTimeout()); // No less than TX timeout } }; diff --git a/libuavcan/include/uavcan/time.hpp b/libuavcan/include/uavcan/time.hpp index 4de173ce85..3c5bb50763 100644 --- a/libuavcan/include/uavcan/time.hpp +++ b/libuavcan/include/uavcan/time.hpp @@ -42,7 +42,7 @@ public: int64_t toUSec() const { return usec_; } int64_t toMSec() const { return usec_ / 1000; } - D getAbs() const { return D::fromUSec(std::abs(usec_)); } + D getAbs() const { return D::fromUSec((usec_ < 0) ? (-usec_) : usec_); } bool isPositive() const { return usec_ > 0; } bool isNegative() const { return usec_ < 0; } @@ -229,8 +229,8 @@ void DurationBase::toString(char buf[StringBufSize]) const *ptr++ = '-'; } (void)snprintf(ptr, StringBufSize - 1, "%llu.%06lu", - static_cast(std::abs(toUSec() / 1000000L)), - static_cast(std::abs(toUSec() % 1000000L))); + static_cast(getAbs().toUSec() / 1000000L), + static_cast(getAbs().toUSec() % 1000000L)); } diff --git a/libuavcan/include/uavcan/util/compile_time.hpp b/libuavcan/include/uavcan/util/compile_time.hpp index 24777cbf1e..438f426be3 100644 --- a/libuavcan/include/uavcan/util/compile_time.hpp +++ b/libuavcan/include/uavcan/util/compile_time.hpp @@ -136,4 +136,109 @@ template <> struct UAVCAN_EXPORT CompileTimeIntSqrt<49> { enum { Result = 7 }; } template <> struct UAVCAN_EXPORT CompileTimeIntSqrt<64> { enum { Result = 8 }; }; template <> struct UAVCAN_EXPORT CompileTimeIntSqrt<81> { enum { Result = 9 }; }; +/** + * Replacement for std::copy(..) + */ +template +UAVCAN_EXPORT +OutputIt copy(InputIt first, InputIt last, OutputIt result) +{ + while (first != last) + { + *result = *first; + ++first; + ++result; + } + return result; +} + +/** + * Replacement for std::fill(..) + */ +template +UAVCAN_EXPORT +void fill(ForwardIt first, ForwardIt last, const T& value) +{ + while (first != last) + { + *first = value; + ++first; + } +} + +/** + * Replacement for std::fill_n(..) + */ +template +UAVCAN_EXPORT +void fill_n(OutputIt first, std::size_t n, const T& value) +{ + while (n--) + { + *first++ = value; + } +} + +/** + * Replacement for std::min(..) + */ +template +UAVCAN_EXPORT +const T& min(const T& a, const T& b) +{ + return (b < a) ? b : a; +} + +/** + * Replacement for std::max(..) + */ +template +UAVCAN_EXPORT +const T& max(const T& a, const T& b) +{ + return (a < b) ? b : a; +} + +/** + * Replacement for std::lexicographical_compare(..) + */ +template +UAVCAN_EXPORT +bool lexicographical_compare(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2) +{ + while ((first1 != last1) && (first2 != last2)) + { + if (*first1 < *first2) + { + return true; + } + if (*first2 < *first1) + { + return false; + } + ++first1; + ++first2; + } + return (first1 == last1) && (first2 != last2); +} + +/** + * Replacement for std::equal(..) + */ +template +UAVCAN_EXPORT +bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2) +{ + while (first1 != last1) + { + if (*first1 != *first2) + { + return false; + } + ++first1; + ++first2; + } + return true; +} + } diff --git a/libuavcan/include/uavcan/util/lazy_constructor.hpp b/libuavcan/include/uavcan/util/lazy_constructor.hpp index 7c628974cf..5cd4903a69 100644 --- a/libuavcan/include/uavcan/util/lazy_constructor.hpp +++ b/libuavcan/include/uavcan/util/lazy_constructor.hpp @@ -7,6 +7,7 @@ #include #include #include +#include #ifndef UAVCAN_CPP_VERSION # error UAVCAN_CPP_VERSION @@ -53,13 +54,13 @@ public: LazyConstructor() : ptr_(NULL) { - std::fill(data_.pool, data_.pool + sizeof(T), 0); + fill(data_.pool, data_.pool + sizeof(T), 0); } LazyConstructor(const LazyConstructor& rhs) // Implicit : ptr_(NULL) { - std::fill(data_.pool, data_.pool + sizeof(T), 0); + fill(data_.pool, data_.pool + sizeof(T), 0); if (rhs) { construct(*rhs); // Invoke copy constructor @@ -95,7 +96,7 @@ public: ptr_->~T(); } ptr_ = NULL; - std::fill(data_.pool, data_.pool + sizeof(T), 0); + fill(data_.pool, data_.pool + sizeof(T), 0); } void construct() diff --git a/libuavcan/src/driver/uc_can.cpp b/libuavcan/src/driver/uc_can.cpp index 81b1b24345..f60696b79c 100644 --- a/libuavcan/src/driver/uc_can.cpp +++ b/libuavcan/src/driver/uc_can.cpp @@ -68,7 +68,7 @@ std::string CanFrame::toString(StringRepresentation mode) const char buf[50]; char* wpos = buf; char* const epos = buf + sizeof(buf); - std::fill(buf, buf + sizeof(buf), 0); + fill(buf, buf + sizeof(buf), 0); if (id & FlagEFF) { diff --git a/libuavcan/src/marshal/uc_bit_stream.cpp b/libuavcan/src/marshal/uc_bit_stream.cpp index 901357cf71..ca7ccb463e 100644 --- a/libuavcan/src/marshal/uc_bit_stream.cpp +++ b/libuavcan/src/marshal/uc_bit_stream.cpp @@ -22,7 +22,7 @@ int BitStream::write(const uint8_t* bytes, const int bitlen) assert(MaxBytesPerRW >= bytelen); tmp[0] = tmp[bytelen - 1] = 0; - std::fill(tmp, tmp + bytelen, 0); + fill(tmp, tmp + bytelen, 0); copyBitArray(bytes, 0, bitlen, tmp, bit_offset_ % 8); const int new_bit_offset = bit_offset_ + bitlen; @@ -69,7 +69,7 @@ int BitStream::read(uint8_t* bytes, const int bitlen) return ResultOutOfBuffer; } - std::fill(bytes, bytes + bitlenToBytelen(bitlen), 0); + fill(bytes, bytes + bitlenToBytelen(bitlen), 0); copyBitArray(tmp, bit_offset_ % 8, bitlen, bytes, 0); bit_offset_ += bitlen; return ResultOk; diff --git a/libuavcan/src/marshal/uc_float_spec.cpp b/libuavcan/src/marshal/uc_float_spec.cpp index 8aad065401..bc2a6921e5 100644 --- a/libuavcan/src/marshal/uc_float_spec.cpp +++ b/libuavcan/src/marshal/uc_float_spec.cpp @@ -83,7 +83,7 @@ uint16_t IEEE754Converter::nativeNonIeeeToHalf(float value) } const int32_t ival = static_cast(value); hbits |= static_cast(((ival < 0) ? (-ival) : ival) & 0x3FFU); - float diff = std::abs(value - static_cast(ival)); + float diff = std::fabs(value - static_cast(ival)); hbits += diff >= 0.5F; return hbits; } diff --git a/libuavcan/src/node/uc_generic_publisher.cpp b/libuavcan/src/node/uc_generic_publisher.cpp index 94f2255970..3ebc7bd699 100644 --- a/libuavcan/src/node/uc_generic_publisher.cpp +++ b/libuavcan/src/node/uc_generic_publisher.cpp @@ -64,8 +64,8 @@ TransferSender* GenericPublisherBase::getTransferSender() void GenericPublisherBase::setTxTimeout(MonotonicDuration tx_timeout) { - tx_timeout = std::max(tx_timeout, getMinTxTimeout()); - tx_timeout = std::min(tx_timeout, getMaxTxTimeout()); + tx_timeout = max(tx_timeout, getMinTxTimeout()); + tx_timeout = min(tx_timeout, getMaxTxTimeout()); tx_timeout_ = tx_timeout; } diff --git a/libuavcan/src/node/uc_scheduler.cpp b/libuavcan/src/node/uc_scheduler.cpp index 7f06955662..be22c21075 100644 --- a/libuavcan/src/node/uc_scheduler.cpp +++ b/libuavcan/src/node/uc_scheduler.cpp @@ -128,7 +128,7 @@ MonotonicTime DeadlineScheduler::getEarliestDeadline() const */ MonotonicTime Scheduler::computeDispatcherSpinDeadline(MonotonicTime spin_deadline) const { - const MonotonicTime earliest = std::min(deadline_scheduler_.getEarliestDeadline(), spin_deadline); + const MonotonicTime earliest = min(deadline_scheduler_.getEarliestDeadline(), spin_deadline); const MonotonicTime ts = getMonotonicTime(); if (earliest > ts) { diff --git a/libuavcan/src/transport/uc_frame.cpp b/libuavcan/src/transport/uc_frame.cpp index 7c1814b704..286f1a871e 100644 --- a/libuavcan/src/transport/uc_frame.cpp +++ b/libuavcan/src/transport/uc_frame.cpp @@ -40,8 +40,8 @@ int Frame::setPayload(const uint8_t* data, unsigned len) { return maxlen; } - len = std::min(unsigned(maxlen), len); - (void)std::copy(data, data + len, payload_); + len = min(unsigned(maxlen), len); + (void)copy(data, data + len, payload_); payload_len_ = len; return len; } @@ -85,7 +85,7 @@ bool Frame::parse(const CanFrame& can_frame) { dst_node_id_ = NodeID::Broadcast; payload_len_ = can_frame.dlc; - (void)std::copy(can_frame.data, can_frame.data + can_frame.dlc, payload_); + (void)copy(can_frame.data, can_frame.data + can_frame.dlc, payload_); break; } case TransferTypeServiceResponse: @@ -102,7 +102,7 @@ bool Frame::parse(const CanFrame& can_frame) } dst_node_id_ = can_frame.data[0] & 0x7F; payload_len_ = can_frame.dlc - 1; - (void)std::copy(can_frame.data + 1, can_frame.data + can_frame.dlc, payload_); + (void)copy(can_frame.data + 1, can_frame.data + can_frame.dlc, payload_); break; } default: @@ -142,7 +142,7 @@ bool Frame::compile(CanFrame& out_can_frame) const case TransferTypeMessageBroadcast: { out_can_frame.dlc = payload_len_; - (void)std::copy(payload_, payload_ + payload_len_, out_can_frame.data); + (void)copy(payload_, payload_ + payload_len_, out_can_frame.data); break; } case TransferTypeServiceResponse: @@ -152,7 +152,7 @@ bool Frame::compile(CanFrame& out_can_frame) const assert((payload_len_ + 1) <= sizeof(out_can_frame.data)); out_can_frame.data[0] = dst_node_id_.get(); out_can_frame.dlc = payload_len_ + 1; - (void)std::copy(payload_, payload_ + payload_len_, out_can_frame.data + 1); + (void)copy(payload_, payload_ + payload_len_, out_can_frame.data + 1); break; } default: @@ -192,7 +192,7 @@ bool Frame::operator==(const Frame& rhs) const (transfer_id_ == rhs.transfer_id_) && (last_frame_ == rhs.last_frame_) && (payload_len_ == rhs.payload_len_) && - std::equal(payload_, payload_ + payload_len_, rhs.payload_); + equal(payload_, payload_ + payload_len_, rhs.payload_); } #if UAVCAN_TOSTRING diff --git a/libuavcan/src/transport/uc_transfer_buffer.cpp b/libuavcan/src/transport/uc_transfer_buffer.cpp index 7971e46a0f..ae5d4b8c45 100644 --- a/libuavcan/src/transport/uc_transfer_buffer.cpp +++ b/libuavcan/src/transport/uc_transfer_buffer.cpp @@ -216,7 +216,7 @@ int DynamicTransferBufferManagerEntry::write(unsigned offset, const uint8_t* dat } const int actually_written = len - left_to_write; - max_write_pos_ = std::max(offset + actually_written, unsigned(max_write_pos_)); + max_write_pos_ = max(offset + actually_written, unsigned(max_write_pos_)); return actually_written; } @@ -239,7 +239,7 @@ int StaticTransferBufferImpl::read(unsigned offset, uint8_t* data, unsigned len) len = max_write_pos_ - offset; } assert((offset + len) <= max_write_pos_); - (void)std::copy(data_ + offset, data_ + offset + len, data); + (void)copy(data_ + offset, data_ + offset + len, data); return len; } @@ -259,8 +259,8 @@ int StaticTransferBufferImpl::write(unsigned offset, const uint8_t* data, unsign len = size_ - offset; } assert((offset + len) <= size_); - (void)std::copy(data, data + len, data_ + offset); - max_write_pos_ = std::max(offset + len, unsigned(max_write_pos_)); + (void)copy(data, data + len, data_ + offset); + max_write_pos_ = max(offset + len, unsigned(max_write_pos_)); return len; } @@ -268,7 +268,7 @@ void StaticTransferBufferImpl::reset() { max_write_pos_ = 0; #if UAVCAN_DEBUG - std::fill(data_, data_ + size_, 0); + fill(data_, data_ + size_, 0); #endif } diff --git a/libuavcan/src/transport/uc_transfer_listener.cpp b/libuavcan/src/transport/uc_transfer_listener.cpp index 82b25421a8..095a976036 100644 --- a/libuavcan/src/transport/uc_transfer_listener.cpp +++ b/libuavcan/src/transport/uc_transfer_listener.cpp @@ -46,7 +46,7 @@ int SingleFrameIncomingTransfer::read(unsigned offset, uint8_t* data, unsigned l len = payload_len_ - offset; } assert((offset + len) <= payload_len_); - (void)std::copy(payload_ + offset, payload_ + offset + len, data); + (void)copy(payload_ + offset, payload_ + offset + len, data); return len; } diff --git a/libuavcan/src/transport/uc_transfer_receiver.cpp b/libuavcan/src/transport/uc_transfer_receiver.cpp index 25ada2ba06..0ae5598ab1 100644 --- a/libuavcan/src/transport/uc_transfer_receiver.cpp +++ b/libuavcan/src/transport/uc_transfer_receiver.cpp @@ -52,8 +52,8 @@ void TransferReceiver::updateTransferTimings() if ((!prev_prev_ts.isZero()) && (!prev_transfer_ts_.isZero()) && (prev_transfer_ts_ >= prev_prev_ts)) { uint64_t interval_usec = (prev_transfer_ts_ - prev_prev_ts).toUSec(); - interval_usec = std::min(interval_usec, uint64_t(MaxTransferIntervalUSec)); - interval_usec = std::max(interval_usec, uint64_t(MinTransferIntervalUSec)); + interval_usec = min(interval_usec, uint64_t(MaxTransferIntervalUSec)); + interval_usec = max(interval_usec, uint64_t(MinTransferIntervalUSec)); transfer_interval_usec_ = static_cast((uint64_t(transfer_interval_usec_) * 7 + interval_usec) / 8); } } diff --git a/libuavcan/src/transport/uc_transfer_sender.cpp b/libuavcan/src/transport/uc_transfer_sender.cpp index 97dd53a1b8..caf6791dad 100644 --- a/libuavcan/src/transport/uc_transfer_sender.cpp +++ b/libuavcan/src/transport/uc_transfer_sender.cpp @@ -49,7 +49,7 @@ int TransferSender::send(const uint8_t* payload, int payload_len, MonotonicTime buf[0] = crc.get() & 0xFF; // Transfer CRC, little endian buf[1] = (crc.get() >> 8) & 0xFF; - (void)std::copy(payload, payload + BUFLEN - 2, buf + 2); + (void)copy(payload, payload + BUFLEN - 2, buf + 2); const int write_res = frame.setPayload(buf, BUFLEN); if (write_res < 2) diff --git a/libuavcan/src/uc_dynamic_memory.cpp b/libuavcan/src/uc_dynamic_memory.cpp index 85e2a03163..6e8564aff1 100644 --- a/libuavcan/src/uc_dynamic_memory.cpp +++ b/libuavcan/src/uc_dynamic_memory.cpp @@ -45,7 +45,7 @@ std::size_t LimitedPoolAllocator::getBlockSize() const std::size_t LimitedPoolAllocator::getNumBlocks() const { - return std::min(max_blocks_, allocator_.getNumBlocks()); + return min(max_blocks_, allocator_.getNumBlocks()); } }