Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redesign API using policies #13

Open
Journeyman1337 opened this issue Jun 12, 2024 · 0 comments
Open

Redesign API using policies #13

Journeyman1337 opened this issue Jun 12, 2024 · 0 comments

Comments

@Journeyman1337
Copy link

Journeyman1337 commented Jun 12, 2024

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();
    }
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant