Skip to content

Commit

Permalink
Use constexpr where possible for method return values
Browse files Browse the repository at this point in the history
  • Loading branch information
mikee47 committed Apr 7, 2024
1 parent cdff515 commit 3ee3da9
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 18 deletions.
11 changes: 0 additions & 11 deletions src/ObjectBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,6 @@ size_t ObjectBase::read(size_t offset, void* buffer, size_t count) const
return count;
}

size_t ObjectBase::length() const
{
if(isNull()) {
return 0;
} else if(isCopy()) {
return reinterpret_cast<const ObjectBase*>(flashLength_ & ~copyBit)->length();
} else {
return flashLength_;
}
}

const uint8_t* ObjectBase::data() const
{
if(isNull()) {
Expand Down
6 changes: 3 additions & 3 deletions src/include/FlashString/Object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,15 @@ template <class ObjectType, typename ElementType> class Object : public ObjectBa
/**
* @brief Return an empty object which evaluates to null
*/
static const ObjectType& empty()
static constexpr const ObjectType& empty()
{
return empty_.as<ObjectType>();
return empty_.as<const ObjectType>();
}

/**
* @brief Get the length of the content in elements
*/
FSTR_INLINE size_t length() const
FSTR_INLINE constexpr const size_t length() const
{
return ObjectBase::length() / sizeof(ElementType);
}
Expand Down
17 changes: 13 additions & 4 deletions src/include/FlashString/ObjectBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,22 @@ class ObjectBase
/**
* @brief Get the length of the object data in bytes
*/
size_t length() const;
FSTR_NOINLINE constexpr const size_t length() const
{
if(isNull()) {
return 0;
}
if(isCopy()) {
return reinterpret_cast<const ObjectBase*>(flashLength_ & ~copyBit)->length();
}
return flashLength_;
}

/**
* @brief Get the object data size in bytes
* @note Always an integer multiple of 4 bytes
*/
FSTR_INLINE size_t size() const
FSTR_INLINE constexpr const size_t size() const
{
return ALIGNUP4(length());
}
Expand Down Expand Up @@ -89,7 +98,7 @@ class ObjectBase
*/
size_t readFlash(size_t offset, void* buffer, size_t count) const;

FSTR_INLINE bool isCopy() const
FSTR_INLINE constexpr const bool isCopy() const
{
return (flashLength_ & copyBit) != 0;
}
Expand All @@ -98,7 +107,7 @@ class ObjectBase
* @brief Indicates an invalid String, used for return value from lookups, etc.
* @note A real String can be zero-length, but it cannot be null
*/
FSTR_INLINE bool isNull() const
FSTR_INLINE constexpr const bool isNull() const
{
return flashLength_ == lengthInvalid;
}
Expand Down
1 change: 1 addition & 0 deletions src/include/FlashString/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <stringutil.h>

#define FSTR_INLINE __attribute__((always_inline)) inline
#define FSTR_NOINLINE __attribute__((noinline))
#define FSTR_ALIGNED __attribute__((aligned(4)))
#define FSTR_PACKED __attribute__((packed))

Expand Down

0 comments on commit 3ee3da9

Please sign in to comment.