libfoedus-core
FOEDUS Core Library
foedus::assorted::FixedString< MAXLEN, CHAR > Class Template Reference

An embedded string object of fixed max-length, which uses no external memory. More...

Detailed Description

template<uint MAXLEN, typename CHAR = char>
class foedus::assorted::FixedString< MAXLEN, CHAR >

An embedded string object of fixed max-length, which uses no external memory.

This header-only object behaves like std::string in many ways. The key difference is that this object is essentially a fixed array while std::string allocates memory in heap (with some exception for optimized stack-allocation, but that's not the point).

This implies a few crucial characteristics.

  • We can copy/overwrite a piece of memory containing this object without taking care of heap-allocated memory (which makes the handling of shared memory much easier).
  • We have limit on the length of the string, determined at compile time (a template param).
  • We always consume that much memory regardless the actual content.

Usecases

As mentioned above, this object is used where we can't use std::string that points to somewhere else. For example, use it as follows.

struct MyPage {
FixedString<12> str1_; // +4+12
FixedString<8> str2_; // +4+8+4 (Anyway 8 byte aligned..)
char other_data_[4096-32];
};
// This page can be simply memcpy-ed unlike a struct that contains std::string.

Limitations

No char traits for exotic comparison rules. Not more than 2^32-1 chars. Length 2^32-1 is reserved for npos.

Definition at line 65 of file fixed_string.hpp.

#include <fixed_string.hpp>

Public Member Functions

 FixedString () noexcept
 Constructs an empty string. More...
 
template<uint MAXLEN2>
 FixedString (const FixedString< MAXLEN2, CHAR > &other) noexcept
 Copy constructor for all FixedString objects. More...
 
 FixedString (const CHAR *str, uint32_t len) noexcept
 Copy constructor for char* and len. More...
 
 FixedString (const CHAR *str) noexcept
 Copy constructor for null-terminated char*. More...
 
template<uint MAXLEN2>
FixedStringoperator= (const FixedString< MAXLEN2, CHAR > &other) noexcept
 Assign operator for all FixedString objects. More...
 
template<uint MAXLEN2>
bool operator== (const FixedString< MAXLEN2, CHAR > &other) const noexcept
 
template<uint MAXLEN2>
bool operator!= (const FixedString< MAXLEN2, CHAR > &other) const noexcept
 
template<uint MAXLEN2>
bool operator< (const FixedString< MAXLEN2, CHAR > &other) const noexcept
 
template<uint MAXLEN2>
void assign (const FixedString< MAXLEN2, CHAR > &other) noexcept
 Assign operator for all FixedString objects. More...
 
void assign (const std::basic_string< CHAR > &str) noexcept
 Assign operator for std::string. More...
 
void assign (const CHAR *str, uint32_t len) noexcept
 Assign operator for char* and length. More...
 
template<uint MAXLEN2>
void append (const FixedString< MAXLEN2, CHAR > &other) noexcept
 Append operator for all FixedString objects. More...
 
void append (const std::basic_string< CHAR > &str) noexcept
 Append operator for std::string. More...
 
void append (const CHAR *str, uint32_t len) noexcept
 Append operator for char* and length. More...
 
uint32_t length () const noexcept
 Returns the length of this string. More...
 
uint32_t size () const noexcept
 Returns the length of this string. More...
 
uint32_t capacity () const noexcept
 Return size of allocated storage. More...
 
uint32_t max_size () const noexcept
 Return maximum size of string. More...
 
void clear () noexcept
 Clear string. More...
 
bool empty () const noexcept
 Test if string is empty. More...
 
void zero_fill_remaining () const noexcept
 Sets zeros to unused data_ region. More...
 
const CHAR * data () const noexcept
 Get string data. More...
 
std::basic_string< CHAR > str () const
 Convert to a std::string object. More...
 
const CHAR * c_str () const
 Convert to a C string. More...
 

Static Public Member Functions

static uint32_t strlen (const char *str) noexcept
 
static uint32_t strlen (const wchar_t *str) noexcept
 

Static Public Attributes

static const uint32_t npos = -1
 npos is a static member constant value with the greatest possible value for uint32_t. More...
 

Friends

std::ostream & operator<< (std::ostream &o, const FixedString &v)
 

Constructor & Destructor Documentation

template<uint MAXLEN, typename CHAR = char>
foedus::assorted::FixedString< MAXLEN, CHAR >::FixedString ( )
inlinenoexcept

Constructs an empty string.

Definition at line 68 of file fixed_string.hpp.

68 : length_(0) {}
template<uint MAXLEN, typename CHAR = char>
template<uint MAXLEN2>
foedus::assorted::FixedString< MAXLEN, CHAR >::FixedString ( const FixedString< MAXLEN2, CHAR > &  other)
inlineexplicitnoexcept

Copy constructor for all FixedString objects.

Note that too-long strings are truncated.

Definition at line 72 of file fixed_string.hpp.

72 { assign(other); }
void assign(const FixedString< MAXLEN2, CHAR > &other) noexcept
Assign operator for all FixedString objects.
template<uint MAXLEN, typename CHAR = char>
foedus::assorted::FixedString< MAXLEN, CHAR >::FixedString ( const CHAR *  str,
uint32_t  len 
)
inlinenoexcept

Copy constructor for char* and len.

Note that too-long strings are truncated.

Definition at line 75 of file fixed_string.hpp.

75 { assign(str, len); }
void assign(const FixedString< MAXLEN2, CHAR > &other) noexcept
Assign operator for all FixedString objects.
std::basic_string< CHAR > str() const
Convert to a std::string object.
template<uint MAXLEN, typename CHAR = char>
foedus::assorted::FixedString< MAXLEN, CHAR >::FixedString ( const CHAR *  str)
inlinenoexcept

Copy constructor for null-terminated char*.

Note that too-long strings are truncated.

Definition at line 78 of file fixed_string.hpp.

78  { // NOLINT(runtime/explicit) follows std::string
79  assign(str, strlen(str));
80  }
static uint32_t strlen(const char *str) noexcept
void assign(const FixedString< MAXLEN2, CHAR > &other) noexcept
Assign operator for all FixedString objects.
std::basic_string< CHAR > str() const
Convert to a std::string object.

Member Function Documentation

template<uint MAXLEN, typename CHAR = char>
template<uint MAXLEN2>
void foedus::assorted::FixedString< MAXLEN, CHAR >::append ( const FixedString< MAXLEN2, CHAR > &  other)
inlinenoexcept

Append operator for all FixedString objects.

Note that too-long strings are truncated.

Definition at line 138 of file fixed_string.hpp.

138  {
139  ASSERT_ND(other.length() <= MAXLEN2);
140  uint32_t len = length_ + other.length() > MAXLEN ? MAXLEN - length_ : other.length();
141  std::memcpy(data_ + length_, other.data(), len * sizeof(CHAR));
142  length_ += len;
143  }
#define ASSERT_ND(x)
A warning-free wrapper macro of assert() that has no performance effect in release mode even when 'x'...
Definition: assert_nd.hpp:72
template<uint MAXLEN, typename CHAR = char>
void foedus::assorted::FixedString< MAXLEN, CHAR >::append ( const std::basic_string< CHAR > &  str)
inlinenoexcept

Append operator for std::string.

Note that too-long strings are truncated.

Definition at line 146 of file fixed_string.hpp.

146  {
147  uint32_t len = length_ + str.size() > MAXLEN ? MAXLEN - length_ : str.size();
148  std::memcpy(data_ + length_, str.data(), len * sizeof(CHAR));
149  length_ += len;
150  }
template<uint MAXLEN, typename CHAR = char>
void foedus::assorted::FixedString< MAXLEN, CHAR >::append ( const CHAR *  str,
uint32_t  len 
)
inlinenoexcept

Append operator for char* and length.

Note that too-long strings are truncated.

Definition at line 153 of file fixed_string.hpp.

153  {
154  len = length_ + len > MAXLEN ? MAXLEN - length_ : len;
155  std::memcpy(data_ + length_, str, len * sizeof(CHAR));
156  length_ += len;
157  }
std::basic_string< CHAR > str() const
Convert to a std::string object.
template<uint MAXLEN, typename CHAR = char>
template<uint MAXLEN2>
void foedus::assorted::FixedString< MAXLEN, CHAR >::assign ( const FixedString< MAXLEN2, CHAR > &  other)
inlinenoexcept

Assign operator for all FixedString objects.

Note that too-long strings are truncated.

Definition at line 119 of file fixed_string.hpp.

Referenced by foedus::assorted::FixedString< 60 >::FixedString(), foedus::externalize::Externalizable::get_element(), foedus::FixedErrorStack::operator=(), foedus::assorted::FixedString< 60 >::operator=(), and foedus::memory::PagePoolPimpl::set_debug_pool_name().

119  {
120  ASSERT_ND(other.length() <= MAXLEN2);
121  length_ = other.length() > MAXLEN ? MAXLEN : other.length();
122  std::memcpy(data_, other.data(), length_ * sizeof(CHAR));
123  }
#define ASSERT_ND(x)
A warning-free wrapper macro of assert() that has no performance effect in release mode even when 'x'...
Definition: assert_nd.hpp:72

Here is the caller graph for this function:

template<uint MAXLEN, typename CHAR = char>
void foedus::assorted::FixedString< MAXLEN, CHAR >::assign ( const std::basic_string< CHAR > &  str)
inlinenoexcept

Assign operator for std::string.

Note that too-long strings are truncated.

Definition at line 125 of file fixed_string.hpp.

125  {
126  length_ = str.size() > MAXLEN ? MAXLEN : str.size();
127  std::memcpy(data_, str.data(), length_ * sizeof(CHAR));
128  }
template<uint MAXLEN, typename CHAR = char>
void foedus::assorted::FixedString< MAXLEN, CHAR >::assign ( const CHAR *  str,
uint32_t  len 
)
inlinenoexcept

Assign operator for char* and length.

Note that too-long strings are truncated.

Definition at line 131 of file fixed_string.hpp.

131  {
132  length_ = len > MAXLEN ? MAXLEN : len;
133  std::memcpy(data_, str, length_ * sizeof(CHAR));
134  }
std::basic_string< CHAR > str() const
Convert to a std::string object.
template<uint MAXLEN, typename CHAR = char>
const CHAR* foedus::assorted::FixedString< MAXLEN, CHAR >::c_str ( ) const
inline

Convert to a C string.

Definition at line 189 of file fixed_string.hpp.

Referenced by foedus::proc::ProcManagerPimpl::get_proc().

189 { return str().c_str(); }
std::basic_string< CHAR > str() const
Convert to a std::string object.

Here is the caller graph for this function:

template<uint MAXLEN, typename CHAR = char>
uint32_t foedus::assorted::FixedString< MAXLEN, CHAR >::capacity ( ) const
inlinenoexcept

Return size of allocated storage.

Actually a constexpr.

Definition at line 166 of file fixed_string.hpp.

166 { return MAXLEN; }
template<uint MAXLEN, typename CHAR = char>
void foedus::assorted::FixedString< MAXLEN, CHAR >::clear ( )
inlinenoexcept
template<uint MAXLEN, typename CHAR = char>
const CHAR* foedus::assorted::FixedString< MAXLEN, CHAR >::data ( ) const
inlinenoexcept

Get string data.

Definition at line 183 of file fixed_string.hpp.

183 { return data_; }
template<uint MAXLEN, typename CHAR = char>
uint32_t foedus::assorted::FixedString< MAXLEN, CHAR >::length ( ) const
inlinenoexcept

Returns the length of this string.

Definition at line 162 of file fixed_string.hpp.

162 { return length_; }
template<uint MAXLEN, typename CHAR = char>
uint32_t foedus::assorted::FixedString< MAXLEN, CHAR >::max_size ( ) const
inlinenoexcept

Return maximum size of string.

Actually a constexpr.

Definition at line 168 of file fixed_string.hpp.

168 { return MAXLEN; }
template<uint MAXLEN, typename CHAR = char>
template<uint MAXLEN2>
bool foedus::assorted::FixedString< MAXLEN, CHAR >::operator!= ( const FixedString< MAXLEN2, CHAR > &  other) const
inlinenoexcept

Definition at line 101 of file fixed_string.hpp.

101  {
102  return !operator==(other);
103  }
bool operator==(const FixedString< MAXLEN2, CHAR > &other) const noexcept
template<uint MAXLEN, typename CHAR = char>
template<uint MAXLEN2>
bool foedus::assorted::FixedString< MAXLEN, CHAR >::operator< ( const FixedString< MAXLEN2, CHAR > &  other) const
inlinenoexcept

Definition at line 105 of file fixed_string.hpp.

105  {
106  uint32_t min_len = std::min<uint32_t>(length_, other.length());
107  if (min_len == 0) {
108  return length_ < other.length();
109  }
110  int result = std::memcmp(data_, other.data(), min_len * sizeof(CHAR));
111  if (result != 0) {
112  return result < 0;
113  }
114  return length_ < other.length();
115  }
template<uint MAXLEN, typename CHAR = char>
template<uint MAXLEN2>
FixedString& foedus::assorted::FixedString< MAXLEN, CHAR >::operator= ( const FixedString< MAXLEN2, CHAR > &  other)
inlinenoexcept

Assign operator for all FixedString objects.

Note that too-long strings are truncated.

Definition at line 87 of file fixed_string.hpp.

87  {
88  assign(other);
89  return *this;
90  }
void assign(const FixedString< MAXLEN2, CHAR > &other) noexcept
Assign operator for all FixedString objects.
template<uint MAXLEN, typename CHAR = char>
template<uint MAXLEN2>
bool foedus::assorted::FixedString< MAXLEN, CHAR >::operator== ( const FixedString< MAXLEN2, CHAR > &  other) const
inlinenoexcept

Definition at line 93 of file fixed_string.hpp.

Referenced by foedus::assorted::FixedString< 60 >::operator!=().

93  {
94  if (length_ == 0) {
95  return other.length() == 0;
96  }
97  return length_ == other.length() &&
98  std::memcmp(data_, other.data(), length_ * sizeof(CHAR)) == 0;
99  }

Here is the caller graph for this function:

template<uint MAXLEN, typename CHAR = char>
uint32_t foedus::assorted::FixedString< MAXLEN, CHAR >::size ( ) const
inlinenoexcept

Returns the length of this string.

Definition at line 164 of file fixed_string.hpp.

164 { return length_; }
template<uint MAXLEN, typename CHAR = char>
static uint32_t foedus::assorted::FixedString< MAXLEN, CHAR >::strlen ( const char *  str)
inlinestaticnoexcept

Definition at line 82 of file fixed_string.hpp.

Referenced by foedus::assorted::FixedString< 60 >::FixedString().

82 { return std::strlen(str); }
std::basic_string< CHAR > str() const
Convert to a std::string object.

Here is the caller graph for this function:

template<uint MAXLEN, typename CHAR = char>
static uint32_t foedus::assorted::FixedString< MAXLEN, CHAR >::strlen ( const wchar_t *  str)
inlinestaticnoexcept

Definition at line 83 of file fixed_string.hpp.

83 { return std::wcslen(str); }
std::basic_string< CHAR > str() const
Convert to a std::string object.
template<uint MAXLEN, typename CHAR = char>
void foedus::assorted::FixedString< MAXLEN, CHAR >::zero_fill_remaining ( ) const
inlinenoexcept

Sets zeros to unused data_ region.

This is just to make valgrind happy.

Definition at line 174 of file fixed_string.hpp.

Referenced by foedus::storage::StorageManagerPimpl::create_storage().

174  {
175  if (length_ < MAXLEN) {
176  // this is not logically changing the content, so const is the right semantics.
177  std::memset(const_cast<char*>(data_ + length_), 0, MAXLEN - length_);
178  }
179  }

Here is the caller graph for this function:

Friends And Related Function Documentation

template<uint MAXLEN, typename CHAR = char>
std::ostream& operator<< ( std::ostream &  o,
const FixedString< MAXLEN, CHAR > &  v 
)
friend

Definition at line 200 of file fixed_string.hpp.

200  {
201  o << v.str();
202  return o;
203  }

Member Data Documentation

template<uint MAXLEN, typename CHAR = char>
const uint32_t foedus::assorted::FixedString< MAXLEN, CHAR >::npos = -1
static

npos is a static member constant value with the greatest possible value for uint32_t.

This value, when used as the value for a len (or sublen) parameter in this object, means "until the end of the string". As a return value, it is usually used to indicate no matches. This constant is defined with a value of -1, which because uint32_t is an unsigned integral type, it is the largest possible representable value for this type.

Definition at line 198 of file fixed_string.hpp.


The documentation for this class was generated from the following file: