0

I wanted to take a well-defined template class which depends on boost::signals2 and abstract it one generic layer deeper which could remove the dependency, permitting a different implementation to be used but still allowing the higher-level functions to be used by depending on the more generic interface instead of the more specific one. In my refactor, I'm completely stuck. And, I already know I'm a little out of my league here. I just started really learning C++ over the course of this year. But, it's a little fun. But there's something I'm missing from my understanding of templates. To start off, the library I'm working on will be using C++17, I'm using clang++ (Mac), and depending on Boost ~1.7.2 Signals2.

Consider the following excerpts:

#include <boost/signals2.hpp>

template<typename Functional_T, typename Return_T, typename ...Args_T>
using SignalHandler = typename Functional_T::template Functional_T<Return_T(Args_T...)>;

template<typename Connection_T>
struct SignalConnection; //no issues here, just wraps a private Connection_T with a getter/setter

template<typename Connection_T, typename Functional_T, typename Return_T, typename ...Args_T>
struct ISignalEmitter
{
protected:
    virtual Return_T trigger(Args_T ...args) = 0;
public:
    virtual void disconnectAll() = 0;
    virtual SignalConnection<Connection_T> onSignal(SignalHandler<Functional_T, Return_T, Args_T...> &signalHandler) = 0;
    virtual void cancelOnSignal(const SignalConnection<Connection_T> &connection) = 0;
};

But here's where I'm completely stuck. The thing I want to define, in my mind looks like this:

template<typename Return_T, typename ...Args_T>
using BoostSignals2SignalHandler = SignalHandler<boost::signals2::slot, Return_T, Args_T...>;

And I think we all know what happens. slot requires template arguments. So how can I construct this so that it works? I've tried a variety of things, but I'm stuck. My next guess is:

template<typename Return_T, typename ...Args_T>
using BoostSignals2SignalHandler = SignalHandler<template boost::signals2::slot::template, Return_T, Args_T...>;

I hate to ask, but I know I can't be the first person trying to solve this riddle. And there must be a way to think of these templates in a way that's comprehensible and not just guessing and testing. I've got a template alias BoostSignals2SignalHandler that is a specific version of SignalHandler minus the first template argument plus using an existing template as the specific template argument for the more generic template.

I would ask, "is this poor design?," but I feel like the concept is adequate to solve the problem I want to solve. I just don't understand what to do exactly. Can anyone point me in the right direction? Again, I hate to ask. This one's been a headache figuring out. Thanks in advance!

2
  • So you want your template alias SignalHandler to take a template as its first parameter? Do you know about template template parameters?
    – Mark
    Jan 17, 2020 at 12:48
  • I had found information on template template parameters, and I had attempted several incantations that I don't understand when I'm pounding them out. And honestly it appears to be a sort of black magick sourcery more than something I actually understand how to use.
    – David Ball
    Jan 17, 2020 at 16:59

1 Answer 1

1

Instead of listing your template arguments as Return_T, Args_T... I would follow Signals2 example and use a single parameter of function type like Return_T(Args_T...) in SignalHandler.

Then we simply change Functional_T to a template template parameter.

template<template <typename> typename Functional_T, typename Signature_T>
using SignalHandler = Functional_T<Signature_T>;

Now the first parameter passed to SignalHandler must be a template that can be instatiated with 1 template parameter. Since you are using c++17 we can pass templates with more parameter as well, as long as they have default values.

To get Return_T and Args_T... in ISignalEmitter we use partial specialization.

template<typename Connection_T, template <typename> typename Functional_T, typename Signature_T>
struct ISignalEmitter; // Base declaration to match the specialization against

template<typename Connection_T, template <typename> typename Functional_T, typename Return_T, typename ...Args_T>
struct ISignalEmitter<Connection_T, Functional_T, Return_T(Args_T...)> // Matching Return_T and Args_T against Signature_T
{
protected:
    virtual Return_T trigger(Args_T ...args) = 0;
public:
    virtual void disconnectAll() = 0;
    virtual SignalConnection<Connection_T> onSignal(SignalHandler<Functional_T, Return_T(Args_T...)> &signalHandler) = 0;
    virtual void cancelOnSignal(const SignalConnection<Connection_T> &connection) = 0;
};

At this point you can define BoostSignals2SignalHandler like so

template<typename Signature_T>
using BoostSignals2SignalHandler = SignalHandler<boost::signals2::slot, Signature_T>;

using MySignalHandler = BoostSignals2SignalHandler<void()>;
1
  • Okay, wow, super, awesome! You totally explained that in a way that makes sense. The solution is on point. And I see where I was messing up, even though I'm still learning. The header-only lib I'm working on is a split from a codebase I've been working on prior to this; I'm going to generalize the part of the program which does the heavy lifting and share it back out as open-source. I'll be sure to credit your contribution in the source code. My endgame is: class OO: public ObservableObject<enum OOPropType> { ObservableProperty<int> myValue; }; Thank you for the template pattern. That works.
    – David Ball
    Jan 19, 2020 at 7:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.