ocpp 0.24.1
A C++ implementation of the Open Charge Point Protocol
string.hpp
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2020 - 2023 Pionix GmbH and Contributors to EVerest
3#ifndef OCPP_COMMON_STRING_HPP
4#define OCPP_COMMON_STRING_HPP
5
6#include <cstddef>
7#include <iostream>
8#include <sstream>
9#include <stdexcept>
10#include <string>
11
12namespace ocpp {
13
14class StringConversionException : public std::runtime_error {
15 using std::runtime_error::runtime_error;
16};
17
18enum class StringTooLarge {
19 Throw,
20 Truncate
21};
22
24template <size_t L> class String {
25private:
26 std::string data;
27 static constexpr size_t length = L;
28
29public:
31 explicit String(const std::string& data, StringTooLarge to_large = StringTooLarge::Throw) {
32 this->set(data, to_large);
33 }
34
35 explicit String(const char* data, StringTooLarge to_large = StringTooLarge::Throw) {
36 this->set(data, to_large);
37 }
38
40 String() = default;
41
44 std::string get() const {
45 return data;
46 }
47
49 void set(const std::string& data, StringTooLarge to_large = StringTooLarge::Throw) {
50 std::string_view view = data;
51 if (view.length() > this->length) {
52 if (to_large == StringTooLarge::Throw) {
53 throw StringConversionException("String length (" + std::to_string(view.length()) +
54 ") exceeds permitted length (" + std::to_string(this->length) + ")");
55 }
56 // Truncate
57 view = view.substr(0, length);
58 }
59
60 if (this->is_valid(view)) {
61 this->data = view;
62 } else {
63 throw StringConversionException("String has invalid format");
64 }
65 }
66
68 bool is_valid(std::string_view data) {
69 (void)data; // not needed here
70 return true;
71 }
72
74 operator std::string() {
75 return this->get();
76 }
77};
78
80template <size_t L> bool operator==(const String<L>& lhs, const char* rhs) {
81 return lhs.get() == rhs;
82}
83
85template <size_t L> bool operator==(const String<L>& lhs, const String<L>& rhs) {
86 return lhs.get() == rhs.get();
87}
88
90template <size_t L> bool operator!=(const String<L>& lhs, const char* rhs) {
91 return !(lhs.get() == rhs);
92}
93
95template <size_t L> bool operator!=(const String<L>& lhs, const String<L>& rhs) {
96 return !(lhs.get() == rhs.get());
97}
98
101template <size_t L> std::ostream& operator<<(std::ostream& os, const String<L>& str) {
102 os << str.get();
103 return os;
104}
105
106} // namespace ocpp
107
108#endif
Definition: string.hpp:14
Contains a String impementation with a maximum length.
Definition: string.hpp:24
void set(const std::string &data, StringTooLarge to_large=StringTooLarge::Throw)
Sets the content of the string to the given data.
Definition: string.hpp:49
String()=default
Creates a string.
std::string get() const
Provides a std::string representation of the string.
Definition: string.hpp:44
String(const std::string &data, StringTooLarge to_large=StringTooLarge::Throw)
Creates a string from the given data.
Definition: string.hpp:31
bool is_valid(std::string_view data)
Override this to check for a specific format.
Definition: string.hpp:68