cpp-common/bt2: systematize the `const` method situation
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Fri, 3 Nov 2023 15:32:49 +0000 (11:32 -0400)
committerSimon Marchi <simon.marchi@efficios.com>
Thu, 14 Dec 2023 15:57:04 +0000 (10:57 -0500)
commitdcb8ae9be121cdae6f6d16757d2233d705df057d
treefbacadadbf0a8b21aded095d98682c47cd871abd
parentc677c492013b15993843ac26e5210a544f64ba90
cpp-common/bt2: systematize the `const` method situation

This patch attempts to systematize the `const` situation regarding
methods (member functions and operators).

The current issue is that, because those wrappers are expected to be
passed and returned by copy (they only wrap a libbabeltrace2 pointer),
one cannot rely on the `const` qualifier to ensure immutability at the
library level. For example:

    void f(const bt2::ArrayValue val)
    {
        // Want to modify what `val` wraps? No problem:
        auto newVal = val;

        newVal += 23;
    }

The signature of f() here doesn't guarantee to the caller that what
`val` wraps is immutable. To achieve this, it needs to accept the
`Const` version of the wrapper:

    void f(bt2::ConstArrayValue val)
    {
        // Now there's no way to modify what `val` wraps
    }

Therefore, since using the `const` keyword is so fragile, this patch
makes `const` (almost) all the methods of wrapper classes: those methods
do not modify the wrapped pointer value, therefore they may be `const`.

This makes the wrapping system have this correspondence:

          bt_xyz *                    bt2::Xyz
          bt_xyz * const        const bt2::Xyz
    const bt_xyz *                    bt2::ConstXyz
    const bt_xyz * const        const bt2::ConstXyz

Because there were many overloads to return a `bt2::Const*` instance
when the object is `const`, this patch removes a lot of code, with the
added benefit of reducing code duplication.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: Ie633e7e9d67a8eb0138800ad8ae14c8d9bd0ca4c
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11228
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
src/cpp-common/bt2/clock-class.hpp
src/cpp-common/bt2/common-iterator.hpp
src/cpp-common/bt2/field-class.hpp
src/cpp-common/bt2/field.hpp
src/cpp-common/bt2/integer-range-set.hpp
src/cpp-common/bt2/message.hpp
src/cpp-common/bt2/trace-ir.hpp
src/cpp-common/bt2/value.hpp
This page took 0.028019 seconds and 4 git commands to generate.