/      日本語

mwf-utils - utils

mwf-utils - utils
Other utilities.

mwf_utils

Other utilities.

prepare_object()

    template <class T>
    static inline std::unique_ptr<T>& prepare_object(std::unique_ptr<T>& spobj, bool b_construct_if_null = true) {
        if (!spobj && b_construct_if_null) {
            spobj.reset(new T());
        }
        return spobj;
    }

This function references an object of the smart pointer std::unique_ptr<>. If the object has not been constructed, it is constructed using new T().

get_value_if()

// When getting a value from a function and proceeding with a process using that value
int v = some_func();
if (v != -1) {
    // Do something with the value of v
    printf("%d", v);
}

// Rewrite as follows
if (auto x = get_value_if::ne(some_func(), -1)) {
    printf("%d", x.value());
}

As shown in the example above, this is a utility class for writing code that uses a function’s return value under a certain condition, by using a variable declaration within an if statement.

In the example above, get_value_if::ne() is used. The first parameter is a function call that returns a value, and the second parameter specifies the value for comparison. In this case, the if block is evaluated only when the return value of some_func() is not -1. The types of the first and second parameters must be the same.

The following comparison expressions can be used: eq, ne, lt, le, gt, ge.

get_value_if::xx() (T is a type)Condition
eq(T lhs, const T rhs)(lhs == rhs)Returns true if the values are the same.
ne (T lhs, const T rhs)(lhs != rhs)Returns true if the values are different.
lt (T lhs, const T rhs)(lhs < rhs)Compares values, returns true if the value is smaller.
le (T lhs, const T rhs)(lhs <= rhs)Compares values, returns true if the value is less than or equal to.
gt (T lhs, const T rhs)(lhs > rhs)Compares values, returns true if the value is larger.
ge (T lhs, const T rhs)(lhs >= rhs)Compares values, returns true if the value is greater than or equal to.

Note: T indicates a type (a type parameter in a template construct).