Simulated product dispenser

Dependencies:   HTS221

Fork of mbed-cloud-workshop-connect-HTS221 by Jim Carver

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers m2mstring.h Source File

m2mstring.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2015 ARM Limited. All rights reserved.
00003  * SPDX-License-Identifier: Apache-2.0
00004  * Licensed under the Apache License, Version 2.0 (the License); you may
00005  * not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #ifndef M2M_STRING_H
00017 #define M2M_STRING_H
00018 
00019 #include <stddef.h> // size_t
00020 #include <stdint.h>
00021 
00022 class Test_M2MString;
00023 
00024 namespace m2m
00025 {
00026 
00027 /*! \file m2mstring.h
00028 * \brief A simple C++ string class, used as replacement for std::string.
00029    */
00030   class String
00031   {
00032     char* p;           ///< The data.
00033     size_t allocated_;  ///< The allocated memory size (including trailing NULL).
00034     size_t size_;       ///< The currently used memory size (excluding trailing NULL).
00035 
00036   public:
00037     typedef size_t size_type;
00038     static const size_type npos;
00039 
00040     String();
00041     ~String();
00042     String(const String&);
00043     String(const char*);
00044     String(const char*, size_t);
00045 
00046     String& operator=(const char*);
00047     String& operator=(const String&);
00048 
00049     String& operator+=(const String&);
00050     String& operator+=(const char*);
00051     String& operator+=(char);
00052     void push_back(char);
00053 
00054     bool operator==(const char*) const;
00055     bool operator==(const String&) const;
00056 
00057     void clear();       // Set the string to empty (memory remains reserved).
00058 
00059     size_type size()   const   { return size_; }   ///< size without terminating NULL
00060     size_type length() const   { return size_; }   ///< as size()
00061 
00062     size_type capacity() const { return allocated_-1; }
00063 
00064     bool empty() const    { return size_ == 0; }
00065 
00066     const char* c_str() const { return p; } ///< raw data
00067 
00068     /** Reserve internal string memory so that n characters can be put into the
00069         string (plus 1 for the NULL char). If there is already enough memory,
00070         nothing happens, if not, the memory is realloated to exactly this
00071         amount.
00072         */
00073     void reserve( size_type n);
00074 
00075     /** Resize string. If n is less than the current size, the string is truncated.
00076         If n is larger, the memory is reallocated to exactly this amount, and
00077         the additional characters are NULL characters.
00078         */
00079     void resize( size_type n);
00080 
00081     /** Resize string. If n is less than the current size, the string is truncated.
00082         If n is larger, the memory is reallocated to exactly this amount, and
00083         the additional characters are c characters.
00084         */
00085     void resize( size_type n, char c);
00086 
00087     /// swap contents
00088     void swap( String& );
00089 
00090     String substr(const size_type pos, size_type length) const;
00091 
00092     // unchecked access:
00093     char& operator[](const size_type i)       { return p[i]; }
00094     char operator[](const size_type i) const { return p[i]; }
00095     // checked access:
00096     char at(const size_type i) const;
00097 
00098     /// erase len characters at position pos
00099     String& erase(size_type pos, size_type len);
00100     /// Append n characters of a string
00101     String& append(const char* str, size_type n);
00102 
00103     // Append n characters of a non-zero-terminated string
00104     // (in contrast with other append(), which performs strlen() for the given string).
00105     String& append_raw(const char*, size_type);
00106 
00107     // convert int to ascii and append it to end of string
00108     void append_int(int);
00109 
00110     int compare( size_type pos, size_type len, const String& str ) const;
00111     int compare( size_type pos, size_type len, const char*   str ) const;
00112 
00113     int find_last_of(char c) const;
00114 
00115     static uint8_t* convert_integer_to_array(int64_t value, uint8_t &size, const uint8_t *array = NULL, const uint32_t array_size = 0);
00116     static int64_t convert_array_to_integer(const uint8_t *value, const uint32_t size);
00117 
00118   private:
00119     // reallocate the internal memory
00120     void new_realloc( size_type n);
00121     char* strdup(const char* other);
00122 
00123     friend class ::Test_M2MString;
00124 
00125   };
00126   // class
00127 
00128   bool operator<(const String&, const String&);
00129 
00130   void reverse(char s[], uint32_t length);
00131 
00132   uint32_t itoa_c (int64_t n, char s[]);
00133 } // namespace
00134 
00135 
00136 #endif // M2M_STRING_H