diff --git a/Sming/Components/Hosted/include/Hosted/Client.h b/Sming/Components/Hosted/include/Hosted/Client.h index ece7a59054..55c981c904 100644 --- a/Sming/Components/Hosted/include/Hosted/Client.h +++ b/Sming/Components/Hosted/include/Hosted/Client.h @@ -154,8 +154,8 @@ class Client host_debug_i("Available commands:"); - for(size_t i = 0; i < commands.count(); i++) { - host_debug_i("\t%s => %d", commands.keyAt(i).c_str(), commands.valueAt(i)); + for(auto cmd : commands) { + host_debug_i("\t%s => %d", cmd.key().c_str(), cmd.value()); } host_debug_i("Connected. Starting application"); diff --git a/Sming/Components/Network/src/Network/Http/HttpClientConnection.cpp b/Sming/Components/Network/src/Network/Http/HttpClientConnection.cpp index cac3f32ce5..d939bfb915 100644 --- a/Sming/Components/Network/src/Network/Http/HttpClientConnection.cpp +++ b/Sming/Components/Network/src/Network/Http/HttpClientConnection.cpp @@ -367,9 +367,9 @@ void HttpClientConnection::sendRequestHeaders(HttpRequest* request) request->headers[HTTP_HEADER_TRANSFER_ENCODING] = _F("chunked"); } - for(unsigned i = 0; i < request->headers.count(); i++) { + for(auto hdr : request->headers) { // TODO: add name and/or value escaping (implement in HttpHeaders) - sendString(request->headers[i]); + sendString(hdr); } sendString("\r\n"); } diff --git a/Sming/Components/Network/src/Network/Http/HttpHeaders.cpp b/Sming/Components/Network/src/Network/Http/HttpHeaders.cpp index 9f98985693..1dfc8acd9f 100644 --- a/Sming/Components/Network/src/Network/Http/HttpHeaders.cpp +++ b/Sming/Components/Network/src/Network/Http/HttpHeaders.cpp @@ -11,6 +11,25 @@ #include "HttpHeaders.h" #include +String HttpHeaders::HeaderConst::getFieldName() const +{ + return fields.toString(key()); +} + +size_t HttpHeaders::HeaderConst::printTo(Print& p) const +{ + size_t n{0}; + n += p.print(getFieldName()); + n += p.print(" = "); + n += p.print(value()); + return n; +} + +HttpHeaders::HeaderConst::operator String() const +{ + return fields.toString(key(), value()); +} + const String& HttpHeaders::operator[](const String& name) const { auto field = fromString(name); @@ -40,9 +59,7 @@ bool HttpHeaders::append(const HttpHeaderFieldName& name, const String& value) void HttpHeaders::setMultiple(const HttpHeaders& headers) { - for(unsigned i = 0; i < headers.count(); i++) { - HttpHeaderFieldName fieldName = headers.keyAt(i); - auto fieldNameString = headers.toString(fieldName); - operator[](fieldNameString) = headers.valueAt(i); + for(auto hdr : headers) { + operator[](hdr.getFieldName()) = hdr.value(); } } diff --git a/Sming/Components/Network/src/Network/Http/HttpHeaders.h b/Sming/Components/Network/src/Network/Http/HttpHeaders.h index 8d181c6eae..af94df076e 100644 --- a/Sming/Components/Network/src/Network/Http/HttpHeaders.h +++ b/Sming/Components/Network/src/Network/Http/HttpHeaders.h @@ -34,6 +34,44 @@ class HttpHeaders : public HttpHeaderFields, private HashMap { public: + class HeaderConst : public BaseElement + { + public: + HeaderConst(const HttpHeaderFields& fields, const HttpHeaderFieldName& key, const String& value) + : BaseElement(key, value), fields(fields) + { + } + + String getFieldName() const; + operator String() const; + size_t printTo(Print& p) const; + + private: + const HttpHeaderFields& fields; + }; + + class Iterator : public HashMap::Iterator + { + public: + Iterator(const HttpHeaders& headers, unsigned index) + : HashMap::Iterator::Iterator(headers, index), fields(headers) + { + } + + HeaderConst operator*() + { + return HeaderConst(fields, map.keyAt(index), map.valueAt(index)); + } + + HeaderConst operator*() const + { + return HeaderConst(fields, map.keyAt(index), map.valueAt(index)); + } + + private: + const HttpHeaderFields& fields; + }; + HttpHeaders() = default; HttpHeaders(const HttpHeaders& headers) @@ -41,6 +79,16 @@ class HttpHeaders : public HttpHeaderFields, private HashMap String operator[](const BaseElement& elem) const + { + return toString(elem.key(), elem.value()); + } + using HashMap::contains; /** diff --git a/Sming/Components/Network/src/Network/Http/HttpRequest.cpp b/Sming/Components/Network/src/Network/Http/HttpRequest.cpp index cf330c6867..5f598f8c70 100644 --- a/Sming/Components/Network/src/Network/Http/HttpRequest.cpp +++ b/Sming/Components/Network/src/Network/Http/HttpRequest.cpp @@ -77,8 +77,8 @@ String HttpRequest::toString() const if(!headers.contains(HTTP_HEADER_HOST)) { content += headers.toString(HTTP_HEADER_HOST, uri.getHostWithPort()); } - for(unsigned i = 0; i < headers.count(); i++) { - content += headers[i]; + for(auto hdr : headers) { + content += hdr; } if(!headers.contains(HTTP_HEADER_CONTENT_LENGTH)) { diff --git a/Sming/Components/Network/src/Network/Http/HttpResponse.cpp b/Sming/Components/Network/src/Network/Http/HttpResponse.cpp index dadcd817a1..559cec1b27 100644 --- a/Sming/Components/Network/src/Network/Http/HttpResponse.cpp +++ b/Sming/Components/Network/src/Network/Http/HttpResponse.cpp @@ -137,8 +137,8 @@ String HttpResponse::toString() const content += ' '; content += ::toString(code); content += " \r\n"; - for(unsigned i = 0; i < headers.count(); i++) { - content += headers[i]; + for(auto hdr : headers) { + content += hdr; } content += "\r\n"; diff --git a/Sming/Components/Network/src/Network/Http/HttpServerConnection.cpp b/Sming/Components/Network/src/Network/Http/HttpServerConnection.cpp index 2dacf1a12a..37699a5238 100644 --- a/Sming/Components/Network/src/Network/Http/HttpServerConnection.cpp +++ b/Sming/Components/Network/src/Network/Http/HttpServerConnection.cpp @@ -312,8 +312,8 @@ void HttpServerConnection::sendResponseHeaders(HttpResponse* response) response->headers[HTTP_HEADER_DATE] = DateTime(SystemClock.now(eTZ_UTC)).toHTTPDate(); } - for(unsigned i = 0; i < response->headers.count(); i++) { - sendString(response->headers[i]); + for(auto hdr : response->headers) { + sendString(hdr); } sendString("\r\n"); } diff --git a/Sming/Components/Network/src/Network/Http/Websocket/WebsocketConnection.cpp b/Sming/Components/Network/src/Network/Http/Websocket/WebsocketConnection.cpp index d161532d58..81d1c46cb4 100644 --- a/Sming/Components/Network/src/Network/Http/Websocket/WebsocketConnection.cpp +++ b/Sming/Components/Network/src/Network/Http/Websocket/WebsocketConnection.cpp @@ -292,9 +292,9 @@ void WebsocketConnection::broadcast(const char* message, size_t length, ws_frame memcpy(copy, message, length); std::shared_ptr data(copy, [](const char* ptr) { delete[] ptr; }); - for(unsigned i = 0; i < websocketList.count(); i++) { + for(auto skt : websocketList) { auto stream = new SharedMemoryStream(data, length); - websocketList[i]->send(stream, type); + skt->send(stream, type); } } diff --git a/Sming/Components/Network/src/Network/SmtpClient.cpp b/Sming/Components/Network/src/Network/SmtpClient.cpp index 5f60c5fc27..5f44450ba2 100644 --- a/Sming/Components/Network/src/Network/SmtpClient.cpp +++ b/Sming/Components/Network/src/Network/SmtpClient.cpp @@ -306,8 +306,8 @@ void SmtpClient::sendMailHeaders(MailMessage* mail) mail->stream = mStream; } - for(unsigned i = 0; i < mail->headers.count(); i++) { - sendString(mail->headers[i]); + for(auto hdr : mail->headers) { + sendString(hdr); } sendString("\r\n"); } diff --git a/Sming/Components/Network/src/Network/TcpServer.cpp b/Sming/Components/Network/src/Network/TcpServer.cpp index 926a750c0e..e64a223a4d 100644 --- a/Sming/Components/Network/src/Network/TcpServer.cpp +++ b/Sming/Components/Network/src/Network/TcpServer.cpp @@ -164,13 +164,10 @@ void TcpServer::shutdown() return; } - for(unsigned i = 0; i < connections.count(); i++) { - TcpConnection* connection = connections[i]; - if(connection == nullptr) { - continue; + for(auto& connection : connections) { + if(connection != nullptr) { + connection->setTimeOut(1); } - - connection->setTimeOut(1); } } diff --git a/Sming/Core/Data/ObjectMap.h b/Sming/Core/Data/ObjectMap.h index af75a16870..19c2acec90 100644 --- a/Sming/Core/Data/ObjectMap.h +++ b/Sming/Core/Data/ObjectMap.h @@ -208,7 +208,7 @@ template class ObjectMap */ void set(const K& key, V* value) { - int i = indexOf(key); + int i = entries.indexOf(key); if(i >= 0) { entries[i].value.reset(value); } else { @@ -224,7 +224,7 @@ template class ObjectMap */ V* find(const K& key) const { - int index = indexOf(key); + int index = entries.indexOf(key); return (index < 0) ? nullptr : entries[index].value.get(); } @@ -235,12 +235,7 @@ template class ObjectMap */ int indexOf(const K& key) const { - for(unsigned i = 0; i < entries.count(); i++) { - if(entries[i].key == key) { - return i; - } - } - return -1; + return entries.indexOf(key); } /** @@ -250,7 +245,7 @@ template class ObjectMap */ bool contains(const K& key) const { - return indexOf(key) >= 0; + return entries.contains(key); } /** @@ -272,10 +267,9 @@ template class ObjectMap int index = indexOf(key); if(index < 0) { return false; - } else { - removeAt(index); - return true; } + removeAt(index); + return true; } /** @@ -322,6 +316,11 @@ template class ObjectMap K key; std::unique_ptr value; + bool operator==(const K& keyToFind) const + { + return key == keyToFind; + } + Entry(const K& key, V* value) : key(key) { this->value.reset(value); diff --git a/Sming/Core/Data/ObjectQueue.h b/Sming/Core/Data/ObjectQueue.h index 9108764b70..3470dc4219 100644 --- a/Sming/Core/Data/ObjectQueue.h +++ b/Sming/Core/Data/ObjectQueue.h @@ -31,11 +31,11 @@ template class ObjectQueue : public FIFO T* peek() const { - return FIFO::count() ? FIFO::peek() : nullptr; + return this->count() ? FIFO::peek() : nullptr; } T* dequeue() { - return FIFO::count() ? FIFO::dequeue() : nullptr; + return this->count() ? FIFO::dequeue() : nullptr; } }; diff --git a/Sming/Libraries/DiskStorage b/Sming/Libraries/DiskStorage index 3c649c03d4..6bfcd80140 160000 --- a/Sming/Libraries/DiskStorage +++ b/Sming/Libraries/DiskStorage @@ -1 +1 @@ -Subproject commit 3c649c03d4a2e7b21c3648184ad8ebe9e0723c96 +Subproject commit 6bfcd80140f1436f6da90d6ab6a438903f8fa752 diff --git a/Sming/Libraries/Yeelight/YeelightBulb.cpp b/Sming/Libraries/Yeelight/YeelightBulb.cpp index 132ec579c7..5cb8b8f71a 100644 --- a/Sming/Libraries/Yeelight/YeelightBulb.cpp +++ b/Sming/Libraries/Yeelight/YeelightBulb.cpp @@ -49,11 +49,11 @@ void YeelightBulb::sendCommand(const String& method, const Vector& param doc["id"] = requestId++; doc["method"] = method; auto arr = doc.createNestedArray("params"); - for(unsigned i = 0; i < params.count(); i++) { - if(isNumeric(params[i])) { - arr.add(params[i].toInt()); + for(auto& param : params) { + if(isNumeric(param)) { + arr.add(param.toInt()); } else { - arr.add(params[i]); + arr.add(param); } } String request = Json::serialize(doc); diff --git a/Sming/Wiring/WHashMap.h b/Sming/Wiring/WHashMap.h index 18dbd4bbb2..923593d8cc 100644 --- a/Sming/Wiring/WHashMap.h +++ b/Sming/Wiring/WHashMap.h @@ -165,7 +165,7 @@ template class HashMap return ElementConst{map.keyAt(index), map.valueAt(index)}; } - private: + protected: Map& map; unsigned index{0}; }; diff --git a/Sming/Wiring/WVector.h b/Sming/Wiring/WVector.h index 6d6856326c..eb2f97a493 100644 --- a/Sming/Wiring/WVector.h +++ b/Sming/Wiring/WVector.h @@ -113,7 +113,7 @@ template class Vector : public Countable return _data.size; } - bool contains(const Element& elem) const + template bool contains(const T& elem) const { return indexOf(elem) >= 0; } @@ -127,7 +127,7 @@ template class Vector : public Countable return _data[0]; } - int indexOf(const Element& elem) const; + template int indexOf(const T& elem) const; bool isEmpty() const { @@ -143,7 +143,7 @@ template class Vector : public Countable return _data[_size - 1]; } - int lastIndexOf(const Element& elem) const; + template int lastIndexOf(const T& elem) const; unsigned int count() const override { @@ -178,9 +178,9 @@ template class Vector : public Countable _size = 0; } - bool removeElement(const Element& obj) + template bool removeElement(const T& elem) { - return removeElementAt(indexOf(obj)); + return removeElementAt(indexOf(elem)); } /** @@ -316,7 +316,7 @@ template void Vector::copyInto(Element* array) const } } -template int Vector::indexOf(const Element& elem) const +template template int Vector::indexOf(const T& elem) const { for(unsigned int i = 0; i < _size; i++) { if(_data[i] == elem) { @@ -327,7 +327,7 @@ template int Vector::indexOf(const Element& elem) const return -1; } -template int Vector::lastIndexOf(const Element& elem) const +template template int Vector::lastIndexOf(const T& elem) const { // check for empty vector if(_size == 0) { diff --git a/samples/Basic_Ssl/app/application.cpp b/samples/Basic_Ssl/app/application.cpp index 6c89546433..e0b9fae5eb 100644 --- a/samples/Basic_Ssl/app/application.cpp +++ b/samples/Basic_Ssl/app/application.cpp @@ -40,8 +40,8 @@ int onDownload(HttpConnection& connection, bool success) Serial << _F(", received ") << stream->available() << _F(" bytes") << endl; auto& headers = connection.getResponse()->headers; - for(unsigned i = 0; i < headers.count(); ++i) { - Serial.print(headers[i]); + for(auto hdr : headers) { + Serial.print(hdr); } auto ssl = connection.getSsl(); diff --git a/samples/Basic_WiFi/app/application.cpp b/samples/Basic_WiFi/app/application.cpp index a9eed2e2b9..8b1f26c94f 100644 --- a/samples/Basic_WiFi/app/application.cpp +++ b/samples/Basic_WiFi/app/application.cpp @@ -14,9 +14,9 @@ void listNetworks(bool succeeded, BssList& list) return; } - for(unsigned i = 0; i < list.count(); i++) { - Serial << _F("\tWiFi: ") << list[i].ssid << ", " << list[i].getAuthorizationMethodName(); - if(list[i].hidden) { + for(auto& bss : list) { + Serial << _F("\tWiFi: ") << bss.ssid << ", " << bss.getAuthorizationMethodName(); + if(bss.hidden) { Serial << _F(" (hidden)"); } Serial.println(); diff --git a/samples/HttpClient_Instapush/app/application.cpp b/samples/HttpClient_Instapush/app/application.cpp index f7c2d0333c..5575d65e95 100644 --- a/samples/HttpClient_Instapush/app/application.cpp +++ b/samples/HttpClient_Instapush/app/application.cpp @@ -50,9 +50,9 @@ class InstapushApplication : protected HttpClient DynamicJsonDocument root(1024); root["event"] = event; JsonObject trackers = root.createNestedObject("trackers"); - for(unsigned i = 0; i < trackersInfo.count(); i++) { - debugf("%s: %s", trackersInfo.keyAt(i).c_str(), trackersInfo.valueAt(i).c_str()); - trackers[trackersInfo.keyAt(i)] = trackersInfo[trackersInfo.keyAt(i)]; + for(auto info : trackersInfo) { + debugf("%s: %s", info.key().c_str(), info.value().c_str()); + trackers[info.key()] = info.value(); } auto stream = new MemoryDataStream; diff --git a/samples/HttpServer_WebSockets/app/CUserData.cpp b/samples/HttpServer_WebSockets/app/CUserData.cpp index 801ea01470..f12e893988 100644 --- a/samples/HttpServer_WebSockets/app/CUserData.cpp +++ b/samples/HttpServer_WebSockets/app/CUserData.cpp @@ -10,14 +10,14 @@ void CUserData::addSession(WebsocketConnection& connection) void CUserData::removeSession(WebsocketConnection& connection) { - for(unsigned i = 0; i < activeWebSockets.count(); i++) { - if(connection == *(activeWebSockets[i])) { - activeWebSockets[i]->setUserData(nullptr); - activeWebSockets.remove(i); - Serial.println(F("Removed user session")); - return; - } + int i = activeWebSockets.indexOf(&connection); + if(i < 0) { + return; } + + activeWebSockets[i]->setUserData(nullptr); + activeWebSockets.remove(i); + Serial.println(F("Removed user session")); } void CUserData::printMessage(WebsocketConnection& connection, const String& msg) @@ -36,8 +36,8 @@ void CUserData::printMessage(WebsocketConnection& connection, const String& msg) void CUserData::logOut() { - for(unsigned i = 0; i < activeWebSockets.count(); i++) { - activeWebSockets[i]->setUserData(nullptr); + for(auto skt : activeWebSockets) { + skt->setUserData(nullptr); } activeWebSockets.removeAllElements(); diff --git a/samples/Wifi_Sniffer/app/application.cpp b/samples/Wifi_Sniffer/app/application.cpp index 37343ca383..14d6076349 100644 --- a/samples/Wifi_Sniffer/app/application.cpp +++ b/samples/Wifi_Sniffer/app/application.cpp @@ -60,11 +60,11 @@ static void printSummary() { Serial.println("\r\n" "-------------------------------------------------------------------------------------\r\n"); - for(unsigned u = 0; u < knownClients.count(); u++) { - printClient(knownClients[u]); + for(auto& client : knownClients) { + printClient(client); } - for(unsigned u = 0; u < knownAPs.count(); u++) { - printBeacon(knownAPs[u]); + for(auto& ap : knownAPs) { + printBeacon(ap); } Serial.println("\r\n" "-------------------------------------------------------------------------------------\r\n"); diff --git a/tests/HostTests/modules/Network/Http.cpp b/tests/HostTests/modules/Network/Http.cpp index ff4e329620..132e42da08 100644 --- a/tests/HostTests/modules/Network/Http.cpp +++ b/tests/HostTests/modules/Network/Http.cpp @@ -49,8 +49,8 @@ class HttpTest : public TestGroup { #if DEBUG_VERBOSE_LEVEL == DBG Serial << _F(" count: ") << headers.count() << endl; - for(unsigned i = 0; i < headers.count(); ++i) { - String s = headers[i]; + for(auto hdr : headers) { + String s = hdr; m_printHex(" ", s.c_str(), s.length(), 0, 32); } #endif