- Main Page
- Namespaces
- Classes
- Files

# D:/projects/ACE/proto/Source/strings.cpp

Go to the documentation of this file.

    00001
    00011 #include "strings.h"
    00012
    00013 namespace ace {
    00014
    00015 const char StringStore::_empty = NULL_CHAR;
    00016
    00017 /*  Interface item implementation - for info see description in header file.
    00018  */
    00019 const char * StringStore::store(const char *str) {
    00020
    00021     static strings_store_t::key_compare str_comp;
    00022
    00023     if ( *str == _empty ) {
    00024         // Empty string is empty string :)
    00025         return &_empty;
    00026     }
    00027
    00028     // Search for given string using lower_bound, because we want to know position,
    00029     // where the string eventually would get inserted.
    00030     strings_store_t::iterator i_str = _strings.lower_bound(str);
    00031     if ( i_str != _strings.end() ) {
    00032         // Compare (pointers to) strings using string_store_t comparator.
    00033         if ( !str_comp(*i_str, str) && !str_comp(str, *i_str) ) {
    00034             // Already exists:
    00035             return *i_str;
    00036         }
    00037     }
    00038
    00039     // New object - new memory needed...
    00040     size_t memory_needed = strlen(str) + 1;
    00041     char* mem = _memory_pool.get_memory(memory_needed); // This may throw!
    00042     // The code above may throw safely... :)
    00043
    00044     // Copy passed string into brand new memory:
    00045     memcpy(mem, str, memory_needed);
    00046     // Insert and return dereferenced iterator.
    00047     return *_strings.insert(i_str, mem);
    00048 }
    00049
    00050 } // namespace ace