ACE_SString Class Reference

A very Simple String ACE_SString class. This is not a general-purpose string class, and you should probably consider using ACE_CString is you don't understand why this class exists... More...

#include <SString.h>

Collaboration diagram for ACE_SString:

Collaboration graph
[legend]
List of all members.

Public Types

typedef ACE_Allocator::size_type size_type

Public Member Functions

 ACE_SString (ACE_Allocator *alloc=0)
 Default constructor.
 ACE_SString (const char *s, ACE_Allocator *alloc=0)
 Constructor that copies s into dynamically allocated memory.
 ACE_SString (const char *s, size_type len, ACE_Allocator *alloc=0)
 ACE_SString (const ACE_SString &)
 Copy constructor.
 ACE_SString (char c, ACE_Allocator *alloc=0)
 Constructor that copies c into dynamically allocated memory.
 ~ACE_SString (void)
 Default destructor.
char operator[] (size_type slot) const
char & operator[] (size_type slot)
ACE_SStringoperator= (const ACE_SString &)
 Assignment operator (does copy memory).
ACE_SString substring (size_type offset, size_type length=npos) const
ACE_SString substr (size_type offset, size_type length=npos) const
 Same as substring.
u_long hash (void) const
 Returns a hash value for this string.
size_type length (void) const
 Return the length of the string.
void rep (char *s)
const char * rep (void) const
 Get the underlying pointer.
const char * fast_rep (void) const
 Get the underlying pointer.
const char * c_str (void) const
 Same as STL String's <c_str> and <fast_rep>.
size_type strstr (const ACE_SString &s) const
size_type find (const ACE_SString &str, size_type pos=0) const
size_type find (const char *s, size_type pos=0) const
size_type find (char c, size_type pos=0) const
size_type rfind (char c, size_type pos=npos) const
bool operator== (const ACE_SString &s) const
 Equality comparison operator (must match entire string).
bool operator< (const ACE_SString &s) const
 Less than comparison operator.
bool operator> (const ACE_SString &s) const
 Greater than comparison operator.
bool operator!= (const ACE_SString &s) const
 Inequality comparison operator.
int compare (const ACE_SString &s) const
 Performs a <strcmp>-style comparison.
void dump (void) const
 Dump the state of an object.

Public Attributes

 ACE_ALLOC_HOOK_DECLARE
 Declare the dynamic allocation hooks.

Static Public Attributes

static const size_type npos
 No position constant.

Private Attributes

ACE_Allocatorallocator_
 Pointer to a memory allocator.
size_type len_
 Length of the ACE_SString (not counting the trailing '').
char * rep_
 Pointer to data.

Detailed Description

A very Simple String ACE_SString class. This is not a general-purpose string class, and you should probably consider using ACE_CString is you don't understand why this class exists...

This class is optimized for efficiency, so it doesn't provide any internal locking. CAUTION: This class is only intended for use with applications that understand how it works. In particular, its destructor does not deallocate its memory when it is destroyed... We need this class since the ACE_Map_Manager requires an object that supports the operator == and operator !=. This class uses an ACE_Allocator to allocate memory. The user can make this a persistant class by providing an ACE_Allocator with a persistable memory pool.

Definition at line 127 of file SString.h.


Member Typedef Documentation

typedef ACE_Allocator::size_type ACE_SString::size_type

Definition at line 131 of file SString.h.


Constructor & Destructor Documentation

ACE_SString::ACE_SString ( ACE_Allocator alloc = 0  ) 

Default constructor.

Definition at line 194 of file SString.cpp.

References ACE_TRACE, allocator_, ACE_Allocator::instance(), len_, and rep_.

Referenced by substring().

00195   : allocator_ (alloc),
00196     len_ (0),
00197     rep_ (0)
00198 
00199 {
00200   ACE_TRACE ("ACE_SString::ACE_SString");
00201 
00202   if (this->allocator_ == 0)
00203     this->allocator_ = ACE_Allocator::instance ();
00204 
00205   this->len_ = 0;
00206   this->rep_ = (char *) this->allocator_->malloc (this->len_ + 1);
00207   this->rep_[this->len_] = '\0';
00208 }

ACE_SString::ACE_SString ( const char *  s,
ACE_Allocator alloc = 0 
)

Constructor that copies s into dynamically allocated memory.

Definition at line 227 of file SString.cpp.

References ACE_TRACE, allocator_, ACE_Allocator::instance(), len_, rep_, ACE_OS::strcpy(), and ACE_OS::strlen().

00229   : allocator_ (alloc)
00230 {
00231   ACE_TRACE ("ACE_SString::ACE_SString");
00232 
00233   if (this->allocator_ == 0)
00234     this->allocator_ = ACE_Allocator::instance ();
00235 
00236   if (s == 0)
00237     {
00238       this->len_ = 0;
00239       this->rep_ = (char *) this->allocator_->malloc (this->len_ + 1);
00240       this->rep_[this->len_] = '\0';
00241     }
00242   else
00243     {
00244       this->len_ = ACE_OS::strlen (s);
00245       this->rep_ = (char *) this->allocator_->malloc (this->len_ + 1);
00246       ACE_OS::strcpy (this->rep_, s);
00247     }
00248 }

ACE_SString::ACE_SString ( const char *  s,
size_type  len,
ACE_Allocator alloc = 0 
)

Constructor that copies len chars of into dynamically allocated memory (will NUL terminate the result).

Definition at line 267 of file SString.cpp.

References ACE_TRACE, allocator_, ACE_Allocator::instance(), len_, ACE_OS::memcpy(), and rep_.

00270   : allocator_ (alloc)
00271 {
00272   ACE_TRACE ("ACE_SString::ACE_SString");
00273 
00274   if (this->allocator_ == 0)
00275     this->allocator_ = ACE_Allocator::instance ();
00276 
00277   if (s == 0)
00278     {
00279       this->len_ = 0;
00280       this->rep_ = (char *) this->allocator_->malloc (this->len_ + 1);
00281       this->rep_[this->len_] = '\0';
00282     }
00283   else
00284     {
00285       this->len_ = len;
00286       this->rep_ = (char *) this->allocator_->malloc (this->len_ + 1);
00287       ACE_OS::memcpy (this->rep_, s, len);
00288       this->rep_[len] = '\0'; // Make sure to NUL terminate this!
00289     }
00290 }

ACE_SString::ACE_SString ( const ACE_SString  ) 

Copy constructor.

Definition at line 176 of file SString.cpp.

References ACE_TRACE, allocator_, ACE_Allocator::instance(), len_, ACE_OS::memcpy(), and rep_.

00177   : allocator_ (s.allocator_),
00178     len_ (s.len_)
00179 {
00180   ACE_TRACE ("ACE_SString::ACE_SString");
00181 
00182   if (this->allocator_ == 0)
00183     this->allocator_ = ACE_Allocator::instance ();
00184 
00185   this->rep_ = (char *) this->allocator_->malloc (s.len_ + 1);
00186   ACE_OS::memcpy ((void *) this->rep_,
00187                   (const void *) s.rep_,
00188                   this->len_);
00189   this->rep_[this->len_] = '\0';
00190 }

ACE_SString::ACE_SString ( char  c,
ACE_Allocator alloc = 0 
)

Constructor that copies c into dynamically allocated memory.

Definition at line 250 of file SString.cpp.

References ACE_TRACE, allocator_, ACE_Allocator::instance(), len_, and rep_.

00252   : allocator_ (alloc)
00253 {
00254   ACE_TRACE ("ACE_SString::ACE_SString");
00255 
00256   if (this->allocator_ == 0)
00257     this->allocator_ = ACE_Allocator::instance ();
00258 
00259   this->len_ = 1;
00260   this->rep_ = (char *) this->allocator_->malloc (this->len_ + 1);
00261   this->rep_[0] = c;
00262   this->rep_[this->len_] = '\0';
00263 }

ACE_INLINE ACE_SString::~ACE_SString ( void   ) 

Default destructor.

Definition at line 66 of file SString.inl.

00067 {
00068 }


Member Function Documentation

ACE_INLINE const char * ACE_SString::c_str ( void   )  const

Same as STL String's <c_str> and <fast_rep>.

Definition at line 116 of file SString.inl.

References ACE_TRACE, and rep_.

00117 {
00118   ACE_TRACE ("ACE_SString::c_str");
00119   return this->rep_;
00120 }

ACE_INLINE int ACE_SString::compare ( const ACE_SString s  )  const

Performs a <strcmp>-style comparison.

Definition at line 166 of file SString.inl.

References ACE_TRACE, rep_, and ACE_OS::strcmp().

00167 {
00168   ACE_TRACE ("ACE_SString::compare");
00169   return ACE_OS::strcmp (this->rep_, s.rep_);
00170 }

void ACE_SString::dump ( void   )  const

Dump the state of an object.

Definition at line 167 of file SString.cpp.

References ACE_TRACE.

00168 {
00169 #if defined (ACE_HAS_DUMP)
00170   ACE_TRACE ("ACE_SString::dump");
00171 #endif /* ACE_HAS_DUMP */
00172 }

ACE_INLINE const char * ACE_SString::fast_rep ( void   )  const

Get the underlying pointer.

Definition at line 107 of file SString.inl.

References ACE_TRACE, and rep_.

Referenced by operator<<().

00108 {
00109   ACE_TRACE ("ACE_SString::fast_rep");
00110   return this->rep_;
00111 }

ACE_INLINE ACE_SString::size_type ACE_SString::find ( char  c,
size_type  pos = 0 
) const

Find starting at pos. Returns the slot of the first location that matches (will be >= pos), else npos.

Definition at line 184 of file SString.inl.

References npos, rep_, ACE_OS::strchr(), and substr().

00185 {
00186   char *substr = this->rep_ + pos;
00187   char *pointer = ACE_OS::strchr (substr, c);
00188   if (pointer == 0)
00189     return ACE_SString::npos;
00190   else
00191     return pointer - this->rep_;
00192 }

ACE_INLINE ACE_SString::size_type ACE_SString::find ( const char *  s,
size_type  pos = 0 
) const

Find <s> starting at pos. Returns the slot of the first location that matches (will be >= pos), else npos.

Definition at line 173 of file SString.inl.

References npos, rep_, ACE_OS::strstr(), and substr().

00174 {
00175   char *substr = this->rep_ + pos;
00176   char *pointer = ACE_OS::strstr (substr, s);
00177   if (pointer == 0)
00178     return ACE_SString::npos;
00179   else
00180     return pointer - this->rep_;
00181 }

ACE_INLINE ACE_SString::size_type ACE_SString::find ( const ACE_SString str,
size_type  pos = 0 
) const

Find <str> starting at pos. Returns the slot of the first location that matches (will be >= pos), else npos.

Definition at line 203 of file SString.inl.

References rep_.

Referenced by strstr().

00204 {
00205   return this->find (str.rep_, pos);
00206 }

ACE_INLINE u_long ACE_SString::hash ( void   )  const

Returns a hash value for this string.

Definition at line 224 of file SString.inl.

References ACE::hash_pjw().

00225 {
00226   return ACE::hash_pjw (this->rep_);
00227 }

ACE_INLINE ACE_SString::size_type ACE_SString::length ( void   )  const

Return the length of the string.

Definition at line 230 of file SString.inl.

References ACE_TRACE, and len_.

00231 {
00232   ACE_TRACE ("ACE_SString::length");
00233   return this->len_;
00234 }

ACE_INLINE bool ACE_SString::operator!= ( const ACE_SString s  )  const

Inequality comparison operator.

Definition at line 159 of file SString.inl.

References ACE_TRACE.

00160 {
00161   ACE_TRACE ("ACE_SString::operator!=");
00162   return !(*this == s);
00163 }

ACE_INLINE bool ACE_SString::operator< ( const ACE_SString s  )  const

Less than comparison operator.

Definition at line 135 of file SString.inl.

References ACE_TRACE, rep_, and ACE_OS::strcmp().

00136 {
00137   ACE_TRACE ("ACE_SString::operator <");
00138 
00139   return (this->rep_ && s.rep_)
00140     ? ACE_OS::strcmp (this->rep_, s.rep_) < 0
00141     : ((s.rep_) ? true : false);
00142 }

ACE_SString & ACE_SString::operator= ( const ACE_SString  ) 

Assignment operator (does copy memory).

Definition at line 295 of file SString.cpp.

References ACE_TRACE, allocator_, ACE_Allocator::free(), len_, rep_, and ACE_OS::strcpy().

00296 {
00297   ACE_TRACE ("ACE_SString::operator=");
00298   // Check for identify.
00299 
00300   if (this != &s)
00301     {
00302       // Only reallocate if we don't have enough space...
00303       if (this->len_ < s.len_)
00304         {
00305           this->allocator_->free (this->rep_);
00306           this->rep_ = (char *) this->allocator_->malloc (s.len_ + 1);
00307         }
00308       this->len_ = s.len_;
00309       ACE_OS::strcpy (this->rep_, s.rep_);
00310     }
00311 
00312   return *this;
00313 }

ACE_INLINE bool ACE_SString::operator== ( const ACE_SString s  )  const

Equality comparison operator (must match entire string).

Definition at line 125 of file SString.inl.

References ACE_TRACE, len_, rep_, and ACE_OS::strcmp().

00126 {
00127   ACE_TRACE ("ACE_SString::operator==");
00128   return this->len_ == s.len_
00129     && ACE_OS::strcmp (this->rep_, s.rep_) == 0;
00130 }

ACE_INLINE bool ACE_SString::operator> ( const ACE_SString s  )  const

Greater than comparison operator.

Definition at line 147 of file SString.inl.

References ACE_TRACE, rep_, and ACE_OS::strcmp().

00148 {
00149   ACE_TRACE ("ACE_SString::operator >");
00150 
00151   return (this->rep_ && s.rep_)
00152     ? ACE_OS::strcmp (this->rep_, s.rep_) > 0
00153     : ((this->rep_) ? true : false );
00154 }

ACE_INLINE char & ACE_SString::operator[] ( size_type  slot  ) 

Return the <slot'th> character by reference in the string (doesn't perform bounds checking).

Definition at line 89 of file SString.inl.

References ACE_TRACE, and rep_.

00090 {
00091   ACE_TRACE ("ACE_SString::operator[]");
00092   return this->rep_[slot];
00093 }

ACE_INLINE char ACE_SString::operator[] ( size_type  slot  )  const

Return the <slot'th> character in the string (doesn't perform bounds checking).

Definition at line 80 of file SString.inl.

References ACE_TRACE, and rep_.

00081 {
00082   ACE_TRACE ("ACE_SString::operator[]");
00083   return this->rep_[slot];
00084 }

ACE_INLINE const char * ACE_SString::rep ( void   )  const

Get the underlying pointer.

Definition at line 98 of file SString.inl.

References ACE_TRACE, and rep_.

00099 {
00100   ACE_TRACE ("ACE_SString::rep");
00101   return this->rep_;
00102 }

void ACE_SString::rep ( char *  s  ) 

Set the underlying pointer. Since this does not copy memory or delete existing memory use with extreme caution!!!

Definition at line 213 of file SString.cpp.

References ACE_TRACE, len_, rep_, and ACE_OS::strlen().

00214 {
00215   ACE_TRACE ("ACE_SString::rep");
00216 
00217   this->rep_ = s;
00218 
00219   if (s == 0)
00220     this->len_ = 0;
00221   else
00222     this->len_ = ACE_OS::strlen (s);
00223 }

ACE_INLINE ACE_SString::size_type ACE_SString::rfind ( char  c,
size_type  pos = npos 
) const

Find starting at pos (counting from the end). Returns the slot of the first location that matches, else npos.

Definition at line 209 of file SString.inl.

References len_, and npos.

00210 {
00211   if (pos == ACE_SString::npos)
00212     pos = this->len_;
00213 
00214   // Do not change to prefix operator!  Proper operation of this loop
00215   // depends on postfix decrement behavior.
00216   for (size_type i = pos; i-- != 0; )
00217     if (this->rep_[i] == c)
00218       return i;
00219 
00220   return ACE_SString::npos;
00221 }

ACE_INLINE ACE_SString::size_type ACE_SString::strstr ( const ACE_SString s  )  const

Comparison operator that will match substrings. Returns the slot of the first location that matches, else npos.

Definition at line 195 of file SString.inl.

References ACE_TRACE, find(), and rep_.

00196 {
00197   ACE_TRACE ("ACE_SString::strstr");
00198 
00199   return this->find (s.rep_);
00200 }

ACE_INLINE ACE_SString ACE_SString::substr ( size_type  offset,
size_type  length = npos 
) const

Same as substring.

Definition at line 71 of file SString.inl.

References substring().

Referenced by find().

00073 {
00074   return this->substring (offset, length);
00075 }

ACE_SString ACE_SString::substring ( size_type  offset,
size_type  length = npos 
) const

Return a substring given an offset and length, if length == npos use rest of str return empty substring if offset or offset/length are invalid

Definition at line 317 of file SString.cpp.

References ACE_SString(), len_, npos, and rep_.

Referenced by substr().

00319 {
00320   size_t count = length;
00321 
00322   // case 1. empty string
00323   if (len_ == 0)
00324     return ACE_SString ();
00325 
00326   // case 2. start pos l
00327   if (offset >= len_)
00328     return ACE_SString ();
00329 
00330   // get all remaining bytes
00331   if (length == npos || count > (this->len_ - offset))
00332     count = len_ - offset;
00333 
00334   return ACE_SString (&rep_[offset], count, this->allocator_);
00335 }


Member Data Documentation

ACE_SString::ACE_ALLOC_HOOK_DECLARE

Declare the dynamic allocation hooks.

Definition at line 234 of file SString.h.

ACE_Allocator* ACE_SString::allocator_ [private]

Pointer to a memory allocator.

Definition at line 238 of file SString.h.

Referenced by ACE_SString(), and operator=().

size_type ACE_SString::len_ [private]

Length of the ACE_SString (not counting the trailing '').

Definition at line 241 of file SString.h.

Referenced by ACE_SString(), length(), operator=(), operator==(), rep(), rfind(), and substring().

ACE_SString::size_type const ACE_SString::npos [static]

Initial value:

No position constant.

Definition at line 134 of file SString.h.

Referenced by find(), rfind(), and substring().

char* ACE_SString::rep_ [private]

Pointer to data.

Definition at line 244 of file SString.h.

Referenced by ACE_SString(), c_str(), compare(), fast_rep(), find(), operator<(), operator=(), operator==(), operator>(), operator[](), rep(), strstr(), and substring().


The documentation for this class was generated from the following files:
Generated on Tue Feb 2 17:35:40 2010 for ACE by  doxygen 1.4.7