Gamedev Framework (gf)  0.5.0
A C++11 framework for 2D games
StringRef.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2017 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  */
21 #ifndef GF_STRING_REF_H
22 #define GF_STRING_REF_H
23 
24 #include <cstring>
25 #include <string>
26 
27 namespace gf {
28 #ifndef DOXYGEN_SHOULD_SKIP_THIS
29 inline namespace v1 {
30 #endif
31 
41  class StringRef {
42  public:
48  constexpr StringRef()
49  : m_data(nullptr)
50  , m_size(0)
51  {
52 
53  }
54 
61  constexpr StringRef(const char *str, std::size_t size)
62  : m_data(str)
63  , m_size(size)
64  {
65 
66  }
67 
75  StringRef(const char *str)
76  : m_data(str)
77  , m_size(std::strlen(str))
78  {
79 
80  }
81 
87  StringRef(const std::string& str)
88  : m_data(str.data())
89  , m_size(str.size())
90  {
91 
92  }
93 
99  constexpr const char *getData() const noexcept {
100  return m_data;
101  }
102 
108  constexpr std::size_t getSize() const noexcept {
109  return m_size;
110  }
111 
119  constexpr bool isEmpty() const noexcept {
120  return m_size == 0;
121  }
122 
130  constexpr const char *begin() const noexcept {
131  return m_data;
132  }
133 
141  constexpr const char *end() const noexcept {
142  return m_data + m_size;
143  }
144 
145  private:
146  const char *m_data;
147  std::size_t m_size;
148  };
149 
150 
151 #ifndef DOXYGEN_SHOULD_SKIP_THIS
152 }
153 #endif
154 }
155 
156 #endif // GF_STRING_REF_H
constexpr StringRef()
Default constructor.
Definition: StringRef.h:48
StringRef(const std::string &str)
Constructor from a std::string
Definition: StringRef.h:87
constexpr const char * getData() const noexcept
Get a pointer to the string.
Definition: StringRef.h:99
constexpr bool isEmpty() const noexcept
Check if the string is empty.
Definition: StringRef.h:119
STL namespace.
The namespace for gf classes.
Definition: Action.h:34
constexpr const char * begin() const noexcept
Get an iterator to the beginning of the string.
Definition: StringRef.h:130
constexpr const char * end() const noexcept
Get an iterator past the end of the string.
Definition: StringRef.h:141
A constant reference to a string and its size.
Definition: StringRef.h:41
constexpr std::size_t getSize() const noexcept
Get the size of the string.
Definition: StringRef.h:108
StringRef(const char *str)
Constructor from a null-terminated string.
Definition: StringRef.h:75
constexpr StringRef(const char *str, std::size_t size)
Constructor from a pointer and a size.
Definition: StringRef.h:61