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 //========================================================================================================
33 template<typename T>
34 class OPENMS_DLLAPI shared_xerces_ptr
35 {
36 // Function to release Xerces data type with a release member function
37 template<typename U>
38 static void doRelease_(U* item)
39 {
40 // Only release this if it has no owner
41 if (nullptr == item->getOwnerDocument())
42 item->release();
43 }
44
45 static void doRelease_(char* item);
46 static void doRelease_(char16_t* item);
47
48 // The actual data we're holding
49 std::shared_ptr<T> item_;
50 public:
51 // Default constructor
52 shared_xerces_ptr() = default;
53 // Assignment constructor
55 : item_(item, doRelease_ )
56 {}
57 // Assignment of data to guard
59 {
60 assign(item);
61 return *this;
62 }
63 // Give up hold on data
64 void reset()
65 {
66 item_.reset();
67 }
68 // Release currently held data, if any, to hold another
69 void assign(T* item)
70 {
71 item_.reset(item, doRelease_ );
72 }
73 // Get pointer to the currently held data, if any
74 T* get()
75 {
76 return item_.get();
77 }
78 const T* get() const
79 {
80 return item_.get();
81 }
82 // Return true if no data is held
83 bool is_released() const
84 {
85 return (nullptr == item_.get());
86 }
87 };
88
89 template <typename T>
90 class OPENMS_DLLAPI unique_xerces_ptr
91 {
92 private:
93
94 template<typename U>
95 static void doRelease_(U*& item)
96 {
97 // Only release this if it has no parent (otherwise
98 // parent will release it)
99 if (nullptr == item->getOwnerDocument())
100 item->release();
101 }
102
103 static void doRelease_(char*& item);
104 static void doRelease_(char16_t*& item);
105
107
108 public:
109
110 // Hide copy constructor and assignment operator
113
115 : item_(nullptr)
116 {}
117
118 explicit unique_xerces_ptr(T* i)
119 : item_(i)
120 {}
121
123 {
124 xerces_release();
125 }
126
128 : item_(nullptr)
129 {
130 this->swap(other);
131 }
132
133 void swap(unique_xerces_ptr<T>& other) noexcept
134 {
135 std::swap(item_, other.item_);
136 }
137
138 // Assignment of data to guard (not chainable)
139 void operator=(T* i)
140 {
141 reassign(i);
142 }
143
144 // Release held data (i.e. delete/free it)
146 {
147 if (!is_released())
148 {
149 // Use type-specific release mechanism
150 doRelease_(item_);
151 item_ = nullptr;
152 }
153 }
154
155 // Give up held data (i.e. return data without releasing)
156 T* yield()
157 {
158 T* tempItem = item_;
159 item_ = nullptr;
160 return tempItem;
161 }
162
163 // Release currently held data, if any, to hold another
164 void assign(T* i)
165 {
166 xerces_release();
167 item_ = i;
168 }
169
170 // Get pointer to the currently held data, if any
171 T* get() const
172 {
173 return item_;
174 }
175
176 // Return true if no data is held
177 bool is_released() const
178 {
179 return (nullptr == item_);
180 }
181 };
182
183 //========================================================================================================
184
196 class OPENMS_DLLAPI StringManager
197 {
198 typedef std::basic_string<char16_t> XercesString;
199
202
204 static unique_xerces_ptr<char16_t> fromNative_(const std::string& str);
205
207 static std::string toNative_(const char16_t* str);
208
210 static std::string toNative_(const unique_xerces_ptr<char16_t>& str);
211
212protected:
214 static void compress64_ (const char16_t * input_it, char* output_it);
215
216public:
219
222
224 // https://github.com/OpenMS/OpenMS/issues/8122
225 static Size strLength(const char16_t* input_ptr);
226
228 static XercesString convert(const char * str);
229
231 static XercesString convert(const std::string & str);
232
234 static unique_xerces_ptr<char16_t> convertPtr(const char * str);
235
237 static unique_xerces_ptr<char16_t> convertPtr(const std::string & str);
238
240 static std::string convert(const char16_t * str);
241
243 static Int parseInt(const char16_t * str);
244
246 static bool isASCII(const char16_t * chars, const Size length);
247
254 static void appendASCII(const char16_t * str, const Size length, std::string & result);
255 };
256
257} // namespace OpenMS::Internal
Helper class for XML parsing that handles the conversions of Xerces strings.
Definition StringManager.h:197
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:198
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.
Shared-ownership handle for a Xerces object or string, which releases it the way Xerces requires.
Definition StringManager.h:35
T * get()
Definition StringManager.h:74
std::shared_ptr< T > item_
Definition StringManager.h:49
static void doRelease_(char *item)
shared_xerces_ptr(T *item)
Definition StringManager.h:54
bool is_released() const
Definition StringManager.h:83
static void doRelease_(char16_t *item)
static void doRelease_(U *item)
Definition StringManager.h:38
void assign(T *item)
Definition StringManager.h:69
void reset()
Definition StringManager.h:64
shared_xerces_ptr & operator=(T *item)
Definition StringManager.h:58
const T * get() const
Definition StringManager.h:78
Definition StringManager.h:91
T * item_
Definition StringManager.h:106
void operator=(T *i)
Definition StringManager.h:139
static void doRelease_(char16_t *&item)
void xerces_release()
Definition StringManager.h:145
unique_xerces_ptr(T *i)
Definition StringManager.h:118
unique_xerces_ptr & operator=(const unique_xerces_ptr< T > &)=delete
void swap(unique_xerces_ptr< T > &other) noexcept
Definition StringManager.h:133
void assign(T *i)
Definition StringManager.h:164
bool is_released() const
Definition StringManager.h:177
unique_xerces_ptr()
Definition StringManager.h:114
unique_xerces_ptr(unique_xerces_ptr< T > &&other) noexcept
Definition StringManager.h:127
unique_xerces_ptr(const unique_xerces_ptr< T > &)=delete
T * yield()
Definition StringManager.h:156
static void doRelease_(char *&item)
~unique_xerces_ptr()
Definition StringManager.h:122
T * get() const
Definition StringManager.h:171
static void doRelease_(U *&item)
Definition StringManager.h:95
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