Home > Back-end >  How to store and process possibly duplicate distributed class coupling information in C ?
How to store and process possibly duplicate distributed class coupling information in C ?

Time:11-09

I'm writing a template<class ...Ts> class SortedTypeHolder. During compile time instantiation SortedTypeHolder must collect distributed information about all provided ...Ts from their headers for future sorting. The gathered information describes which classes depend on others in a form of template<typename A, typename B> class Pair specializations. I want to provide multiple dependees for one depender class at once with a macro DEFINE_PAIRS.

#include <boost/preprocessor.hpp>
template<class ...Ts> class SortedTypeHolder;

template<typename Depender, typename Dependee>
class Pair : std::false_type {}; //doesn't depend, the default

#define TRUESPEC( _, Depender, Dependee ) \
template<> class Pair<Depender, Dependee> : public std::true_type {}; //depends

#define DEFINE_PAIRS( Depender, ... ) \
   BOOST_PP_LIST_FOR_EACH( TRUESPEC, Depender, BOOST_PP_TUPLE_TO_LIST( ( __VA_ARGS__ ) ) )

class A {};
class B {};
class C {};
class D {};

// from A.hpp
DEFINE_PAIRS( A, B, C, D )
// from B.hpp
DEFINE_PAIRS( B, A, C, D )
// from D.hpp. this dependency is reverse yet allowed
//DEFINE_PAIRS( A, D )     //Error C2766 explicit specialization; 'Pair<A,D>' has already been defined

...

SortedTypeHolder<A,B,C,D> holder;

I also want my code to allow duplicate dependencies without generating new specializations, but my current realization produces an error in such case. Please give me an advice how do I enhance the code to allow duplicate dependencies. If it is possible to define multiple Pairs at once without macros, it will be great.

CodePudding user response:

Just use function declarations. You don't need to define them, can easily extract the parameter types, can have duplicate forward declarations with no problem, and get ADL for free.

void dependency(A, B, C, D); // A depends on B, C, D

or, if it's easier to use

std::tuple<B, C, D> dependency(A);

You'll need to either use SFINAE to handle classes with no dependencies, or have a special guaranteed-worst-match

NoDepTagType dependency(...);
  • Related