OpenMS
Loading...
Searching...
No Matches
StringManager.h
Go to the documentation of this file.
1// Copyright (c) 2002-present, OpenMS Inc. -- EKU Tuebingen, ETH Zurich, and FU Berlin
2// SPDX-License-Identifier: BSD-3-Clause
3//
4// --------------------------------------------------------------------------
5// $Maintainer: Chris Bielow $
6// $Authors: Marc Sturm, Chris Bielow $
7// --------------------------------------------------------------------------
8
9#pragma once
10
12
13#include <memory>
14#include <string>
15
16namespace OpenMS::Internal
17{
20 #define CONST_XMLCH(s) (u ## s)
21
22 //Adapted from https://www.codeproject.com/articles/99551/redux-raii-adapter-for-xerces
23 //Copyright 2010 Orjan Westin
24 //Under BSD license
25 //========================================================================================================
26 template<typename T>
27 class OPENMS_DLLAPI shared_xerces_ptr
28 {
29 // Function to release Xerces data type with a release member function
30 template<typename U>
31 static void doRelease_(U* item)
32 {
33 // Only release this if it has no owner
34 if (nullptr == item->getOwnerDocument())
35 item->release();
36 }
37
38 static void doRelease_(char* item);
39 static void doRelease_(char16_t* item);
40
41 // The actual data we're holding
42 std::shared_ptr<T> item_;
43 public:
44 // Default constructor
45 shared_xerces_ptr() = default;
46 // Assignment constructor
48 : item_(item, doRelease_ )
49 {}
50 // Assignment of data to guard
52 {
53 assign(item);
54 return *this;
55 }
56 // Give up hold on data
57 void reset()
58 {
59 item_.reset();
60 }
61 // Release currently held data, if any, to hold another
62 void assign(T* item)
63 {
64 item_.reset(item, doRelease_ );
65 }
66 // Get pointer to the currently held data, if any
67 T* get()
68 {
69 return item_.get();
70 }
71 const T* get() const
72 {
73 return item_.get();
74 }
75 // Return true if no data is held
76 bool is_released() const
77 {
78 return (nullptr == item_.get());
79 }
80 };
81
82 template <typename T>
83 class OPENMS_DLLAPI unique_xerces_ptr
84 {
85 private:
86
87 template<typename U>
88 static void doRelease_(U*& item)
89 {
90 // Only release this if it has no parent (otherwise
91 // parent will release it)
92 if (nullptr == item->getOwnerDocument())
93 item->release();
94 }
95
96 static void doRelease_(char*& item);
97 static void doRelease_(char16_t*& item);
98
100
101 public:
102
103 // Hide copy constructor and assignment operator
106
108 : item_(nullptr)
109 {}
110
111 explicit unique_xerces_ptr(T* i)
112 : item_(i)
113 {}
114
116 {
117 xerces_release();
118 }
119
121 : item_(nullptr)
122 {
123 this->swap(other);
124 }
125
126 void swap(unique_xerces_ptr<T>& other) noexcept
127 {
128 std::swap(item_, other.item_);
129 }
130
131 // Assignment of data to guard (not chainable)
132 void operator=(T* i)
133 {
134 reassign(i);
135 }
136
137 // Release held data (i.e. delete/free it)
139 {
140 if (!is_released())
141 {
142 // Use type-specific release mechanism
143 doRelease_(item_);
144 item_ = nullptr;
145 }
146 }
147
148 // Give up held data (i.e. return data without releasing)
149 T* yield()
150 {
151 T* tempItem = item_;
152 item_ = nullptr;
153 return tempItem;
154 }
155
156 // Release currently held data, if any, to hold another
157 void assign(T* i)
158 {
159 xerces_release();
160 item_ = i;
161 }
162
163 // Get pointer to the currently held data, if any
164 T* get() const
165 {
166 return item_;
167 }
168
169 // Return true if no data is held
170 bool is_released() const
171 {
172 return (nullptr == item_);
173 }
174 };
175
176 //========================================================================================================
177
189 class OPENMS_DLLAPI StringManager
190 {
191 typedef std::basic_string<char16_t> XercesString;
192
195
197 static unique_xerces_ptr<char16_t> fromNative_(const std::string& str);
198
200 static std::string toNative_(const char16_t* str);
201
203 static std::string toNative_(const unique_xerces_ptr<char16_t>& str);
204
205protected:
207 static void compress64_ (const char16_t * input_it, char* output_it);
208
209public:
212
215
217 // https://github.com/OpenMS/OpenMS/issues/8122
218 static Size strLength(const char16_t* input_ptr);
219
221 static XercesString convert(const char * str);
222
224 static XercesString convert(const std::string & str);
225
227 static unique_xerces_ptr<char16_t> convertPtr(const char * str);
228
230 static unique_xerces_ptr<char16_t> convertPtr(const std::string & str);
231
233 static std::string convert(const char16_t * str);
234
236 static Int parseInt(const char16_t * str);
237
239 static bool isASCII(const char16_t * chars, const Size length);
240
247 static void appendASCII(const char16_t * str, const Size length, std::string & result);
248 };
249
250} // namespace OpenMS::Internal
Helper class for XML parsing that handles the conversions of Xerces strings.
Definition StringManager.h:190
static std::string toNative_(const char16_t *str)
Converts from a wide-character string to a narrow-character string.
static void appendASCII(const char16_t *str, const Size length, std::string &result)
Transcodes the supplied char16_t* and appends it to the OpenMS String.
static unique_xerces_ptr< char16_t > convertPtr(const std::string &str)
Transcode the supplied C++ string to a UTF-16 string pointer.
std::basic_string< char16_t > XercesString
Definition StringManager.h:191
static bool isASCII(const char16_t *chars, const Size length)
Checks if supplied chars in char16_t* can be encoded with ASCII (i.e. the upper byte of each char is ...
static void compress64_(const char16_t *input_it, char *output_it)
Compresses eight 8x16bit Chars in char16_t* to 8x8bit Chars by cutting upper byte.
static unique_xerces_ptr< char16_t > convertPtr(const char *str)
Transcode the supplied C string to a UTF-16 string pointer.
static std::string convert(const char16_t *str)
Transcode the supplied char16_t* to a std::string.
static Size strLength(const char16_t *input_ptr)
Calculates the length of a char16_t* string using SIMDe.
static XercesString convert(const std::string &str)
Transcode the supplied C++ string to a UTF-16 string.
static unique_xerces_ptr< char16_t > fromNative_(const char *str)
Converts from a narrow-character string to a wide-character string.
static XercesString convert(const char *str)
Transcode the supplied C string to a UTF-16 string.
static Int parseInt(const char16_t *str)
Parse an integer from a UTF-16 numeric string (as Xerces' XMLString::parseInt would).
static unique_xerces_ptr< char16_t > fromNative_(const std::string &str)
Converts from a narrow-character string to a wide-character string.
static std::string toNative_(const unique_xerces_ptr< char16_t > &str)
Converts from a wide-character string to a narrow-character string.
Definition StringManager.h:28
T * get()
Definition StringManager.h:67
std::shared_ptr< T > item_
Definition StringManager.h:42
static void doRelease_(char *item)
shared_xerces_ptr(T *item)
Definition StringManager.h:47
bool is_released() const
Definition StringManager.h:76
static void doRelease_(char16_t *item)
static void doRelease_(U *item)
Definition StringManager.h:31
void assign(T *item)
Definition StringManager.h:62
void reset()
Definition StringManager.h:57
shared_xerces_ptr & operator=(T *item)
Definition StringManager.h:51
const T * get() const
Definition StringManager.h:71
Definition StringManager.h:84
T * item_
Definition StringManager.h:99
void operator=(T *i)
Definition StringManager.h:132
static void doRelease_(char16_t *&item)
void xerces_release()
Definition StringManager.h:138
unique_xerces_ptr(T *i)
Definition StringManager.h:111
unique_xerces_ptr & operator=(const unique_xerces_ptr< T > &)=delete
void swap(unique_xerces_ptr< T > &other) noexcept
Definition StringManager.h:126
void assign(T *i)
Definition StringManager.h:157
bool is_released() const
Definition StringManager.h:170
unique_xerces_ptr()
Definition StringManager.h:107
unique_xerces_ptr(unique_xerces_ptr< T > &&other) noexcept
Definition StringManager.h:120
unique_xerces_ptr(const unique_xerces_ptr< T > &)=delete
T * yield()
Definition StringManager.h:149
static void doRelease_(char *&item)
~unique_xerces_ptr()
Definition StringManager.h:115
T * get() const
Definition StringManager.h:164
static void doRelease_(U *&item)
Definition StringManager.h:88
int Int
Signed integer type.
Definition Types.h:72
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition Types.h:97
Namespace used to hide implementation details from users.
Definition BayesianProteinInferenceAlgorithm.h:26