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

TypedVarHost::Has #296

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 27 additions & 15 deletions omnn/math/VarHost.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,34 +35,34 @@ namespace math {

public:
using ptr = std::shared_ptr<VarHost>;
using cptr = std::shared_ptr<const VarHost>;
using cptr = std::shared_ptr<const VarHost>;
using cref = const VarHost&;
using hosted_storage_t = std::pair<Variable, std::string>;

virtual ~VarHost() = default;

template<class T>
Variable New(const T& id) {
#ifndef NDEBUG
#ifndef NDEBUG
auto host = dynamic_cast<TypedVarHost<T>*>(this);
if (host != this) {
LOG_AND_IMPLEMENT("wrong id type");
}
#endif
if (host != this) {
LOG_AND_IMPLEMENT("wrong id type");
}
#endif
Variable v(sh());
v.SetId(id);
return v;
}

Variable New();
Variable New();

protected:
VarHost() = default;

const any::any& GetId(const Variable&) const;

Variable New(const any::any& id);
Variable New(const any::any& id);

virtual void AddNewId(const void* id) {
IMPLEMENT;
}
Expand Down Expand Up @@ -176,16 +176,27 @@ namespace math {
}

bool Has(const any::any& id) const override {
IMPLEMENT
return varIds.find(any::any_cast<T>(id)) != varIds.end();
try {
return varIds.find(any::any_cast<T>(id)) != varIds.end();
} catch (const any::bad_any_cast&) {
// Handle the error, e.g., log it or return a default value
// For now, we'll return false to indicate the id was not found
return false;
}
}

size_t Hash(const any::any& id) const override {
return std::hash<T>()(any::any_cast<T>(id));
}

bool CompareIdsLess(const any::any& a, const any::any& b) const override {
return any::any_cast<T>(a) < any::any_cast<T>(b);
try {
return any::any_cast<T>(a) < any::any_cast<T>(b);
} catch (const any::bad_any_cast&) {
// Handle the error, e.g., log it or return a default value
// For now, we'll return false to indicate the comparison failed
return false;
}
}

bool CompareIdsEqual(const any::any& a, const any::any& b) const override {
Expand Down Expand Up @@ -215,15 +226,16 @@ namespace math {
it = hosted.emplace(id, hosted_storage_t{New(id), ""s}).first;
}
} else {
IMPLEMENT
}
// Handle unsupported string types
throw std::runtime_error("Unsupported string type for id");
}
}
}
if (it == hosted.end()) {
const T& idT = *idTp;
it = hosted.find(idT);
if (it == hosted.end()) {
it = hosted.emplace(idT, hosted_storage_t{New(id), ""s}).first;
it = hosted.emplace(idT, hosted_storage_t{New(id), ""s}).first;
}
}

Expand Down
Loading