Skip to content

Redesign API using policies #13

@ghost

Description

I think a policy class would be great to allow users to customize how the optional determines if the value is existing. This would allow users to customize this behavior while still using the same unified interface. This is similar to how allocators work in the stl. If its set to void, it can use std::optional behind the scenes instead. could do this by default.

template<typename TYPE, TYPE VALUE>
struct ValueSentinel final
{
   bool has_value(const TYPE& value)
   {
      return value != VALUE;
   }
};

template<typename TYPE, TYPE VALUE>
struct GreaterThanSentinel final
{
   bool has_value(const TYPE& value)
   {
      return value > VALUE;
   }
};

template<typename TYPE, typename SENTINEL = void>
Markable;

template<typename TYPE, typename SENTINEL>
Markable<TYPE, SENTINEL>
{
   // with [[no_unique_address]] the compiler is smart enough to optimize sentinel away if its an empty struct.
    [[no_unique_address]] SENTINEL _sentinel;
    TYPE _value;
    
    bool has_value()
    {
        return _sentinel.has_value(this->_value);
    }
};

// specialization for no sentinel
template<typename TYPE>
Markable<TYPE, void>
{
    std::optional<TYPE> value_o = std::nullopt;
    
    bool has_value()
    {
        value_o.has_value();
    }
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions