File tree Expand file tree Collapse file tree 1 file changed +19
-7
lines changed
include/cddp-cpp/cddp_core Expand file tree Collapse file tree 1 file changed +19
-7
lines changed Original file line number Diff line number Diff line change @@ -135,17 +135,29 @@ class CDDP {
135
135
136
136
// Get a specific constraint by name
137
137
template <typename T>
138
- T& getConstraint (const std::string& name) {
138
+ T* getConstraint (const std::string& name) {
139
139
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
140
150
if (it == constraint_set_.end ()) {
141
- throw std::runtime_error ( " Constraint not found: " + name) ;
151
+ return nullptr ;
142
152
}
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 ;
148
158
}
159
+
160
+ return cast_constraint;
149
161
}
150
162
151
163
// Getter for the constraint set
You can’t perform that action at this time.
0 commit comments