Add C++ interface for the libbabeltrace2 `bt_value` API
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Fri, 11 Dec 2020 00:40:00 +0000 (19:40 -0500)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Fri, 28 Jan 2022 16:22:26 +0000 (11:22 -0500)
commit33a17831def0537394f436c42d02ba53da8133c3
treedcee3774d3fc4e3b1d9c35a7908b7d768d410446
parentdcfc2f5c7a9372d8301435fa67b5dec09369d9b9
Add C++ interface for the libbabeltrace2 `bt_value` API

This patch adds C++ wrappers for Babeltrace 2 value objects.

The class hierarchy is:

    Value
      NullValue
      BoolValue
      UnsignedIntegerValue
      SignedIntegerValue
      RealValue
      StringValue
      ArrayValue
      MapValue
    ConstValue
      ConstNullValue
      ConstBoolValue
      ConstUnsignedIntegerValue
      ConstSignedIntegerValue
      ConstRealValue
      ConstStringValue
      ConstArrayValue
      ConstMapValue

Implicitly convert from a mutable value to a constant value with
converting constructors and assignment operators.

All classes have the `Shared` type alias which is what the shared()
method returns. You can also implicitly convert from a shared mutable
value to a shared constant value.

Create a shared value with the static create() methods, for example:

    auto myRealVal = bt2::RealValue::create(17.42);

You can also use one of the overloaded bt2::createValue() functions:

    auto myStrVal = bt2::createValue("salut");

Those methods and functions can throw a `LibMemoryError` instance.

`NullValue` has no create() static function as there's nothing to be
created. If you want to wrap the null value singleton, use:

    bt2::NullValue {}

or

    bt2::ConstNullValue {}

If you want a shared null value for any reason, use:

    bt2::NullValue {}.shared()

Downcast a `Value` or `ConstValue` object to a specific value object
with one of its as*() methods, for example:

    auto myBoolVal = myVal.asBool();

Each as*() method checks that the conversion is legal with
BT_ASSERT_DBG().

MapValue::forEach() and ConstMapValue::forEach() accept a function which
receives the key and value of the entry, for example:

    myMapVal.forEach([&](const bpstd::string_view& key,
                         const Value val) {
        // Do something
    });

If you throw anything from such a function, then
bt_value_map_foreach_entry() or bt_value_map_foreach_entry_const()
receives `BT_VALUE_MAP_FOREACH_ENTRY_STATUS_ERROR` or
`BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_ERROR`, which in turn makes the
forEach() method throw a `LibError` instance. As of this patch, there's
no way for the user function to gracefully interrupt the iterating
operation (`BT_VALUE_MAP_FOREACH_ENTRY_STATUS_INTERRUPT` and
`BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_INTERRUPT` status code
equivalents).

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I7c278aa660e9716a69e7d540763dc31623e45fec
Reviewed-on: https://review.lttng.org/c/babeltrace/+/4537
src/cpp-common/bt2/value.hpp [new file with mode: 0644]
This page took 0.025322 seconds and 4 git commands to generate.