Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7884069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:42:03+00:00 2026-06-03T04:42:03+00:00

I have an application consisting of many linked objects, each of which have parameters

  • 0

I have an application consisting of many linked objects, each of which have parameters that they need in order to function. I am using the context pattern so that each object sets its own parameters according to a context object reference that is given on construction. This works well in the simplified code example given below.

The next feature that I’m trying to add is the observer pattern, so that when the parameters in the context object change, each subscriber object is notified and updates its parameters accordingly. However, I’m having trouble working out the syntax that I need to use in order to connect the signals to the slots.

Ideally, I would like to have the subscriber objects register themselves with the parameter class on construction, so that they can update themselves for the lifetime of the object (i.e. they must deregister themselves on destruction, or better yet, use some kind of RAII technique to deregister themselves if they go out of scope). Below is a code example that I have hopefully boiled down as simple as possible.

The output is currently

Processing unit 0 param = 1

Processing unit 1 param = 2

Processing unit 0 param = 1

Processing unit 1 param = 2

The goal is to get it to be …

Processing unit 0 param = 1

Processing unit 1 param = 2

Processing unit 0 param = 11

Processing unit 1 param = 22

The code that I’m currently working with is below. It compiles just fine in its present form if you want to try it. Please suggest the changes I need in order to connect my signal to the slots. Feel free to point out any design issues that may cause trouble down the line also. Thanks in advance.

//
//  main.cpp
//  context_observer
//

#include <iostream>
#include <sstream>
#include <map>

#include "boost/signals2.hpp"

/* This class holds the parameters and is the class that I want to set
 * observable. It holds a std::map of the parameters and a simple accessor*/
class cParamsContext
{      
    typedef std::map<std::string, float> myMap_t; 
    typedef boost::signals2::signal<void (cParamsContext&)> signal_t;
    
    myMap_t paramMap;    
        
    
public:            
    signal_t sig;

    cParamsContext()
    {
        paramMap["a0"] = 1.f;
        paramMap["a1"] = 2.f;                
    }
    
    
    float getParam( std::string key, float const & defaultVal ) 
    {        
        myMap_t::iterator it = paramMap.find(key); 
        if ( it == paramMap.end() ) 
            return defaultVal; 
        else 
            return it->second; 
    }
    
    void changePars()
    {
        paramMap["a0"] = 11.f;
        paramMap["a1"] = 22.f;
        sig(*this);
    }    
};

/* This is an example of a processing class that I would like to have
 * subscribe to the parameter class.*/
class cProcessingUnit
{
    float parameter;
    int id;
    
public:
    cProcessingUnit(cParamsContext &contextObj, int id_) :  parameter (80.f), id(id_)
    { 
        updatePars(contextObj);
        // Something like contextObj.sig.connect ... here
    }
       
    void updatePars(cParamsContext &contextObj) 
    {
        std::stringstream idStream;
        idStream << id;        
        std::string key = std::string( "a" + idStream.str() );                      
        
        parameter = contextObj.getParam( key, parameter );
    }
    
    float getParam() {return parameter;}
};


/* This is a very simple main function used here for testing. It 
 * instantiates 2 processing objects. The parameters are then changed
 * in the observable parameter object. The processing objects should
 * then update themselves and so the last "cout" calls should reveal the 
 * new parameters. At least, this is what I would like to happen!*/
int main(int argc, char *argv[])
{
    cParamsContext contextObj;       
    
    cProcessingUnit temp0(contextObj, 0);
    cProcessingUnit temp1(contextObj, 1);
            
    std::cout << "Processing unit "  << 0 << " param = " << temp0.getParam() << std::endl;
    std::cout << "Processing unit "  << 1 << " param = " << temp1.getParam() << std::endl;
    
    contextObj.changePars();
    
    std::cout << "Processing unit "  << 0 << " param = " << temp0.getParam() << std::endl;
    std::cout << "Processing unit "  << 1 << " param = " << temp1.getParam() << std::endl;    
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T04:42:05+00:00Added an answer on June 3, 2026 at 4:42 am

    Assuming you wish to call updatePars when the signal fires you’d connect where you commented with this:

    // Something like contextObj.sig.connect ... here
    contextObj.sig.connect(boost::bind(&cProcessingUnit::updatePars, this, _1));
    

    You may need to retain the connection returned so you can disconnect when you’re destroyed. Your question isn’t really well worded though so it’s hard to say if this is what you really need or not.

    Basically, to connect to a signal you need to pass it a “callable entity” that matches the signature of the signal. This can be a binder (result of bind and a few others), a free function, a lambda expression…a custom functor…etc…

    My latest blog entry might be of some help but it’s rather quick and surface.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a C# application using WPF consisting of a single Window holding four
I have an application consisting of many scripts doing some stuff on their own.
Currently we have many applications, where each application has its own error notification and
I have a Rails application which I now plan to deploy many instances to
I have a silverlight based application consisting of charts using silverlight toolkit and connected
I have an application consisting of different modules written in C++. One of the
I have property management application consisting of tables: tenants landlords units properties vendors-contacts Basically
I have a flashlite3 application with navigation consisting of icons the user can browse
I have a web application, built with Maven, consisting of a Java (Spring) backend
I'm creating my first application and I have a window consisting of multiple subclasses

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.