-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate value provider (WIP) implementation from current code
- Loading branch information
Showing
3 changed files
with
120 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
#pragma once | ||
|
||
#ifndef USE_BASS_VALUEPROVIDER | ||
#define USE_BASS_VALUEPROVIDER | ||
#endif | ||
|
||
#include "defines.h" | ||
|
||
#include <map> | ||
#include <optional> | ||
|
||
|
||
template <typename T> | ||
class ValueProvider { | ||
protected: | ||
std::any val; // TODO: change to std::optional<T> | ||
|
||
public: | ||
const std::string_view __owner_classname__; | ||
|
||
explicit ValueProvider(std::string const& owner_classname) | ||
: __owner_classname__(owner_classname) | ||
{} | ||
explicit ValueProvider(T const& v, std::string const& owner_classname) | ||
: val(v) | ||
, __owner_classname__(persist(owner_classname)) | ||
{} | ||
virtual ~ValueProvider() = default; | ||
|
||
inline bool hasValue() const { return val.has_value(); } | ||
inline constexpr auto type() const { | ||
// NOTE: Contrary to std::any the typeid is always known. | ||
return typeid(T); | ||
} | ||
|
||
inline T getValue() const { | ||
if (val.has_value()) { | ||
return std::any_cast<T>(val); | ||
} else { | ||
throw std::runtime_error(fmt::format("{} holds a 'None' value.", __owner_classname__)); | ||
} | ||
} | ||
|
||
inline void setValue(T const& newVal) { | ||
if (val.has_value() && std::any_cast<T>(val) != newVal) { | ||
fmt::print("Changing {} value from {} to {}\n", __owner_classname__, std::any_cast<T>(val), newVal); | ||
// throw? | ||
} | ||
val = newVal; | ||
} | ||
}; | ||
|
||
template <typename T = AsmValue> | ||
class MultiValueProvider { | ||
protected: | ||
std::map<std::string_view, std::optional<T>> vals; | ||
|
||
public: | ||
const std::string_view __owner_classname__; | ||
|
||
explicit MultiValueProvider(std::string const& owner_classname) | ||
: __owner_classname__(persist(owner_classname)) | ||
{} | ||
virtual ~MultiValueProvider() = default; | ||
|
||
inline bool hasValue(std::string_view key) { | ||
return vals[key].has_value(); | ||
} | ||
|
||
inline bool hasAllValues() const { | ||
for (auto const& val : vals) { | ||
if (!val.has_value()) { | ||
return false; | ||
} | ||
if ((*val).valueless_by_exception()) { | ||
return false; | ||
} | ||
} | ||
|
||
// all values are defined | ||
return true; | ||
} | ||
|
||
inline void setValue(std::string_view key, T const& val) { | ||
persist(key); | ||
vals[key] = val; | ||
} | ||
|
||
inline T& getValue(std::string_view key) { | ||
return *vals[key]; | ||
} | ||
|
||
inline std::optional<T>& getOptional(std::string_view key) { | ||
return vals[key]; | ||
} | ||
}; |