Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c0453a4042e9fc926ffa535d077d6ebddf4e3151 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*******************************************************************************
 * Copyright (c) 2016 protos software gmbh (http://www.protos.de).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * CONTRIBUTORS:
 * 		Thomas Schuetz (initial contribution)
 *
 *******************************************************************************/

#ifndef STATICSTRING_H_
#define STATICSTRING_H_

#include "StaticArray.h"

namespace etRuntime {

/**
 * a static size string class that basically encapsulates a C string
 */
template<int Size>
class StaticString: public StaticArray<char, Size> {
public:
	/**
	 * default constructor is empty string
	 */
	StaticString()
	{
		this->data[0] = 0;
	}

	/**
	 * copy constructor calls operator=(const char *rhs)
	 */
	template<int RhsSize> StaticString(const StaticString<RhsSize> &rhs)
	{
		operator=(rhs.getData());
	}

	/**
	 * constructor calls operator=(const char *rhs)
	 */
	StaticString(const char *rhs)
	{
		operator=(rhs);
	}

	/**
	 * virtual destructor
	 */
	virtual ~StaticString(void) {
	}

	/**
	 * returns the length of the C string stored
	 */
	int length(void) const {
		return std::strlen(this->data);
	}

	/**
	 * returns a const char * pointer to the data of this array
	 */
	const char* c_str(void) const {
		return this->data;
	}


	/**
	 * forwards to operator=(const char *rhs)
	 */
	template<int RhsSize> StaticString<Size>& operator=(const StaticString<RhsSize> &rhs) {
		return operator=(rhs.data);
	}

	/**
	 * works like strncpy() with the size of this string. Will be 0 terminated.
	 */
	StaticString<Size>& operator=(const char *rhs) {
		if (rhs && rhs!=this->data) {
			std::strncpy(this->data, rhs, Size);
			this->data[Size-1] = 0;
		}
		return *this;
	}

	/**
	 * concatenates the rhs to the end of the string if the result fits.
	 * If it doesn't fit nothing is done.
	 */
	template<int RhsSize> const StaticString<Size> operator+(const StaticString<RhsSize> &other) const {
		// make a copy and add
		return StaticString<Size>(*this) += other;
	}

	/**
	 * concatenates the rhs to the end of the string if the result fits.
	 * If it doesn't fit nothing is done.
	 */
	const StaticString<Size> operator+(const char* other) const {
		// make a copy and add
		return StaticString<Size>(*this) += other;
	}

	/*
	 * concatenates the rhs to the end of the string if the result fits.
	 * If it doesn't fit nothing is done.
	 */
	template<int RhsSize> const StaticString<Size> operator+=(const StaticString<RhsSize>& rhs) {
		return operator+=(rhs.data);
	}

	/*
	 * concatenates the rhs to the end of the string if the result fits.
	 * If it doesn't fit nothing is done.
	 */
	const StaticString<Size> operator+=(const char* rhs) {
		if (rhs && (this->length() + std::strlen(rhs)) < Size) {
			std::strcat(this->data, rhs);
		};

		// Note: this function returns a const, not a const&!
		// this prohibits things like (a + b) = c
		return *this;
	}

	/**
	 * compares two strings using std::strcmp()
	 */
	template<int RhsSize> bool operator==(const StaticString<RhsSize> &rhs) {
		return operator==(rhs.data);
	}

	/**
	 * compares two strings using std::strcmp()
	 */
	bool operator==(const char *rhs) {
		return strcmp(this->data, rhs)==0;
	}
};

} /* namespace etRuntime */

#endif /* STATICSTRING_H_ */

Back to the top