summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'Swiften/Base/SafeString.h')
-rw-r--r--Swiften/Base/SafeString.h69
1 files changed, 69 insertions, 0 deletions
diff --git a/Swiften/Base/SafeString.h b/Swiften/Base/SafeString.h
new file mode 100644
index 0000000..44b0316
--- /dev/null
+++ b/Swiften/Base/SafeString.h
@@ -0,0 +1,69 @@
+/*
+ * Copyright (c) 2011 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#pragma once
+
+#include <vector>
+#include <string>
+
+#include <Swiften/Base/SafeAllocator.h>
+
+namespace Swift {
+ class SafeString {
+ public:
+ typedef std::vector<char, SafeAllocator<char> > Data;
+ typedef Data::iterator Iterator;
+ typedef Data::const_iterator ConstIterator;
+
+ SafeString() {
+ }
+
+ SafeString(const std::string& s) : data(s.begin(), s.end()) {
+ }
+
+ SafeString(const char*);
+
+
+ std::string toString() const {
+ return data.empty() ? std::string() : std::string(&data[0], data.size());
+ }
+
+ void resize(size_t n) {
+ data.resize(n);
+ }
+
+ char& operator[](size_t n) {
+ return data[n];
+ }
+
+ Iterator begin() {
+ return data.begin();
+ }
+
+ Iterator end() {
+ return data.end();
+ }
+
+ ConstIterator begin() const {
+ return data.begin();
+ }
+
+ ConstIterator end() const {
+ return data.end();
+ }
+
+ size_t size() const {
+ return data.size();
+ }
+
+ bool operator==(const SafeString& o) const {
+ return data == o.data;
+ }
+
+ private:
+ std::vector<char, SafeAllocator<char> > data;
+ };
+};