Using boost to eliminate an ugly facade function

Hello, Can boost help me eliminate the ugly facade function I use below? I have a boost::signals2::signal that passes an object ("CPerson"). I want to connect to this signal not to receive the object ("CPerson") but to receive the result of one of its methods ("CPerson::GetAge()"). Can boost help me with this? If so I'm eager to read and learn about it, I just need to know what it is called. Thank you! Chris //=== #include <boost/signals2/signal.hpp> //---------- // A CPerson offers GetAge() //---------- struct CPerson { int GetAge() const { return 0; } }; //---------- // A CCompany has a boss and a signal // to tell when boss is hired //---------- struct CCompany { CPerson GetBoss() const { return mBoss; } boost::signals2::signal<void(const CPerson&)> BossHired; CPerson mBoss; }; //---------- // A CProcessor processes int ages. // It does not know aobut CPerson //---------- struct CProcessor { void f(int Age) {} }; //---------- // ProcessorFacade is used to strip // an age of a CPerson before // passing the age to CProcessor //---------- void ProcessorFacade(CProcessor& Processor, const CPerson& Person) { Processor.f(Person.GetAge()); } //---------- // main puts it all together //---------- int main() { CCompany Company; CProcessor Processor; // Compiler error: the signal sends an instance of CPerson, // not the result of CPerson::GetAge(). Too bad because // CProcessor does not have know about CPerson // // Company.BossHired.connect( // boost::bind(&CProcessor::f, &Processor, _1)); // So we use an ugly facade instead Company.BossHired.connect( boost::bind(ProcessorFacade, boost::ref(Processor), _1)); return 0; }

CCompany Company;
CProcessor Processor;
// Compiler error: the signal sends an instance of CPerson, // not the result of CPerson::GetAge(). Too bad because // CProcessor does not have know about CPerson // // Company.BossHired.connect( // boost::bind(&CProcessor::f, &Processor, _1));
// So we use an ugly facade instead Company.BossHired.connect( boost::bind(ProcessorFacade, boost::ref(Processor), _1));
Although this is not an answer to your question, but if you have c++11 compiler, you can make a lambda: Company.BossHired.connect([&Processor](const CPerson& Person) { Processor.f(Person.GetAge()); });

On Wed, Aug 29, 2012 at 11:50 AM, Igor R <boost.lists@gmail.com> wrote:
Although this is not an answer to your question, but if you have c++11 compiler, you can make a lambda: Company.BossHired.connect([&Processor](const CPerson& Person) { Processor.f(Person.GetAge()); });
Igor, Thank you for the idea and especially writing out the solution. I am not using c++11, but I have the option as I am using a newer gcc. In this simple case a nested bind (as described by Steven) looks simpler. However, I can imagine a more complicated case that might lend itself to a c++11 lambda. Thank you, Chris

AMDG On 08/29/2012 11:24 AM, Chris Stankevitz wrote:
Hello,
Can boost help me eliminate the ugly facade function I use below? I have a boost::signals2::signal that passes an object ("CPerson").
I want to connect to this signal not to receive the object ("CPerson") but to receive the result of one of its methods ("CPerson::GetAge()"). Can boost help me with this? If so I'm eager to read and learn about it, I just need to know what it is called.
You should be able to use a nested bind: boost::bind(&CProcessor::f, &Processor, boost::bind(&CPerson::GetAge, _1)) In Christ, Steven Watanabe
participants (3)
-
Chris Stankevitz
-
Igor R
-
Steven Watanabe