Skip to content

Commit b2bddd5

Browse files
fix constraint call
1 parent b7a64f2 commit b2bddd5

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

include/cddp-cpp/cddp_core/cddp_core.hpp

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,17 +135,29 @@ class CDDP {
135135

136136
// Get a specific constraint by name
137137
template <typename T>
138-
T& getConstraint(const std::string& name) {
138+
T* getConstraint(const std::string& name) {
139139
auto it = constraint_set_.find(name);
140+
141+
// Special case for ControlBoxConstraint - must exist
142+
if (std::is_same<T, ControlBoxConstraint>::value) {
143+
if (it == constraint_set_.end()) {
144+
throw std::runtime_error("ControlBoxConstraint not found");
145+
}
146+
return dynamic_cast<T*>(it->second.get());
147+
}
148+
149+
// For other constraints, return nullptr if not found
140150
if (it == constraint_set_.end()) {
141-
throw std::runtime_error("Constraint not found: " + name);
151+
return nullptr;
142152
}
143-
try {
144-
// Note: Returning a non-const reference here
145-
return dynamic_cast<T&>(*(it->second));
146-
} catch (const std::bad_cast& e) {
147-
throw std::runtime_error("Invalid constraint type: " + name);
153+
154+
// Try to cast to the requested type
155+
T* cast_constraint = dynamic_cast<T*>(it->second.get());
156+
if (!cast_constraint) {
157+
return nullptr;
148158
}
159+
160+
return cast_constraint;
149161
}
150162

151163
// Getter for the constraint set

0 commit comments

Comments
 (0)