Skip to content
Closed
Show file tree
Hide file tree
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
7 changes: 5 additions & 2 deletions src/shogun/base/SGObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,9 @@ CSGObject* CSGObject::get(const std::string& name, index_t index) const
CSGObject* CSGObject::get(std::string_view name, index_t index) const
#endif
{
auto* result = sgo_details::get_by_tag(this, name, sgo_details::GetByNameIndex(index));
//auto* result = sgo_details::get_by_tag(this, name, sgo_details::GetByNameIndex(index));
auto* result = sgo_details::get_by_tag(this, name);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the problem here that imo this ignores the index parameter. so the visitor that you've written only works in the expected manner for CSGObject::get(std::string_view name, std::nothrow_t). i need to see how we support get CSGObject* CSGObject::get(std::string_view name, index_t index) const. that's basically when the registered parameter is an array


if (!result && has(name))
{
error(
Expand All @@ -818,7 +820,8 @@ CSGObject* CSGObject::get(std::string_view name, index_t index) const
CSGObject* CSGObject::get(std::string_view name, std::nothrow_t) const
noexcept
{
return sgo_details::get_by_tag(this, name, sgo_details::GetByName());
//return sgo_details::get_by_tag(this, name, sgo_details::GetByName());
return sgo_details::get_by_tag(name);
}
#endif

Expand Down
60 changes: 37 additions & 23 deletions src/shogun/base/SGObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ bool dispatch_array_type(const CSGObject* obj, std::string_view name,
#endif // DOXYGEN_SHOULD_SKIP_THIS
#endif // SWIG

struct InterfaceVisitor
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this might be better to place into a separate .h file and include it here. place it under src/shogun/util/visitors

{
CSGObject* value;
};

template <class T, class K> class CMap;

struct TParameter;
Expand Down Expand Up @@ -857,6 +862,13 @@ class CSGObject
{
BaseTag tag(name);
create_parameter(tag, AnyParameter(make_any_ref(value), properties));

if constexpr (is_sg_base<T>::value) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

imo if you wrap this whole block into a constexpr function then you could just call that instead of copy pasting things 4 times.

Any::register_visitor<T, InterfaceVisitor>([] (auto n_value, auto visitor) {
visitor->value = n_value;
});
}

}

/** Puts a pointer to some parameter into the parameter map.
Expand All @@ -882,6 +894,12 @@ class CSGObject
tag,
AnyParameter(
make_any_ref(value), properties, std::move(auto_init)));

if constexpr (is_sg_base<T>::value) {
Any::register_visitor<T, InterfaceVisitor>([] (auto n_value, auto visitor) {
visitor->value = n_value;
});
}
}

#ifndef SWIG
Expand Down Expand Up @@ -915,6 +933,11 @@ class CSGObject
constrain_function.run(casted_val, result);
return result;
}));
if constexpr (is_sg_base<T1>::value) {
Any::register_visitor<T1, InterfaceVisitor>([] (auto n_value, auto visitor) {
visitor->value = n_value;
});
}
}
#endif

Expand Down Expand Up @@ -954,6 +977,12 @@ class CSGObject
BaseTag tag(name);
create_parameter(
tag, AnyParameter(make_any_ref(value, rows, cols), properties));

if constexpr (is_sg_base<T>::value) {
Any::register_visitor<T, InterfaceVisitor>([] (auto n_value, auto visitor) {
visitor->value = n_value;
});
}
}

#ifndef SWIG
Expand Down Expand Up @@ -1344,30 +1373,15 @@ CSGObject* get_if_possible(const CSGObject* obj, std::string_view name, GetByNam
return result;
}

template<typename T>
CSGObject* get_dispatch_all_base_types(const CSGObject* obj, std::string_view name,
T&& how)
{
if (auto* result = get_if_possible<CKernel>(obj, name, how))
return result;
if (auto* result = get_if_possible<CFeatures>(obj, name, how))
return result;
if (auto* result = get_if_possible<CMachine>(obj, name, how))
return result;
if (auto* result = get_if_possible<CLabels>(obj, name, how))
return result;
if (auto* result = get_if_possible<CEvaluationResult>(obj, name, how))
return result;

return nullptr;
}

template<class T>
CSGObject* get_by_tag(const CSGObject* obj, std::string_view name,
T&& how)
{
return get_dispatch_all_base_types(obj, name, how);
}
CSGObject* get_by_tag(const CSGObject* obj, std::string_view name) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this whole thing could be deleted and go into a private function that is implemented in .cpp

BaseTag tag(name);
auto param = obj->get_parameter(tag);
InterfaceVisitor iv;
param.get_value().visit_with(&iv);
if (iv.value)
return iv.value;
return nullptr;
}

#endif //DOXYGEN_SHOULD_SKIP_THIS
Expand Down