Skip to content

Commit

Permalink
feat: more compile-time interface checking
Browse files Browse the repository at this point in the history
  • Loading branch information
muffgaga committed Dec 1, 2023
1 parent 50c00b8 commit 25352a8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
15 changes: 0 additions & 15 deletions nestkernel/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,6 @@ class Connection
Connection( const Connection< targetidentifierT >& rhs ) = default;
Connection& operator=( const Connection< targetidentifierT >& rhs ) = default;

/**
* Get a pointer to an instance of a SecondaryEvent if this connection supports secondary events.
*
* To prevent erronous calls of this function on primary connections, the base class implementation
* below just contains `assert(false)`.
*/
SecondaryEvent* get_secondary_event();

/**
* Get all properties of this connection and put them into a dictionary.
*/
Expand Down Expand Up @@ -398,13 +390,6 @@ Connection< targetidentifierT >::trigger_update_weight( const size_t,
throw IllegalConnection( "Connection does not support updates that are triggered by a volume transmitter." );
}

template < typename targetidentifierT >
SecondaryEvent*
Connection< targetidentifierT >::get_secondary_event()
{
assert( false );
}

} // namespace nest

#endif /* CONNECTION_H */
31 changes: 30 additions & 1 deletion nestkernel/connector_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,26 @@ enum class ConnectionModelProperties : unsigned
REQUIRES_CLOPATH_ARCHIVING = 1 << 6,
REQUIRES_URBANCZIK_ARCHIVING = 1 << 7
};
}

namespace
{ // FIXME: utils namespace?

template < typename, typename = void >
struct has_get_secondary_event_t : std::false_type
{};

template < typename ConnectionT >
struct has_get_secondary_event_t<
ConnectionT,
std::void_t<
decltype( std::declval< ConnectionT >().get_secondary_event( ) ) > > : public std::true_type
{};

} // end of FIXME

namespace nest
{

template <>
struct EnableBitMaskOperators< ConnectionModelProperties >
Expand Down Expand Up @@ -199,7 +219,16 @@ class GenericConnectorModel : public ConnectorModel
SecondaryEvent*
get_secondary_event() override
{
return default_connection_.get_secondary_event();
constexpr bool is_primary = flag_is_set( ConnectionT::properties, nest::ConnectionModelProperties::IS_PRIMARY );
constexpr bool has_get_secondary_event = has_get_secondary_event_t< ConnectionT >::value;
static_assert( is_primary xor has_get_secondary_event,
"Non-primary connections have to provide get_secondary_event()" );
if constexpr ( ( not is_primary ) and has_get_secondary_event ) {
return default_connection_.get_secondary_event();
} else {
// unreachable code
return nullptr;
}
}

ConnectionT const&
Expand Down
5 changes: 3 additions & 2 deletions nestkernel/connector_model_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ GenericConnectorModel< ConnectionT >::clone( std::string name, synindex syn_id )
ConnectorModel* new_cm = new GenericConnectorModel( *this, name ); // calls copy construtor
new_cm->set_syn_id( syn_id );

const bool is_primary = new_cm->has_property( ConnectionModelProperties::IS_PRIMARY );
if ( not is_primary )
constexpr bool is_primary = flag_is_set( ConnectionT::properties, nest::ConnectionModelProperties::IS_PRIMARY );
constexpr bool has_get_secondary_event = has_get_secondary_event_t< ConnectionT >::value;
if constexpr ( ( not is_primary ) and has_get_secondary_event )
{
new_cm->get_secondary_event()->add_syn_id( syn_id );
}
Expand Down

0 comments on commit 25352a8

Please sign in to comment.