A platform for high-performance distributed tool and library development written in C++. It can be deployed in two different cluster modes: standalone or distributed. API for v0.5.0, released on June 13, 2018.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
PDBString.cc
Go to the documentation of this file.
1 /*****************************************************************************
2  * *
3  * Copyright 2018 Rice University *
4  * *
5  * Licensed under the Apache License, Version 2.0 (the "License"); *
6  * you may not use this file except in compliance with the License. *
7  * You may obtain a copy of the License at *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
14  * See the License for the specific language governing permissions and *
15  * limitations under the License. *
16  * *
17  *****************************************************************************/
18 
19 #ifndef PDBSTRING_CC
20 #define PDBSTRING_CC
21 
22 #include "PDBString.h"
23 #include <string.h>
24 #include <iostream>
25 #include <string>
26 #include "PDBMap.h"
27 #include "BuiltInObjectTypeIDs.h"
28 
29 #define CAN_FIT_IN_DATA(len) (len <= sizeof(decltype(data().getOffset())))
30 
31 namespace pdb {
32 
33 inline Handle<char>& String::data() const {
34  return *((Handle<char>*)this->storage);
35 }
36 
37 inline String::String() {
38 
39  // view the Handle as actually storing data
40  data().setExactTypeInfoValue(1);
41  data().setOffset(-1);
42  c_str()[0] = 0;
43 }
44 
45 inline size_t String::hash() const {
46 
47  return hashMe(c_str(), size() - 1);
48 }
49 
50 inline String& String::operator=(const char* toMe) {
51  int len = strlen(toMe) + 1;
52  if (CAN_FIT_IN_DATA(len)) {
53  if (data().getExactTypeInfoValue() < 0) {
54  data() = nullptr;
55  }
56  data().setExactTypeInfoValue(len);
57  } else {
58  if (data().getExactTypeInfoValue() >= 0) {
59  data().setOffset(-1);
60  }
61  data() = makeObjectWithExtraStorage<char>((len - 1) * sizeof(char));
62  data().setExactTypeInfoValue(-len);
63  }
64 
65  memmove(c_str(), toMe, len);
66  return *this;
67 }
68 
69 inline String::String(const String& s) {
70 
71  // if the other guy is short
72  if (s.data().getExactTypeInfoValue() >= 0) {
73  data().setOffset(s.data().getOffset());
74 
75  // the other guy is big
76  } else {
77  data().setOffset(-1);
78  data() = s.data();
79  }
80 
81  data().setExactTypeInfoValue(s.data().getExactTypeInfoValue());
82 }
83 
84 inline String::~String() {
85  if (data().getExactTypeInfoValue() < -1) {
86  (&data())->~Handle();
87  }
88 }
89 
90 inline String& String::operator=(const String& s) {
91 
92  if (this == &s) {
93  std::cout << "Assigning to myself!!!!\n";
94  exit(1);
95  }
96 
97  // if the other guy is short
98  if (s.data().getExactTypeInfoValue() >= 0) {
99  if (data().getExactTypeInfoValue() < 0) {
100  data() = nullptr;
101  }
102  data().setOffset(s.data().getOffset());
103 
104  // the other guy is big
105  } else {
106  if (data().getExactTypeInfoValue() >= 0) {
107  data().setOffset(-1);
108  }
109  data() = s.data();
110  }
111  data().setExactTypeInfoValue(s.data().getExactTypeInfoValue());
112  return *this;
113 }
114 
115 inline String& String::operator=(const std::string& s) {
116  int len = s.size() + 1;
117  if (CAN_FIT_IN_DATA(len)) {
118  if (data().getExactTypeInfoValue() < 0) {
119  data() = nullptr;
120  }
121  data().setExactTypeInfoValue(len);
122  } else {
123  if (data().getExactTypeInfoValue() >= 0) {
124  data().setOffset(-1);
125  }
126  data() = makeObjectWithExtraStorage<char>((len - 1) * sizeof(char));
127  data().setExactTypeInfoValue(-len);
128  }
129 
130  memmove(c_str(), s.c_str(), len);
131  return *this;
132 }
133 
134 inline String::String(const char* toMe) {
135  int len = strlen(toMe) + 1;
136  if (CAN_FIT_IN_DATA(len)) {
137  data().setExactTypeInfoValue(len);
138  } else {
139  data().setOffset(-1);
140  data() = makeObjectWithExtraStorage<char>((len - 1) * sizeof(char));
141  data().setExactTypeInfoValue(-len);
142  }
143 
144  memmove(c_str(), toMe, len);
145 }
146 
147 inline String::String(const char* toMe, size_t n) {
148  int len = n + 1;
149 
150  if (CAN_FIT_IN_DATA(len)) {
151  data().setExactTypeInfoValue(len);
152  } else {
153  data().setOffset(-1);
154  data() = makeObjectWithExtraStorage<char>((len - 1) * sizeof(char));
155  data().setExactTypeInfoValue(-len);
156  }
157 
158  memmove(c_str(), toMe, len - 1);
159  c_str()[len - 1] = 0;
160 }
161 
162 inline String::String(const std::string& s) {
163  int len = s.size() + 1;
164 
165  if (CAN_FIT_IN_DATA(len)) {
166  data().setExactTypeInfoValue(len);
167  } else {
168  data().setOffset(-1);
169  data() = makeObjectWithExtraStorage<char>((len - 1) * sizeof(char));
170  data().setExactTypeInfoValue(-len);
171  }
172 
173  memmove(c_str(), s.c_str(), len);
174 }
175 
176 inline char& String::operator[](int whichOne) {
177  return c_str()[whichOne];
178 }
179 
180 inline String::operator std::string() const {
181  return std::string(c_str());
182 }
183 
184 inline char* String::c_str() const {
185 
186  if (data().getExactTypeInfoValue() >= 0) {
187  return (char*)&data();
188  } else {
189  return &(*data());
190  }
191 }
192 
193 inline size_t String::size() const {
194  if (data().getExactTypeInfoValue() >= 0) {
195  return data().getExactTypeInfoValue();
196  } else {
197  return -data().getExactTypeInfoValue();
198  }
199 }
200 
201 inline std::ostream& operator<<(std::ostream& stream, const String& printMe) {
202  char* returnVal = printMe.c_str();
203  stream << returnVal;
204  return stream;
205 }
206 
207 inline bool String::operator==(const String& toMe) {
208  return strcmp(c_str(), toMe.c_str()) == 0;
209 }
210 
211 inline bool String::operator==(const std::string& toMe) {
212  return strcmp(c_str(), toMe.c_str()) == 0;
213 }
214 
215 inline bool String::operator==(const char* toMe) {
216  return strcmp(c_str(), toMe) == 0;
217 }
218 
219 inline bool String::operator!=(const String& toMe) {
220  return strcmp(c_str(), toMe.c_str()) != 0;
221 }
222 
223 inline bool String::operator!=(const std::string& toMe) {
224  return strcmp(c_str(), toMe.c_str()) != 0;
225 }
226 
227 inline bool String::operator!=(const char* toMe) {
228  return strcmp(c_str(), toMe) != 0;
229 }
230 
231 
232 inline bool String::endsWith(const std::string& suffix) {
233  std::string str = c_str();
234  int len1 = str.length();
235  int len2 = suffix.length();
236  if (len1 > len2) {
237  return (str.compare(len1 - len2, len2, suffix) == 0);
238  } else {
239  return false;
240  }
241 }
242 
243 }
244 
245 #endif
String & operator=(const char *toMe)
Definition: PDBString.cc:50
size_t size() const
Definition: PDBString.cc:193
Handle< char > & data() const
Definition: PDBString.cc:33
size_t hash() const
Definition: PDBString.cc:45
bool operator!=(const String &toMe)
Definition: PDBString.cc:219
bool endsWith(const std::string &suffix)
Definition: PDBString.cc:232
size_t hashMe(char *me, size_t len)
Definition: PDBMap.cc:36
bool operator==(const String &toMe)
Definition: PDBString.cc:207
char & operator[](int whichOne)
Definition: PDBString.cc:176
char * c_str() const
Definition: PDBString.cc:184
char storage[sizeof(Handle< char >)]
Definition: PDBString.h:45
#define CAN_FIT_IN_DATA(len)
Definition: PDBString.cc:29
std::ostream & operator<<(std::ostream &stream, const String &printMe)
Definition: PDBString.cc:201