Skip to content

Commit

Permalink
Replace custom StringView with standard library std::string_view (#280)
Browse files Browse the repository at this point in the history
* Start replacement work

* Fix the rest of the problems

* Some extra fixes

* Move non-constexpr functions out of header

* Better string split for fixed lengths

* Fix const qualifier
  • Loading branch information
hhvrc authored Sep 6, 2024
1 parent 258188b commit 2868374
Show file tree
Hide file tree
Showing 41 changed files with 485 additions and 786 deletions.
7 changes: 3 additions & 4 deletions include/CaptivePortal.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#pragma once

#include "StringView.h"

#include <freertos/portmacro.h>

#include <cstdint>
#include <string_view>

namespace OpenShock::CaptivePortal {
void SetAlwaysEnabled(bool alwaysEnabled);
Expand All @@ -15,9 +14,9 @@ namespace OpenShock::CaptivePortal {
bool IsRunning();
void Update();

bool SendMessageTXT(uint8_t socketId, StringView data);
bool SendMessageTXT(uint8_t socketId, std::string_view data);
bool SendMessageBIN(uint8_t socketId, const uint8_t* data, std::size_t len);

bool BroadcastMessageTXT(StringView data);
bool BroadcastMessageTXT(std::string_view data);
bool BroadcastMessageBIN(const uint8_t* data, std::size_t len);
} // namespace OpenShock::CaptivePortal
6 changes: 3 additions & 3 deletions include/CaptivePortalInstance.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#pragma once

#include "StringView.h"
#include "WebSocketDeFragger.h"

#include <DNSServer.h>
Expand All @@ -11,16 +10,17 @@
#include <freertos/task.h>

#include <cstdint>
#include <string_view>

namespace OpenShock {
class CaptivePortalInstance {
public:
CaptivePortalInstance();
~CaptivePortalInstance();

bool sendMessageTXT(uint8_t socketId, StringView data) { return m_socketServer.sendTXT(socketId, data.data(), data.length()); }
bool sendMessageTXT(uint8_t socketId, std::string_view data) { return m_socketServer.sendTXT(socketId, data.data(), data.length()); }
bool sendMessageBIN(uint8_t socketId, const uint8_t* data, std::size_t len) { return m_socketServer.sendBIN(socketId, data, len); }
bool broadcastMessageTXT(StringView data) { return m_socketServer.broadcastTXT(data.data(), data.length()); }
bool broadcastMessageTXT(std::string_view data) { return m_socketServer.broadcastTXT(data.data(), data.length()); }
bool broadcastMessageBIN(const uint8_t* data, std::size_t len) { return m_socketServer.broadcastBIN(data, len); }

private:
Expand Down
7 changes: 2 additions & 5 deletions include/Common.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#pragma once

#include "StringView.h"

#include <cstdint>
#include <string_view>

#define DISABLE_COPY(TypeName) \
TypeName(const TypeName&) = delete; \
Expand All @@ -11,7 +10,6 @@
TypeName(TypeName&&) = delete; \
void operator=(TypeName&&) = delete


#ifndef OPENSHOCK_API_DOMAIN
#error "OPENSHOCK_API_DOMAIN must be defined"
#endif
Expand Down Expand Up @@ -60,6 +58,5 @@
#endif

namespace OpenShock::Constants {
const char* const FW_USERAGENT = OPENSHOCK_FW_USERAGENT;
const StringView FW_USERAGENT_sv = OPENSHOCK_FW_USERAGENT ""_sv;
const char* const FW_USERAGENT = OPENSHOCK_FW_USERAGENT;
} // namespace OpenShock::Constants
5 changes: 2 additions & 3 deletions include/GatewayClient.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#pragma once

#include "StringView.h"

#include <WebSocketsClient.h>

#include <cstdint>
#include <string>
#include <string_view>
#include <unordered_map>

namespace OpenShock {
Expand All @@ -26,7 +25,7 @@ namespace OpenShock {
void connect(const char* lcgFqdn);
void disconnect();

bool sendMessageTXT(StringView data);
bool sendMessageTXT(std::string_view data);
bool sendMessageBIN(const uint8_t* data, std::size_t length);

bool loop();
Expand Down
6 changes: 3 additions & 3 deletions include/GatewayConnectionManager.h
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
#pragma once

#include "AccountLinkResultCode.h"
#include "StringView.h"

#include <cstdint>
#include <functional>
#include <string_view>

namespace OpenShock::GatewayConnectionManager {
[[nodiscard]] bool Init();

bool IsConnected();

bool IsLinked();
AccountLinkResultCode Link(StringView linkCode);
AccountLinkResultCode Link(std::string_view linkCode);
void UnLink();

bool SendMessageTXT(StringView data);
bool SendMessageTXT(std::string_view data);
bool SendMessageBIN(const uint8_t* data, std::size_t length);

void Update();
Expand Down
1 change: 0 additions & 1 deletion include/OtaUpdateManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "FirmwareBootType.h"
#include "OtaUpdateChannel.h"
#include "SemVer.h"
#include "StringView.h"

#include <array>
#include <string>
Expand Down
12 changes: 6 additions & 6 deletions include/SemVer.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#pragma once

#include "StringView.h"

#include <cstdint>
#include <string>
#include <string_view>

namespace OpenShock {
struct SemVer {
Expand All @@ -16,8 +16,8 @@ namespace OpenShock {
SemVer(uint16_t major, uint16_t minor, uint16_t patch)
: major(major), minor(minor), patch(patch), prerelease(), build()
{}
SemVer(uint16_t major, uint16_t minor, uint16_t patch, StringView prerelease, StringView build)
: major(major), minor(minor), patch(patch), prerelease(prerelease.toString()), build(build.toString())
SemVer(uint16_t major, uint16_t minor, uint16_t patch, std::string_view prerelease, std::string_view build)
: major(major), minor(minor), patch(patch), prerelease(std::string(prerelease)), build(std::string(build))
{}

bool operator==(const SemVer& other) const {
Expand Down Expand Up @@ -72,5 +72,5 @@ namespace OpenShock {
std::string toString() const;
};

bool TryParseSemVer(StringView str, SemVer& out);
} // namespace OpenShock
bool TryParseSemVer(std::string_view str, SemVer& out);
} // namespace OpenShock
Loading

0 comments on commit 2868374

Please sign in to comment.