(file) Return to string.h CVS log (file) (dir) Up to [OMI] / omi / micxx

  1 mike  1.1 /*
  2           **==============================================================================
  3           **
  4           ** Open Management Infrastructure (OMI)
  5           **
  6           ** Copyright (c) Microsoft Corporation
  7           ** 
  8           ** Licensed under the Apache License, Version 2.0 (the "License"); you may not 
  9           ** use this file except in compliance with the License. You may obtain a copy 
 10           ** of the License at 
 11           **
 12           **     http://www.apache.org/licenses/LICENSE-2.0 
 13           **
 14           ** THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15           ** KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED 
 16           ** WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, 
 17           ** MERCHANTABLITY OR NON-INFRINGEMENT. 
 18           **
 19           ** See the Apache 2 License for the specific language governing permissions 
 20           ** and limitations under the License.
 21           **
 22 mike  1.1 **==============================================================================
 23           */
 24           
 25           #ifndef _micxx_string_h
 26           #define _micxx_string_h
 27           
 28           #include <MI.h>
 29           #include "linkage.h"
 30           #include "atomic.h"
 31           
 32           MI_BEGIN_NAMESPACE
 33           
 34           class MICXX_LINKAGE String
 35           {
 36           public:
 37           
 38               String();
 39           
 40               String(const MI_Char* str);
 41           
 42               String(const MI_Char* str, MI_Uint32 size);
 43 mike  1.1 
 44               String(const String& x);
 45           
 46               ~String();
 47           
 48               void Clear();
 49           
 50               String& operator=(const String& x);
 51           
 52               String& operator+=(const String& x);
 53           
 54               String& operator+=(const MI_Char* str);
 55           
 56               String& operator+=(const MI_Char ch);
 57           
 58               void Append(const MI_Char* str, MI_Uint32 size);
 59           
 60               MI_Char& operator[](MI_Uint32 index);
 61           
 62               MI_Char operator[](MI_Uint32 index) const;
 63           
 64 mike  1.1 #if 0
 65               /* ATTN: disabled for now (not in standard basic_string<>) */
 66               operator const MI_Char*() const;
 67           #endif
 68           
 69               const MI_Char* Str() const;
 70           
 71               bool Equal(const String& x) const;
 72           
 73               bool Equal(const MI_Char* str) const;
 74           
 75               MI_Uint32 GetSize() const;
 76           
 77           private:
 78           
 79               struct Header
 80               {
 81                   AtomicType      m_refCounter;
 82                   MI_Uint32       m_size;
 83                   MI_Uint32       m_capacity;
 84               };
 85 mike  1.1 
 86               String& StrCat(const MI_Char* str, MI_Uint32 size);
 87           
 88               void AddRef();
 89           
 90               void Release();
 91           
 92               static MI_Char* Allocate(int capacity);
 93           
 94               // returns 0 - for 0; 16 for 1-6; 32 for 17-32 etc
 95               static int AlignCapacity(MI_Uint32 size) 
 96               {
 97                   const MI_Uint32 ALIGNMENT = 16;
 98                   return ((size + ALIGNMENT -1) / ALIGNMENT) * ALIGNMENT;
 99               }
100           
101               static Header* GetHeader(MI_Char* buf) 
102               {
103                   return reinterpret_cast<Header*>(
104                       reinterpret_cast<char*>(buf) - sizeof(Header));
105               }
106 mike  1.1 
107               // for compatability with "C", it must have only one pointer memeber!
108               MI_Char*  m_data;
109           };
110           
111           inline String::String() : m_data(0) 
112           {
113           }
114           
115           inline String::String(const String& x)
116           {
117               m_data = x.m_data;
118               AddRef();
119           }
120           
121           inline String::~String()
122           {
123               Release();
124           }
125           
126           inline void String::AddRef()
127 mike  1.1 {
128               if ( m_data )
129                   AtomicInc(GetHeader(m_data)->m_refCounter);
130           }
131           
132           inline String& String::operator+=(const String& x)
133           {
134               if ( !m_data )
135                   return operator=(x);
136           
137               return StrCat(x.Str(), x.GetSize());
138           }
139           
140           inline const MI_Char* String::Str() const 
141           {
142               return m_data ? m_data : MI_T("");
143           }
144           
145           inline bool String::Equal(const MI_Char* str) const 
146           {
147               return memcmp(str, Str(), (GetSize()+1) * sizeof(MI_Char)) == 0;
148 mike  1.1 }
149           
150           inline MI_Uint32 String::GetSize() const 
151           {
152               return m_data ? GetHeader(m_data)->m_size : 0;
153           }
154           
155           inline bool operator==(const String& x, const String& y)
156           {
157               return x.Equal(y);
158           }
159           
160           inline bool operator==(const String& x, const MI_Char* str)
161           {
162               return x.Equal(str);
163           }
164           
165           inline bool operator==(const MI_Char* str, const String& x)
166           {
167               return x.Equal(str);
168           }
169 mike  1.1 
170           inline bool operator!=(const String& x, const String& y)
171           {
172               return !x.Equal(y);
173           }
174           
175           inline bool operator!=(const String& x, const MI_Char* str)
176           {
177               return !x.Equal(str);
178           }
179           
180           inline bool operator!=(const MI_Char* str, const String& x)
181           {
182               return !x.Equal(str);
183           }
184           
185           MI_END_NAMESPACE
186           
187           #endif /* _micxx_string_h */

ViewCVS 0.9.2