Geant4 11.4.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
MCGIDI_string.hpp
Go to the documentation of this file.
1/*
2 # <<BEGIN-copyright>>
3# Copyright 2019, Lawrence Livermore National Security, LLC.
4# This file is part of the gidiplus package (https://github.com/LLNL/gidiplus).
5# gidiplus is licensed under the MIT license (see https://opensource.org/licenses/MIT).
6# SPDX-License-Identifier: MIT
7 # <<END-copyright>>
8 */
9
10#ifndef MCGIDI_STRING_HPP
11#define MCGIDI_STRING_HPP
12
13/* Modified from Karsten Burger's version 2017
14 * Made changes to make it more compatible with GPUs.
15 * Allow for data to be initialized to nullptr.
16 *
17 * Modified from public domain software:
18 * Karsten Burger 2014
19 *
20 * Sourceforge project "Simple C++ String Class"
21 * http://sourceforge.net/projects/simplecstringclass/
22
23 * This a simple C++ string class based on class my_string by
24 * Christian Stigen Larsen, 2007, http://csl.name/programming/my_string/
25 *
26 * It only uses the C-string functions and is thus independent of the
27 * standard C++ library.
28 *
29 * It is public domain, in the hope, that you find it useful.
30 * Please note that there is no guarantee of any kind: it is supplied
31 * without any warranty; without even the implied warranty of
32 * merchantability or fitness for a particular purpose.
33 *
34 * You can probably replace std::string with this one in many
35 * cases, but a lot of stuff is missing, and I would recommend
36 * you stick to std::string anyway.
37 *
38 * I want to point out that there is nothing fancy about this class.
39 * It keeps every string in its own buffer, and copies as often as
40 * needed.
41 * The data always contains a trailing NUL char.
42 *
43 * However, I believe that is a good approach. For instance, it
44 * uses malloc rather than new, which makes it possible to use
45 * realloc. On many systems, realloc will try to use up "invisible"
46 * space that was used by malloc to pad a string for memory alignment.
47 * That makes it potentially fast for small concatenations.
48 *
49 * I don't propose to use this class for anything practical, since
50 * we already have std::string, but it may be an interesting read
51 * for C++ novices at the very least. Also, additional functions can
52 * easily be expanded.
53 *
54 * Also I met a case, where I had to avoid std::string because of
55 * link problems with an application using mixed libraries, especially
56 * one compiled with an old Intel compiler icc 7.
57 *
58 * Bugs/suggestions to info [at) dr-burger ]dot[ com
59 * or via the Sourceforge project page.
60 */
61
62#include <sys/types.h> // size_t
63#include <stdexcept>
64#include <LUPI_declareMacro.hpp>
65
66 /** @brief Simple C++ string class, useful as replacement for
67 std::string if this cannot be used, or just for fun.
68
69 */
70namespace MCGIDI {
71
72class String
73{
74
75 char* p; ///< The data
76 size_t allocated_; ///< The allocated memory size (including trailing NUL)
77 size_t size_; ///< The currently used memory size (excluding trailing NUL)
78
79 public:
80 typedef size_t size_type;
81 static const size_type npos;
82
86 LUPI_HOST_DEVICE String(const char*);
87
88 LUPI_HOST_DEVICE String& operator=(const char*);
90
94 LUPI_HOST_DEVICE void push_back(char);
95
96 friend String
97 LUPI_HOST_DEVICE operator+(const String& lhs, const String& rhs);
98
99 LUPI_HOST_DEVICE bool operator==(const char*) const;
100 LUPI_HOST_DEVICE bool operator==(const String&) const;
101
102 LUPI_HOST_DEVICE void clear(); // set string to empty string (memory remains reserved)
103 LUPI_HOST_DEVICE void clearMemory(); // set string to empty string (memory is free'd)
104
105 LUPI_HOST_DEVICE size_type size() const { return size_; } ///< size without terminating NUL
106 LUPI_HOST_DEVICE size_type length() const { return size_; } ///< as size()
107
108 // size if fully used
109 LUPI_HOST_DEVICE size_type capacity() const { return allocated_-1; }
110
111 // 8 byte alligned size
113 size_t delta = allocated_;
114 size_t sub = delta % 8;
115 if (sub != 0) delta += (8-sub);
116 return delta * sizeof(char);
117 }
118
119 LUPI_HOST_DEVICE bool empty() const { return size_ == 0; }
120
121 LUPI_HOST_DEVICE const char* c_str() const { return p; } ///< raw data
122
123 /** Reserve internal string memory so that n characters can be put into the
124 string (plus 1 for the NUL char). If there is already enough memory,
125 nothing happens, if not, the memory will be realloated to exactly this
126 amount.
127 */
128 LUPI_HOST_DEVICE void reserve( size_type n, char ** address = nullptr);
129
130 /** Resize string. If n is less than the current size, the string will be truncated.
131 If n is larger, then the memory will be reallocated to exactly this amount, and
132 the additional characters will be NUL characters.
133 */
134 LUPI_HOST_DEVICE void resize( size_type n, char ** address = nullptr);
135
136 /** Resize string. If n is less than the current size, the string will be truncated.
137 If n is larger, then the memory will be reallocated to exactly this amount, and
138 the additional characters will be c characters.
139 */
140 LUPI_HOST_DEVICE void resize( size_type n, char c, char ** address = nullptr);
141
142 /// swap contents
144
146
147 // unchecked access:
148 LUPI_HOST_DEVICE char& operator[](const size_type i) { return p[i]; }
149 LUPI_HOST_DEVICE char operator[](const size_type i) const { return p[i]; }
150 // checked access:
151 LUPI_HOST_DEVICE char& at(const size_type i);
152 LUPI_HOST_DEVICE char at(const size_type i) const;
153
154 /// erase len characters at position pos
156 /// Append n characters of a string
157 LUPI_HOST_DEVICE String& append(const char* str, size_type n);
158
159 LUPI_HOST_DEVICE int compare( size_type pos, size_type len, const String& str ) const;
160 LUPI_HOST_DEVICE int compare( size_type pos, size_type len, const char* str ) const;
161
162 private:
163 // reallocate the internal memory
164 LUPI_HOST_DEVICE void my_realloc( size_type n, char ** address = nullptr);
165 LUPI_HOST_DEVICE char* strdup_never_null(const char* other);
166
167};
168// class
169
170LUPI_HOST_DEVICE bool operator<(const String&, const String&);
171
172}
173
174#endif
#define LUPI_HOST_DEVICE
LUPI_HOST_DEVICE size_t internalSize() const
static const size_type npos
LUPI_HOST_DEVICE int compare(size_type pos, size_type len, const String &str) const
LUPI_HOST_DEVICE char & operator[](const size_type i)
LUPI_HOST_DEVICE bool operator==(const char *) const
LUPI_HOST_DEVICE String & append(const char *str, size_type n)
Append n characters of a string.
LUPI_HOST_DEVICE String & erase(size_type pos, size_type len)
erase len characters at position pos
LUPI_HOST_DEVICE String & operator+=(const String &)
LUPI_HOST_DEVICE void clear()
LUPI_HOST_DEVICE char operator[](const size_type i) const
LUPI_HOST_DEVICE char & at(const size_type i)
LUPI_HOST_DEVICE void swap(String &)
swap contents
friend String LUPI_HOST_DEVICE operator+(const String &lhs, const String &rhs)
LUPI_HOST_DEVICE String & operator=(const char *)
LUPI_HOST_DEVICE size_type capacity() const
LUPI_HOST_DEVICE String substr(const size_type pos, size_type length) const
LUPI_HOST_DEVICE void resize(size_type n, char **address=nullptr)
LUPI_HOST_DEVICE bool empty() const
LUPI_HOST_DEVICE size_type size() const
size without terminating NUL
LUPI_HOST_DEVICE const char * c_str() const
raw data
LUPI_HOST_DEVICE String()
LUPI_HOST_DEVICE size_type length() const
as size()
LUPI_HOST_DEVICE void reserve(size_type n, char **address=nullptr)
LUPI_HOST_DEVICE void clearMemory()
LUPI_HOST_DEVICE ~String()
LUPI_HOST_DEVICE void push_back(char)
Simple C++ string class, useful as replacement for std::string if this cannot be used,...
Definition MCGIDI.hpp:43
LUPI_HOST_DEVICE bool operator<(const String &, const String &)