Skip to content

Commit

Permalink
In C++ use std::max and std::min instead of the max() and min() macro…
Browse files Browse the repository at this point in the history
…s. (#1368)

The old Arduino libs can still continue to use the macros.
  • Loading branch information
slaff authored Apr 24, 2018
1 parent cd7a1d3 commit ff7fb1e
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 20 deletions.
9 changes: 9 additions & 0 deletions Sming/SmingCore/ArduinoCompat.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@
extern "C" {
#endif

#define abs(x) ((x)>0?(x):-(x))
#ifndef min
#define min(a,b) ((a)<(b)?(a):(b))
#endif
#ifndef max
#define max(a,b) ((a)>(b)?(a):(b))
#endif
#define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))

void yield();

#ifdef __cplusplus
Expand Down
7 changes: 4 additions & 3 deletions Sming/SmingCore/DataSourceStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
****/

#include "../SmingCore/DataSourceStream.h"
#include <algorithm>

int IDataSourceStream::read()
{
Expand Down Expand Up @@ -91,7 +92,7 @@ size_t MemoryDataStream::write(const uint8_t* data, size_t len)

uint16_t MemoryDataStream::readMemoryBlock(char* data, int bufSize)
{
int available = min(size - (pos - buf), bufSize);
int available = std::min(size - (pos - buf), bufSize);
memcpy(data, pos, available);
return available;
}
Expand Down Expand Up @@ -154,7 +155,7 @@ FileStream::~FileStream()

uint16_t FileStream::readMemoryBlock(char* data, int bufSize)
{
int len = min(bufSize, size - pos);
int len = std::min(bufSize, size - pos);
int available = fileRead(handle, data, len);
fileSeek(handle, pos, eSO_FileStart); // Don't move cursor now (waiting seek)
if(available < 0) {
Expand Down Expand Up @@ -250,7 +251,7 @@ uint16_t TemplateFileStream::readMemoryBlock(char* data, int bufSize)
debug_d("var %s not found", varName.c_str());
state = eTES_Wait;
int len = FileStream::readMemoryBlock(data, bufSize);
return min(len, skipBlockSize);
return std::min(len, skipBlockSize);
}
}
else if (state == eTES_SendingVar)
Expand Down
4 changes: 3 additions & 1 deletion Sming/SmingCore/Network/Http/HttpRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

#include "HttpRequest.h"

#include <algorithm>

HttpRequest::HttpRequest(const URL& uri) {
this->uri = uri;
}
Expand Down Expand Up @@ -159,7 +161,7 @@ String HttpRequest::getBody()
char buf[1024];
while(stream->available() > 0) {
int available = memory->readMemoryBlock(buf, 1024);
memory->seek(max(available, 0));
memory->seek(std::max(available, 0));
ret += String(buf, available);
if(available < 1024) {
break;
Expand Down
4 changes: 3 additions & 1 deletion Sming/SmingCore/Network/Http/Stream/HttpChunkedStream.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "HttpChunkedStream.h"

#include <algorithm>

HttpChunkedStream::HttpChunkedStream(ReadWriteStream *stream)
{
this->stream = stream;
Expand Down Expand Up @@ -43,7 +45,7 @@ uint16_t HttpChunkedStream::readMemoryBlock(char* data, int bufSize)
int len = readSize;
char buffer[len];
len = stream->readMemoryBlock(buffer, len);
stream->seek(max(len, 0));
stream->seek(std::max(len, 0));
if(len < 1) {
return 0;
}
Expand Down
4 changes: 2 additions & 2 deletions Sming/SmingCore/Network/MqttClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void MqttClient::setPingRepeatTime(int seconds)
{
if (PingRepeatTime > keepAlive)
PingRepeatTime = keepAlive;
else
else
PingRepeatTime = seconds;
}

Expand Down Expand Up @@ -239,7 +239,7 @@ err_t MqttClient::onReceive(pbuf *buf)
continue;
}

int available = min(waitingSize, buf->tot_len - received);
int available = std::min(waitingSize, buf->tot_len - received);
waitingSize -= available;
if (current != NULL)
{
Expand Down
6 changes: 4 additions & 2 deletions Sming/SmingCore/Network/TcpConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include "../Wiring/WString.h"
#include "../Wiring/IPAddress.h"

#include <algorithm>

TcpConnection::TcpConnection(bool autoDestruct) : autoSelfDestruct(autoDestruct), sleep(0), canSend(true), timeOut(70)
{

Expand Down Expand Up @@ -265,7 +267,7 @@ int TcpConnection::write(IDataSourceStream* stream)
do
{
pushCount++;
int read = min(NETWORK_SEND_BUFFER_SIZE, getAvailableWriteSize());
int read = std::min((uint16_t)NETWORK_SEND_BUFFER_SIZE, getAvailableWriteSize());
if (read > 0)
available = stream->readMemoryBlock(buffer, read);
else
Expand All @@ -275,7 +277,7 @@ int TcpConnection::write(IDataSourceStream* stream)
{
int written = write(buffer, available, TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE);
total += written;
stream->seek(max(written, 0));
stream->seek(std::max(written, 0));
debug_d("Written: %d, Available: %d, isFinished: %d, PushCount: %d [TcpBuf: %d]", written, available, (stream->isFinished()?1:0), pushCount, tcp_sndbuf(tcp));
repeat = written == available && !stream->isFinished() && pushCount < 25;
}
Expand Down
4 changes: 2 additions & 2 deletions Sming/SmingCore/SPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ void SPIClass::transfer(uint8 *buffer, size_t numberBytes) {
while (blocks--) {

// get full BLOCKSIZE or number of remaining bytes
bufLenght = min(numberBytes-bufIndx, (unsigned int)BLOCKSIZE);
bufLenght = std::min(numberBytes-bufIndx, (unsigned int)BLOCKSIZE);

#ifdef SPI_DEBUG
debugf("Write/Read Block %d total %d bytes", total-blocks, bufLenght);
Expand Down Expand Up @@ -453,7 +453,7 @@ void SPIClass::setFrequency(int freq) {
return;
}

freq = min(freq, _CPU_freq/2);
freq = std::min(freq, _CPU_freq/2);
_SPISettings._speed = freq;


Expand Down
3 changes: 0 additions & 3 deletions Sming/SmingCore/SmingCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@

#include <functional>

#define min(A, B) std::min(A, B)
#define max(A, B) std::max(A, B)

#include "../Wiring/WiringFrameworkIncludes.h"

#include "Delegate.h"
Expand Down
12 changes: 6 additions & 6 deletions Sming/Wiring/WConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,12 @@

#define sq(x) ((x)*(x))
//#define abs(x) ((x)>0?(x):-(x))
#ifndef min
#define min(a,b) ((a)<(b)?(a):(b))
#endif
#ifndef max
#define max(a,b) ((a)>(b)?(a):(b))
#endif
//#ifndef min
//#define min(a,b) ((a)<(b)?(a):(b))
//#endif
//#ifndef max
//#define max(a,b) ((a)>(b)?(a):(b))
//#endif
//#define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))
#define radians(deg) ((deg)*DEG_TO_RAD)
#define degrees(rad) ((rad)*RAD_TO_DEG)
Expand Down

0 comments on commit ff7fb1e

Please sign in to comment.