AceCommon  1.6.1
Arduino library for low-level common functions and features with no external dependencies
Public Member Functions | List of all members
ace_common::FlashString Class Reference

A thin wrapper around a (const __FlashStringHelper*) so that it acts exactly like a (const char*) with regards to operator*(), operator++() and other common operations on (const char*). More...

#include <FlashString.h>

Public Member Functions

 FlashString (const __FlashStringHelper *p)
 Constructor, with implicit conversion.
 
 FlashString (const FlashString &)=default
 Default copy constructor.
 
FlashStringoperator= (const FlashString &)=default
 Default assignment operator.
 
char operator* () const
 Dereference operator.
 
char operator[] (size_t i) const
 Array dereference operator.
 
 operator const void * () const
 Implicit cast to const void* to support operator==() and operator!=(). More...
 
 operator const __FlashStringHelper * () const
 Retrieve the original pointer with an explicit cast.
 
FlashString operator++ ()
 Pre-increment.
 
FlashString operator++ (int)
 Post-increment.
 
FlashString operator-- ()
 Pre-decrement.
 
FlashString operator-- (int)
 Post-decrement.
 

Detailed Description

A thin wrapper around a (const __FlashStringHelper*) so that it acts exactly like a (const char*) with regards to operator*(), operator++() and other common operations on (const char*).

This is intended to allow template functions to be written to avoid the need to write almost duplicate code twice, one for (const char*) and another for (const __FlashStringHelper*) with the dereferencing replaced by a pgm_read_byte() function.

Instead, write the templatized code once for a generic T type that acts like a const char*. That template will be used directly when called with an actual const char*. Then create a template specialization for (const __FlashStringHelper*) whose implementation just wraps the const __FlashStringHelper* pointer inside a FlashString, then forwards the call to the templatized function with a FlashString instead.

Definition at line 49 of file FlashString.h.

Member Function Documentation

◆ operator const void *()

ace_common::FlashString::operator const void * ( ) const
inline

Implicit cast to const void* to support operator==() and operator!=().

I tried using an implicit operator const __FlashStringHelper*() const here. But when I try use operator[] on the object, some of the C++ compilers (not AVR) produce a warning that says: "ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second". (Other compilers don't produce this error.) As far as I can tell, the compiler thinks that instead of just calling the operator[] that I provide above, it can do an implicit conversion to const __FlashStringHelper*, then call the built-in operator[] on that pointer, and it thinks that the 2 paths are equally valid. This is above my understanding of C++ implicit conversion rules.

My workaround is to create this implicit cast operator to const void*, so that I can get operator==() and operator!=() to work as expected. Then create an explicit operator const __FlashStringHelper*() so that I can convert a FlashString back to its original pointer when I need to.

Definition at line 87 of file FlashString.h.


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