Solution for Bluetooth SIG hands-on training course
Dependencies: BLE_API mbed-dev-bin nRF51822-bluetooth-mdw
Fork of microbit-dal-bluetooth-mdw_starter by
ManagedString.cpp
00001 /* 00002 The MIT License (MIT) 00003 00004 Copyright (c) 2016 British Broadcasting Corporation. 00005 This software is provided by Lancaster University by arrangement with the BBC. 00006 00007 Permission is hereby granted, free of charge, to any person obtaining a 00008 copy of this software and associated documentation files (the "Software"), 00009 to deal in the Software without restriction, including without limitation 00010 the rights to use, copy, modify, merge, publish, distribute, sublicense, 00011 and/or sell copies of the Software, and to permit persons to whom the 00012 Software is furnished to do so, subject to the following conditions: 00013 00014 The above copyright notice and this permission notice shall be included in 00015 all copies or substantial portions of the Software. 00016 00017 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00018 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00019 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 00020 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00021 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 00022 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 00023 DEALINGS IN THE SOFTWARE. 00024 */ 00025 00026 /** 00027 * Class definition for a ManagedString. 00028 * 00029 * Uses basic reference counting to implement a copy-assignable, immutable string. 00030 * 00031 * This maps closely to the constructs found in many high level application languages, 00032 * such as Touch Develop. 00033 * 00034 * Written from first principles here, for several reasons: 00035 * 1) std::shared_ptr is not yet availiable on the ARMCC compiler 00036 * 00037 * 2) to reduce memory footprint - we don't need many of the other features in the std library 00038 * 00039 * 3) it makes an interesting case study for anyone interested in seeing how it works! 00040 * 00041 * 4) we need explicit reference counting to inter-op with low-level application langauge runtimes. 00042 * 00043 * 5) the reference counting needs to also work for read-only, flash-resident strings 00044 */ 00045 #include <string.h> 00046 #include <stdlib.h> 00047 00048 #include "mbed.h" 00049 #include "MicroBitConfig.h" 00050 #include "ManagedString.h" 00051 #include "MicroBitCompat.h" 00052 00053 static const char empty[] __attribute__ ((aligned (4))) = "\xff\xff\0\0\0"; 00054 00055 /** 00056 * Internal constructor helper. 00057 * 00058 * Configures this ManagedString to refer to the static EmptyString 00059 */ 00060 void ManagedString::initEmpty() 00061 { 00062 ptr = (StringData*)(void*)empty; 00063 } 00064 00065 /** 00066 * Internal constructor helper. 00067 * 00068 * Creates this ManagedString based on a given null terminated char array. 00069 */ 00070 void ManagedString::initString(const char *str) 00071 { 00072 // Initialise this ManagedString as a new string, using the data provided. 00073 // We assume the string is sane, and null terminated. 00074 int len = strlen(str); 00075 ptr = (StringData *) malloc(4+len+1); 00076 ptr->init(); 00077 ptr->len = len; 00078 memcpy(ptr->data, str, len+1); 00079 } 00080 00081 /** 00082 * Constructor. 00083 * Create a managed string from a specially prepared string literal. 00084 * 00085 * @param ptr The literal - first two bytes should be 0xff, then the length in little endian, then the literal. The literal has to be 4-byte aligned. 00086 * 00087 * @code 00088 * static const char hello[] __attribute__ ((aligned (4))) = "\xff\xff\x05\x00" "Hello"; 00089 * ManagedString s((StringData*)(void*)hello); 00090 * @endcode 00091 */ 00092 ManagedString::ManagedString(StringData *p) 00093 { 00094 ptr = p; 00095 ptr->incr(); 00096 } 00097 00098 /** 00099 * Get current ptr, do not decr() it, and set the current instance to empty string. 00100 * 00101 * This is to be used by specialized runtimes which pass StringData around. 00102 */ 00103 StringData* ManagedString::leakData() 00104 { 00105 StringData *res = ptr; 00106 initEmpty(); 00107 return res; 00108 } 00109 00110 /** 00111 * Constructor. 00112 * 00113 * Create a managed string from a given integer. 00114 * 00115 * @param value The integer from which to create the ManagedString. 00116 * 00117 * @code 00118 * ManagedString s(20); 00119 * @endcode 00120 */ 00121 ManagedString::ManagedString(const int value) 00122 { 00123 char str[12]; 00124 00125 itoa(value, str); 00126 initString(str); 00127 } 00128 00129 /** 00130 * Constructor. 00131 * Create a managed string from a given char. 00132 * 00133 * @param value The character from which to create the ManagedString. 00134 * 00135 * @code 00136 * ManagedString s('a'); 00137 * @endcode 00138 */ 00139 ManagedString::ManagedString(const char value) 00140 { 00141 char str[2] = {value, 0}; 00142 initString(str); 00143 } 00144 00145 00146 /** 00147 * Constructor. 00148 * 00149 * Create a managed string from a pointer to an 8-bit character buffer. 00150 * 00151 * The buffer is copied to ensure safe memory management (the supplied 00152 * character buffer may be declared on the stack for instance). 00153 * 00154 * @param str The character array on which to base the new ManagedString. 00155 * 00156 * @code 00157 * ManagedString s("abcdefg"); 00158 * @endcode 00159 */ 00160 ManagedString::ManagedString(const char *str) 00161 { 00162 // Sanity check. Return EmptyString for anything distasteful 00163 if (str == NULL || *str == 0) 00164 { 00165 initEmpty(); 00166 return; 00167 } 00168 00169 initString(str); 00170 } 00171 00172 /** 00173 * Private Constructor. 00174 * 00175 * Create a managed string based on a concat of two strings. 00176 * The buffer is copied to ensure sane memory management (the supplied 00177 * character buffer may be declared on the stack for instance). 00178 * 00179 * @param str1 The first string on which to base the new ManagedString. 00180 * 00181 * @param str2 The second string on which to base the new ManagedString. 00182 */ 00183 ManagedString::ManagedString(const ManagedString &s1, const ManagedString &s2) 00184 { 00185 // Calculate length of new string. 00186 int len = s1.length() + s2.length(); 00187 00188 // Create a new buffer for holding the new string data. 00189 ptr = (StringData*) malloc(4+len+1); 00190 ptr->init(); 00191 ptr->len = len; 00192 00193 // Enter the data, and terminate the string. 00194 memcpy(ptr->data, s1.toCharArray(), s1.length()); 00195 memcpy(ptr->data + s1.length(), s2.toCharArray(), s2.length()); 00196 ptr->data[len] = 0; 00197 } 00198 00199 00200 /** 00201 * Constructor. 00202 * Create a ManagedString from a PacketBuffer. All bytes in the 00203 * PacketBuffer are added to the ManagedString. 00204 * 00205 * @param buffer The PacktBuffer from which to create the ManagedString. 00206 * 00207 * @code 00208 * ManagedString s = radio.datagram.recv(); 00209 * @endcode 00210 */ 00211 ManagedString::ManagedString(PacketBuffer buffer) 00212 { 00213 // Allocate a new buffer ( just in case the data is not NULL terminated). 00214 ptr = (StringData*) malloc(4+buffer.length()+1); 00215 ptr->init(); 00216 00217 // Store the length of the new string 00218 ptr->len = buffer.length(); 00219 memcpy(ptr->data, buffer.getBytes(), buffer.length()); 00220 ptr->data[buffer.length()] = 0; 00221 } 00222 00223 /** 00224 * Constructor. 00225 * Create a ManagedString from a pointer to an 8-bit character buffer of a given length. 00226 * 00227 * The buffer is copied to ensure sane memory management (the supplied 00228 * character buffer may be declared on the stack for instance). 00229 * 00230 * @param str The character array on which to base the new ManagedString. 00231 * 00232 * @param length The length of the character array 00233 * 00234 * @code 00235 * ManagedString s("abcdefg",7); 00236 * @endcode 00237 */ 00238 ManagedString::ManagedString(const char *str, const int16_t length) 00239 { 00240 // Sanity check. Return EmptyString for anything distasteful 00241 if (str == NULL || *str == 0 || (uint16_t)length > strlen(str)) // XXX length should be unsigned on the interface 00242 { 00243 initEmpty(); 00244 return; 00245 } 00246 00247 00248 // Allocate a new buffer, and create a NULL terminated string. 00249 ptr = (StringData*) malloc(4+length+1); 00250 ptr->init(); 00251 // Store the length of the new string 00252 ptr->len = length; 00253 memcpy(ptr->data, str, length); 00254 ptr->data[length] = 0; 00255 } 00256 00257 /** 00258 * Copy constructor. 00259 * Makes a new ManagedString identical to the one supplied. 00260 * 00261 * Shares the character buffer and reference count with the supplied ManagedString. 00262 * 00263 * @param s The ManagedString to copy. 00264 * 00265 * @code 00266 * ManagedString s("abcdefg"); 00267 * ManagedString p(s); 00268 * @endcode 00269 */ 00270 ManagedString::ManagedString(const ManagedString &s) 00271 { 00272 ptr = s.ptr; 00273 ptr->incr(); 00274 } 00275 00276 00277 /** 00278 * Default constructor. 00279 * 00280 * Create an empty ManagedString. 00281 * 00282 * @code 00283 * ManagedString s(); 00284 * @endcode 00285 */ 00286 ManagedString::ManagedString() 00287 { 00288 initEmpty(); 00289 } 00290 00291 /** 00292 * Destructor. 00293 * 00294 * Free this ManagedString, and decrement the reference count to the 00295 * internal character buffer. 00296 * 00297 * If we're holding the last reference, also free the character buffer. 00298 */ 00299 ManagedString::~ManagedString() 00300 { 00301 ptr->decr(); 00302 } 00303 00304 /** 00305 * Copy assign operation. 00306 * 00307 * Called when one ManagedString is assigned the value of another. 00308 * 00309 * If the ManagedString being assigned is already referring to a character buffer, 00310 * decrement the reference count and free up the buffer as necessary. 00311 * 00312 * Then, update our character buffer to refer to that of the supplied ManagedString, 00313 * and increase its reference count. 00314 * 00315 * @param s The ManagedString to copy. 00316 * 00317 * @code 00318 * ManagedString s("abcd"); 00319 * ManagedString p("efgh"); 00320 * p = s // p now points to s, s' ref is incremented 00321 * @endcode 00322 */ 00323 ManagedString& ManagedString::operator = (const ManagedString& s) 00324 { 00325 if (this->ptr == s.ptr) 00326 return *this; 00327 00328 ptr->decr(); 00329 ptr = s.ptr; 00330 ptr->incr(); 00331 00332 return *this; 00333 } 00334 00335 /** 00336 * Equality operation. 00337 * 00338 * Called when one ManagedString is tested to be equal to another using the '==' operator. 00339 * 00340 * @param s The ManagedString to test ourselves against. 00341 * 00342 * @return true if this ManagedString is identical to the one supplied, false otherwise. 00343 * 00344 * @code 00345 * MicroBitDisplay display; 00346 * ManagedString s("abcd"); 00347 * ManagedString p("efgh"); 00348 * 00349 * if(p == s) 00350 * display.scroll("We are the same!"); 00351 * else 00352 * display.scroll("We are different!"); //p is not equal to s - this will be called 00353 * @endcode 00354 */ 00355 bool ManagedString::operator== (const ManagedString& s) 00356 { 00357 return ((length() == s.length()) && (strcmp(toCharArray(),s.toCharArray())==0)); 00358 } 00359 00360 /** 00361 * Inequality operation. 00362 * 00363 * Called when one ManagedString is tested to be less than another using the '<' operator. 00364 * 00365 * @param s The ManagedString to test ourselves against. 00366 * 00367 * @return true if this ManagedString is alphabetically less than to the one supplied, false otherwise. 00368 * 00369 * @code 00370 * MicroBitDisplay display; 00371 * ManagedString s("a"); 00372 * ManagedString p("b"); 00373 * 00374 * if(s < p) 00375 * display.scroll("a is before b!"); //a is before b 00376 * else 00377 * display.scroll("b is before a!"); 00378 * @endcode 00379 */ 00380 bool ManagedString::operator< (const ManagedString& s) 00381 { 00382 return (strcmp(toCharArray(), s.toCharArray())<0); 00383 } 00384 00385 /** 00386 * Inequality operation. 00387 * 00388 * Called when one ManagedString is tested to be greater than another using the '>' operator. 00389 * 00390 * @param s The ManagedString to test ourselves against. 00391 * 00392 * @return true if this ManagedString is alphabetically greater than to the one supplied, false otherwise. 00393 * 00394 * @code 00395 * MicroBitDisplay display; 00396 * ManagedString s("a"); 00397 * ManagedString p("b"); 00398 * 00399 * if(p>a) 00400 * display.scroll("b is after a!"); //b is after a 00401 * else 00402 * display.scroll("a is after b!"); 00403 * @endcode 00404 */ 00405 bool ManagedString::operator> (const ManagedString& s) 00406 { 00407 return (strcmp(toCharArray(), s.toCharArray())>0); 00408 } 00409 00410 /** 00411 * Extracts a ManagedString from this string, at the position provided. 00412 * 00413 * @param start The index of the first character to extract, indexed from zero. 00414 * 00415 * @param length The number of characters to extract from the start position 00416 * 00417 * @return a ManagedString representing the requested substring. 00418 * 00419 * @code 00420 * MicroBitDisplay display; 00421 * ManagedString s("abcdefg"); 00422 * 00423 * display.scroll(s.substring(0,2)) // displays "ab" 00424 * @endcode 00425 */ 00426 ManagedString ManagedString::substring(int16_t start, int16_t length) 00427 { 00428 // If the parameters are illegal, just return a reference to the empty string. 00429 if (start >= this->length()) 00430 return ManagedString(ManagedString::EmptyString); 00431 00432 // Compute a safe copy length; 00433 length = min(this->length()-start, length); 00434 00435 // Build a ManagedString from this. 00436 return ManagedString(toCharArray()+start, length); 00437 } 00438 00439 /** 00440 * Concatenates two strings. 00441 * 00442 * @param lhs The first ManagedString to concatenate. 00443 * @param rhs The second ManagedString to concatenate. 00444 * 00445 * @return a new ManagedString representing the joined strings. 00446 * 00447 * @code 00448 * MicroBitDisplay display; 00449 * ManagedString s("abcd"); 00450 * ManagedString p("efgh") 00451 * 00452 * display.scroll(s + p) // scrolls "abcdefgh" 00453 * @endcode 00454 */ 00455 ManagedString operator+ (const ManagedString& lhs, const ManagedString& rhs) 00456 { 00457 00458 // If the either string is empty, nothing to do! 00459 if (rhs.length() == 0) 00460 return lhs; 00461 00462 if (lhs.length() == 0) 00463 return rhs; 00464 00465 return ManagedString(lhs, rhs); 00466 } 00467 00468 00469 /** 00470 * Provides a character value at a given position in the string, indexed from zero. 00471 * 00472 * @param index The position of the character to return. 00473 * 00474 * @return the character at position index, zero if index is invalid. 00475 * 00476 * @code 00477 * MicroBitDisplay display; 00478 * ManagedString s("abcd"); 00479 * 00480 * display.scroll(s.charAt(1)) // scrolls "b" 00481 * @endcode 00482 */ 00483 char ManagedString::charAt(int16_t index) 00484 { 00485 return (index >=0 && index < length()) ? ptr->data[index] : 0; 00486 } 00487 00488 /** 00489 * Empty string constant literal 00490 */ 00491 ManagedString ManagedString::EmptyString((StringData*)(void*)empty);
Generated on Tue Jul 12 2022 20:39:13 by 1.7.2