Class fixed_string

Synopsis

#include <include/EASTL/fixed_string.h>

template <typename T, int nodeCount, bool bEnableOverflow = true, typename OverflowAllocator = EASTLAllocatorType>
class fixed_string : public basic_string<T, fixed_vector_allocator<sizeof(T), nodeCount, EASTL_ALIGN_OF(T), 0, bEnableOverflow, OverflowAllocator> >

Description

fixed_string

A fixed_string with bEnableOverflow == true is identical to a regular string in terms of its behavior. All the expectations of regular string apply to it and no additional expectations come from it. When bEnableOverflow is false, fixed_string behaves like regular string with the exception that its capacity can never increase. All operations you do on such a fixed_string which require a capacity increase will result in undefined behavior or an C++ allocation exception, depending on the configuration of EASTL.

Note: The nodeCount value is the amount of characters to allocate, which needs to take into account a terminating zero. Thus if you want to store strings with a strlen of 30, the nodeCount value must be at least 31.

Template parameters: T The type of object the string holds (char, wchar_t, char8_t, char16_t, char32_t). nodeCount The max number of objects to contain. bEnableOverflow Whether or not we should use the overflow heap if our object pool is exhausted. OverflowAllocator Overflow allocator, which is only used if bEnableOverflow == true. Defaults to the global heap.

Notes: The nodeCount value must be at least 2, one for a character and one for a terminating 0.

As of this writing, the string class necessarily reallocates when an insert of self is done into self. As a result, the fixed_string class doesn't support inserting self into self unless the bEnableOverflow template parameter is true.

Example usage: fixed_string<char, 128 + 1, true> fixedString("hello world"); // Can hold up to a strlen of 128.

fixedString = "hola mundo"; fixedString.clear(); fixedString.resize(200); fixedString.sprintf("%f", 1.5f);

Mentioned in

Inheritance

Ancestors: basic_string

Methods

fixed_string overloadfixed_string
append overload
can_overflow
capacity
clear
DoAllocateHelper functions for initialization/insertion operations.
DoFree
full
get_allocator overloadAllocator.
get_overflow_allocator overloadOverflowAllocator.
has_overflowed
internalLayout overload
left
max_size
operator= overload
reset_lose_memory
resize overload
set_capacity
set_overflow_allocator
size
sprintf_va_list
substrThe inherited versions of substr/left/right call the basic_string constructor, which will call the overflow allocator and fail if bEnableOverflow == false
swap

Source

Lines 73-153 in include/EASTL/fixed_string.h.

template <typename T, int nodeCount, bool bEnableOverflow = true, typename OverflowAllocator = EASTLAllocatorType>
class fixed_string : public basic_string<T, fixed_vector_allocator<sizeof(T), nodeCount, EASTL_ALIGN_OF(T), 0, bEnableOverflow, OverflowAllocator> >
{
public:
    typedef fixed_vector_allocator<sizeof(T), nodeCount, EASTL_ALIGN_OF(T),
                        0, bEnableOverflow, OverflowAllocator>              fixed_allocator_type;
    typedef typename fixed_allocator_type::overflow_allocator_type          overflow_allocator_type;
    typedef basic_string<T, fixed_allocator_type>                           base_type;
    typedef fixed_string<T, nodeCount, bEnableOverflow, OverflowAllocator>  this_type;
    typedef typename base_type::size_type                                   size_type;
    typedef typename base_type::value_type                                  value_type;
    typedef typename base_type::CtorDoNotInitialize                         CtorDoNotInitialize;
    typedef typename base_type::CtorSprintf                                 CtorSprintf;
    typedef aligned_buffer<nodeCount * sizeof(T), EASTL_ALIGN_OF(T)>        aligned_buffer_type;
    enum { kMaxSize = nodeCount - 1 }; // -1 because we need to save one element for the silent terminating null.
    using base_type::npos;
    using base_type::mPair;
    using base_type::append;
    using base_type::resize;
    using base_type::clear;
    using base_type::capacity;
    using base_type::size;
    using base_type::sprintf_va_list;
    using base_type::DoAllocate;
    using base_type::DoFree;
    using base_type::internalLayout;
    using base_type::get_allocator;
protected:
    union // We define a union in order to avoid strict pointer aliasing issues with compilers like GCC.
    {
        value_type          mArray[1];
        aligned_buffer_type mBuffer;     // Question: Why are we doing this aligned_buffer thing? Why not just do an array of value_type, given that we are using just strings of char types.
    };
public:
    fixed_string();
    explicit fixed_string(const overflow_allocator_type& overflowAllocator);             // Only applicable if bEnableOverflow is true.
    fixed_string(const base_type& x, size_type position, size_type n = base_type::npos); // Currently we don't support overflowAllocator specification for other constructors, for simplicity.
    fixed_string(const value_type* p, size_type n);
    fixed_string(const value_type* p);
    fixed_string(size_type n, const value_type& value);
    fixed_string(const this_type& x);
    fixed_string(const this_type& x, const overflow_allocator_type& overflowAllocator);
    fixed_string(const base_type& x);
    fixed_string(const value_type* pBegin, const value_type* pEnd);
    fixed_string(CtorDoNotInitialize, size_type n);
    fixed_string(CtorSprintf, const value_type* pFormat, ...);
    fixed_string(std::initializer_list<T> ilist, const overflow_allocator_type& overflowAllocator);
    fixed_string(this_type&& x);
    fixed_string(this_type&& x, const overflow_allocator_type& overflowAllocator);
    this_type& operator=(const this_type& x);
    this_type& operator=(const base_type& x);
    this_type& operator=(const value_type* p);
    this_type& operator=(const value_type c);
    this_type& operator=(std::initializer_list<T> ilist);
    this_type& operator=(this_type&& x);
    void swap(this_type& x);
    void      set_capacity(size_type n);
    void      reset_lose_memory();          // This is a unilateral reset to an initially empty state. No destructors are called, no deallocation occurs.
    size_type max_size() const;
    bool      full() const;                 // Returns true if the fixed space has been fully allocated. Note that if overflow is enabled, the container size can be greater than nodeCount but full() could return true because the fixed space may have a recently freed slot.
    bool      has_overflowed() const;       // Returns true if the allocations spilled over into the overflow allocator. Meaningful only if overflow is enabled.
    bool      can_overflow() const;         // Returns the value of the bEnableOverflow template parameter.
    // The inherited versions of substr/left/right call the basic_string constructor,
    // which will call the overflow allocator and fail if bEnableOverflow == false
    this_type substr(size_type position, size_type n) const;
    this_type left(size_type n) const;
    this_type right(size_type n) const;
    // OverflowAllocator
    const overflow_allocator_type& get_overflow_allocator() const EA_NOEXCEPT;
    overflow_allocator_type&       get_overflow_allocator() EA_NOEXCEPT;
    void                           set_overflow_allocator(const overflow_allocator_type& allocator);
}; // fixed_string





Add Discussion as Guest

Log in