Wednesday, October 30, 2024

Signals and Slots in C++ using Boost's Library...

 I remember in the early part of my Bangalore stay, two C++ libraries attracted me - Boost and Loki. However, with the daily grinding in the office, i never got time to deep dive into it. And finally i got a chance to look into the Boost library in 2007, during my stay in Japan. But it was difficult - without a proper hand holding. I just got a chance to delve into the Boost pointers. 

Today, i will discuss about another important aspect of Boost library - Signals and Slots. These are Boost's answer to publisher-subscriber or Observer pattern or as the Java people call it - the event listener pattern - and for all others - simply the event driven programming.

You should use this pattern in case of...

Event-Driven Programming:

Signals and slots are ideal for event-driven programming, where objects need to react to events triggered by other objects.

GUI Development:

Boost.Signals2 is commonly used in GUI development to connect UI elements to actions or events.

Decoupling Components:

Signals and slots allow you to decouple components of your application, making it easier to modify and maintain the code.

Example : 

The following example writes "Hello, World!" using signals and slots. 

Here are the two function objects.

class Hello

{

public:

void operator()() const

{

cout << "Hello";

}

};


class World

{

public:

void operator()() const

{

cout << ", World!" << endl;

}

};

Then, we create a signal sig1, a signal that takes no arguments and has a void return value. 

boost::signals2::signal<void()> sig1;

Next, we connect the hello and world function objects to the signal using the connect method.

boost::signals2::connection c1 = sig1.connect(hello);


boost::signals2::connection c2 = sig1.connect(world);


By default, slots are pushed onto the back of the slot

list, so the output of this program will be as expected.


Hello, World!


The way signals and slots are connected, they can

be disconnected as wellas we have done here.


c1.disconnect(); // Disconnect the Hello object


Signals can propagate arguments to each of the slots

they call. For instance, a signal that propagates mouse

motion events might want to pass along the new mouse

coordinates and whether the mouse buttons are pressed.


//Slot Arguments

void print_args(float x, float y)

{

cout << "The arguments are " << x << " and " << y << endl;

}


void print_sum(float x, float y)

{

cout << "The sum is " << x + y << endl;

}


void print_product(float x, float y)

{

cout << "The product is " << x * y << endl;

}


void print_difference(float x, float y)

{

cout << "The difference is " << x - y << endl;

}


void print_quotient(float x, float y)

{

cout << "The quotient is " << x / y << endl;

}


And this is how we create the signals


boost::signals2::signal<void (float, float)> sig;


sig.connect(&print_args);

sig.connect(&print_sum);

sig.connect(&print_product);

sig.connect(&print_difference);

sig.connect(&print_quotient);


sig(5., 3.);


And the output will be as follows:


The arguments are 5 and 3 The sum is 8 The product is 15 The difference is 2 The quotient is 1.66667


Here is the complete source code of my

exploration.


Enjoy...


//============================================================================

// Name : BoostSignal2Test.cpp

// Author : Som

// Version :

// Copyright : som-itsolutions

// Description : Hello World in C++, Ansi-style

//============================================================================


#include <boost/signals2/signal.hpp>

#include <iostream>

using namespace std;

class Hello

{

public:

void operator()() const

{

cout << "Hello";

}

};


class World

{

public:

void operator()() const

{

cout << ", World!" << endl;

}

};

//Slot Arguments

void print_args(float x, float y)

{

cout << "The arguments are " << x << " and " << y << endl;

}


void print_sum(float x, float y)

{

cout << "The sum is " << x + y << endl;

}


void print_product(float x, float y)

{

cout << "The product is " << x * y << endl;

}


void print_difference(float x, float y)

{

cout << "The difference is " << x - y << endl;

}


void print_quotient(float x, float y)

{

cout << "The quotient is " << x / y << endl;

}



int main() {

boost::signals2::signal<void()> sig1;


Hello hello;

World world;


boost::signals2::connection c1 = sig1.connect(hello);

boost::signals2::connection c2 = sig1.connect(world);


sig1();


    c1.disconnect(); // Disconnect the HelloWorld object

    cout << "c1 is disconnected\n";


    sig1();


    c2.disconnect();

    cout << "c2 is disconnected\n";

    sig1();



    //parameter passing


    boost::signals2::signal<void (float, float)> sig;


    sig.connect(&print_args);

    sig.connect(&print_sum);

    sig.connect(&print_product);

    sig.connect(&print_difference);

    sig.connect(&print_quotient);


    sig(5., 3.);


return 0;

}


Friday, October 11, 2024

Do you want your python code to access C/C++ code? use SWIG...

 SWIG (Simplifies Wrapper and Interface Generator) is an interface compiler that connects programs written in C and C++ with scripting languages such as Perl, Python, Ruby, and Tcl. It works by taking the declarations found in C/C++ header files and using them to generate the wrapper code that scripting languages need to access the underlying C/C++ code.

Using SWIG, you can replace the main() function of a C/C++ program with a scripting interpreter from which you can control the application. This adds quite a lot of flexibility and makes the program "programmable." 

SWIG allows C/C++ programs to be placed in a scripting environment that can be used for testing and debugging. For example, you might test a library with a collection of scripts or use the scripting interpreter as an interactive debugger.

With SWIG, different C/C++ programs can be turned into scripting language extension modules. These modules can then be combined together to create new and interesting applications.

SWIG is sometimes compared to interface definition language (IDL) compilers such as those you find with systems such as CORBA and COM. Although there are a few similarities, the whole point of SWIG is to make it so you don't have to add an extra layer of IDL specifications to your application.

SWIG requires little, if any, modifications to existing code. For the most part, it encourages you to keep a clean separation between C/C++ and its scripting interface.

The primary audience of SWIG is C/C++ programmers who want to add a scripting language component to their applications.

So... here we go...

My experimentation with SWIG...

Step I. MyClass.h

#ifndef MYCLASS_H
#define MYCLASS_H

class MyClass {
public:
    MyClass();
    ~MyClass();
    void set_value(int val);
    int get_value() const;

private:
    int value;
};

#endif

Step II

MyClass.cpp

#include "MyClass.h"

MyClass::MyClass() : value(0) {}

MyClass::~MyClass() {}

void MyClass::set_value(int val) {
    value = val;
}

int MyClass::get_value() const {
    return value;
}


Step III


MyClass.i - the SWIG interface file...


%module MyClass
%{
#include "MyClass.h"
%}

%include "MyClass.h"


Step IV


Generate the Wrapper Code Using SWIG


Run in terminal the following command


swig -python -c++ MyClass.i


This generates two files:

  • MyClass_wrap.cxx: The C++ wrapper code SWIG generates.
  • MyClass.py: The Python module that SWIG creates, which interfaces with the C++ code.

Step V

Then compile the C++ code and the SWIG-generated wrapper using g++ or another compiler. Make sure to link against Python libraries as shown below


g++ -fPIC -shared MyClass_wrap.cxx MyClass.cpp -I/usr/include/python3.x -o _MyClass.so


Here:

  • -fPIC ensures that the code is position-independent (required for shared libraries).
  • -shared tells the compiler to generate a shared library (_MyClass.so).
  • The -I flag specifies the path to Python headers.

Step VI

Once compiled, you can use the MyClass module in Python:

import MyClass

# Create an instance of MyClass
obj = MyClass.MyClass()

# Set and get value using the wrapped C++ methods
obj.set_value(100)
print(obj.get_value())  # Output: 100