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

  1 krisbash 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 krisbash 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 krisbash 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 krisbash 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 krisbash 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(_In_ MI_Char* buf) 
102                  {
103                      return reinterpret_cast<Header*>(
104                          reinterpret_cast<char*>(buf) - sizeof(Header));
105                  }
106 krisbash 1.1 
107                  // for compatability with "C", it must have only one pointer member!
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 krisbash 1.1 {
128                  if ( m_data )
129                  {
130                      AtomicInc(GetHeader(m_data)->m_refCounter);
131                  }
132              }
133              
134              inline String& String::operator+=(const String& x)
135              {
136                  if ( !m_data )
137                      return operator=(x);
138              
139                  return StrCat(x.Str(), x.GetSize());
140              }
141              
142              inline const MI_Char* String::Str() const 
143              {
144                  return m_data ? m_data : MI_T("");
145              }
146              
147              inline bool String::Equal(const MI_Char* str) const 
148 krisbash 1.1 {
149                  return memcmp(str, Str(), (GetSize()+1) * sizeof(MI_Char)) == 0;
150              }
151              
152              inline MI_Uint32 String::GetSize() const 
153              {
154                  return m_data ? GetHeader(m_data)->m_size : 0;
155              }
156              
157              inline bool operator==(const String& x, const String& y)
158              {
159                  return x.Equal(y);
160              }
161              
162              inline bool operator==(const String& x, const MI_Char* str)
163              {
164                  return x.Equal(str);
165              }
166              
167              inline bool operator==(const MI_Char* str, const String& x)
168              {
169 krisbash 1.1     return x.Equal(str);
170              }
171              
172              inline bool operator!=(const String& x, const String& y)
173              {
174                  return !x.Equal(y);
175              }
176              
177              inline bool operator!=(const String& x, const MI_Char* str)
178              {
179                  return !x.Equal(str);
180              }
181              
182              inline bool operator!=(const MI_Char* str, const String& x)
183              {
184                  return !x.Equal(str);
185              }
186              
187              MI_END_NAMESPACE
188              
189              #endif /* _micxx_string_h */

ViewCVS 0.9.2