Austin Blackstone / Mbed 2 deprecated mbed-client-classic-example-lwip

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of mbed-client-classic-example-lwip by sandbox

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers m2mstring.h Source File

m2mstring.h

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 #include <stddef.h> // size_t
00017 #include <stdexcept>
00018 
00019 class Test_M2MString;
00020 
00021 namespace m2m
00022 {
00023 
00024   /** @brief Simple C++ string class, used as replacement for
00025    std::string.
00026    */
00027   class String
00028   {
00029     char*     p;           ///< The data
00030     size_t    allocated_;  ///< The allocated memory size (including trailing NULL)
00031     size_t    size_;       ///< The currently used memory size (excluding trailing NULL)
00032 
00033   public:
00034     typedef size_t size_type;
00035     static const   size_type npos;
00036 
00037     String();
00038     virtual ~String();
00039     String(const String&);
00040     String(const char*);
00041 
00042     String&  operator=(const char*);
00043     String&  operator=(const String&);
00044 
00045     String&  operator+=(const String&);
00046     String&  operator+=(const char*);
00047     String&  operator+=(char);
00048     void     push_back(char);
00049 
00050     //No need for this += is
00051     //friend String operator+(const String& lhs, const String& rhs);
00052 
00053     bool     operator==(const char*) const;
00054     bool     operator==(const String&) const;
00055 
00056     void     clear();       // set string to empty string (memory remains reserved)
00057 
00058     size_type size()   const   { return size_; }   ///< size without terminating NULL
00059     size_type length() const   { return size_; }   ///< as size()
00060 
00061     /// size if fully used
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 will be 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 will be truncated.
00076         If n is larger, the memory will be reallocated to exactly this amount, and
00077         the additional characters will be NULL characters.
00078         */
00079     void resize( size_type n);
00080 
00081     /** Resize string. If n is less than the current size, the string will be truncated.
00082         If n is larger, the memory will be reallocated to exactly this amount, and
00083         the additional characters will be 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);
00097     char     at(const size_type i) const;
00098 
00099     /// erase len characters at position pos
00100     String& erase(size_type pos, size_type len);
00101     /// Append n characters of a string
00102     String& append(const char* str, size_type n);
00103 
00104     int     compare( size_type pos, size_type len, const String& str ) const;
00105     int     compare( size_type pos, size_type len, const char*   str ) const;
00106 
00107     int     find_last_of(char c) const;
00108 
00109   private:
00110     // reallocate the internal memory
00111     void  new_realloc( size_type n);
00112     char* strdup_never_null(const char* other);
00113 
00114     char _return_value;
00115 
00116     friend class ::Test_M2MString;
00117 
00118   };
00119   // class
00120 
00121   bool
00122   operator<(const String&, const String&);
00123 
00124 } // namespace