DPC++ Runtime
Runtime libraries for oneAPI DPC++
string.hpp
Go to the documentation of this file.
1 //==----------------- string.hpp - SYCL standard header file ---------------==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include <cstring>
10 #include <string>
11 
12 #pragma once
13 
14 namespace sycl {
15 inline namespace _V1 {
16 namespace detail {
17 
18 // This class and detail::string_view class are intended to support
19 // different ABIs between libsycl and the user program.
20 // This class is not inteded to replace std::string for general purpose usage.
21 class string {
22  char *str = nullptr;
23 
24 public:
25  string() noexcept = default;
26  ~string() { delete[] str; }
27 
28  string(std::string_view strn) {
29  size_t len = strn.length();
30  str = new char[len + 1];
31  strn.copy(str, len);
32  str[len] = 0;
33  }
34 
35  friend void swap(string &lhs, string &rhs) noexcept {
36  std::swap(lhs.str, rhs.str);
37  }
38 
39  string(string &&other) noexcept { swap(*this, other); }
40  string(const string &other) {
41  if (other.str == nullptr)
42  return;
43  *this = string{other.str};
44  }
45 
46  string &operator=(string &&other) noexcept {
47  swap(*this, other);
48  return *this;
49  }
50  string &operator=(const string &other) {
51  *this = string{other};
52  return *this;
53  }
54 
55  string &operator=(std::string_view strn) {
56  *this = string{strn};
57  return *this;
58  }
59 
60  const char *c_str() const noexcept { return str ? str : ""; }
61 
62  friend bool operator==(const string &lhs, std::string_view rhs) noexcept {
63  return rhs == lhs.c_str();
64  }
65  friend bool operator==(std::string_view lhs, const string &rhs) noexcept {
66  return lhs == rhs.c_str();
67  }
68 };
69 
70 } // namespace detail
71 } // namespace _V1
72 } // namespace sycl
friend bool operator==(std::string_view lhs, const string &rhs) noexcept
Definition: string.hpp:65
string() noexcept=default
string & operator=(string &&other) noexcept
Definition: string.hpp:46
string(const string &other)
Definition: string.hpp:40
string & operator=(const string &other)
Definition: string.hpp:50
const char * c_str() const noexcept
Definition: string.hpp:60
string(string &&other) noexcept
Definition: string.hpp:39
string & operator=(std::string_view strn)
Definition: string.hpp:55
friend bool operator==(const string &lhs, std::string_view rhs) noexcept
Definition: string.hpp:62
string(std::string_view strn)
Definition: string.hpp:28
friend void swap(string &lhs, string &rhs) noexcept
Definition: string.hpp:35
Definition: access.hpp:18
_Abi const simd< _Tp, _Abi > & noexcept
Definition: simd.hpp:1324