Category Archives: Programming languages

Why do we pass parameters to coroutines by value in C++?

C++ coroutines and const reference parameters

A coroutine func accepts a parameter by const reference in the code below:

#include <boost/asio.hpp>

#include <iostream>

namespace asio = boost::asio;
using asio::awaitable;
using asio::use_awaitable;

class Param
{
public:

    Param(int val) : m_val(val)
    {
        std::cout << "Param constructor " << m_val << std::endl;
    }
(more…)

Using BOOST 1.89 with BOOST_ASIO_HAS_IO_URING on Linux

Added the following to the common section of CMake:

add_definitions("-DBOOST_ASIO_HAS_IO_URING")

and the following to the project section:

find_library(URING_LIB uring)
target_link_libraries(${TEST_TARGET} PRIVATE ${URING_LIB})
(more…)

Investigating how LDAP works with Seal and Sign flags

C# code:

public void bindWithMs(string ldapServer, int ldapPort, string ldapUser, string ldapPassword)
{
    var ldap = new System.DirectoryServices.Protocols.LdapDirectoryIdentifier(ldapServer, ldapPort);

    using (var connection = new System.DirectoryServices.Protocols.LdapConnection(ldap))
    {
        connection.AuthType = System.DirectoryServices.Protocols.AuthType.Negotiate;
        connection.Timeout = TimeSpan.FromSeconds(120);

        connection.SessionOptions.ProtocolVersion = 3;
        connection.SessionOptions.Signing = true;
        connection.SessionOptions.Sealing = true;

        connection.Credential = new System.Net.NetworkCredential(ldapUser, ldapPassword);
        connection.Bind();
    }
}
(more…)

Increasing image size in WordPress

I updated my Ubuntu 24.04 and my WordPress stopped loading images of size 1.3MB and higher.

I fixed this by adding the following:

client_max_body_size 32M;

to Nginx configuration.

(more…)

An example where we need const_cast in C++

class Example
{
public:

    Example()
    {
        m.emplace("abc", 13);
    }

    const int& findValue(const std::string& val) const
    {
        auto i = m.find(val);

        if (i == m.end()) {
            throw std::runtime_error(std::format("Key {} not found.", val));
        }

        return i->second;
    }

    int& findValue(const std::string& val)
    {
        return const_cast<int&>(
            const_cast<const Example*>(this)->findValue(val));
    }
(more…)

An Interface Segregation Principle (ISP) example

#include <iostream>
#include <memory>
#include <string>
#include <vector>

struct IObject {
    virtual ~IObject() = default;

    /// Returns the name of the object.
    virtual std::string getName() const = 0;

    /// Prints a textual description of the object.
    virtual void describe() const = 0;

    /// Eats another object.
    virtual bool eat(IObject* other) = 0;
};
(more…)

An investigation of why dynamic_cast violates LSP

Signal sender cast in QT

QT implies that the client code will do qobject_cast that is actually dynamic_cast:

awl::ProcessTask<void> MarketModel::coPlaceOrder(OrderPtr p)
{
    // Update SQLite databse...
    // ...
    // QT signals are used in both C++ and QML.
    // They works with QObject*, and they are not aware of concrete types.
    QObject::connect(p.get(), &OrderModel::statusChanged, this, &MarketModel::onOrderStatusChanged);
}
(more…)

Examples of const function parameters in C++

Examples with int

Compiles without warnings with MSVC:

class A
{
public:

    void f(const int a);
};

void A::f(int a)
{
    a = 10;
    std::cout << a;
}
(more…)

Investigating cobalt::generator

Standard C++ generator

The code below demonstrates how standard C++23 synchronous generator works:

#include <boost/cobalt.hpp>
#include <iostream>
#include <generator>

namespace cobalt = boost::cobalt;

std::generator<int> numbers()
{
    for (int i = 1; i <= 5; ++i)
    {
        co_yield i;
    }
}
(more…)

Building boost::cobalt with MSVC 2022

cd C:\dev\repos\boost_1_89_0

set OPENSSL_ROOT_DIR=C:/dev/libs/OpenSSL
set OPENSSL_USE_STATIC_LIBS=ON
 
bootstrap.bat

b2 cxxstd=20 install --with-system --with-thread --with-cobalt --prefix=C:/dev/libs/boost_1_89_0 --toolset=msvc-14.3 -s _WIN32_WINNT=0x0A00 link=static runtime-link=static variant=release address-model=64
b2 cxxstd=20 install --with-system --with-thread --with-cobalt --prefix=C:/dev/libs/boost_1_89_0 --toolset=msvc-14.3 -s _WIN32_WINNT=0x0A00 link=static runtime-link=static variant=debug address-model=64
(more…)