Is there a simple way to check unsafe expression in C++?

╄→гoц情女王★ 提交于 2019-12-10 23:09:35

问题


I'm trying to find a [better] way to run/check a potentially unsafe expression or perform multiple null checks in a more elegant way.

Here is an example of codes I would like to improve:

if (myObjectPointer &&
    myObjectPointer->getSubObject() &&
    myObjectPointer->getSubObject()->getSubSubObject() &&
    myObjectPointer->getSubObject()->getSubSubObject()->getTarget()) {

  // Use safely target
  ... *(myObjectPointer->getSubObject()->getSubSubObject()->getTarget()) ...
}

I tried to find a more elegant way to achieve this (instead of the above verbose null checks). Here is my first thoughts:

template<typename T>
bool isSafe(T && function) {
   try {
       function(); 
       // Just running the func above, but we could e.g. think about returning the actual value instead of true/fase - not that important. 
       return true;
    }
    catch (...) {
       return false;
    }
}

...
// And use the above as follow :
if(isSafe([&](){ myObjectPointer->getSubObject()->getSubSubObject()->getTarget(); })) {
    // Use safely target
}
...

The problem with the above is that we can't catch signals (Segmentation fault, ...). And I obviously don't want to handle all signals in the program, but only in this very specific check/eval function.

I'm I tackling the problem the wrong way ? Any other recommendations ? or the verbose if is inevitable ?

Many thanks in advance.


回答1:


I was thinking about this, and like Jarod42 said, there must be some variadic template stuff. I'm not the best at this, but came up with this:

#include <memory>
#include <functional>
#include <iostream>

template <typename T, typename MemFn, typename... Params> 
void safeExecute(T* ptr, MemFn memFn, Params&&... params) {
    if (ptr != nullptr)
        safeExecute(std::invoke(memFn, ptr), std::forward<Params>(params)...);
}

template <typename T, typename MemFn>
void safeExecute(T* ptr, MemFn memFn) {
    if (ptr != nullptr) std::invoke(memFn, ptr);
}


struct Target {
    void Bar() { std::cout << "tada!\n"; };
};


template<typename T>
class Object {
private:
    std::unique_ptr<T> ptr;
public:
    Object() : ptr(std::make_unique<T>()) {}

    T* Get() { return ptr.get(); }
};

using SubSubObject = Object<Target>;
using SubObject = Object<SubSubObject>;
using MyObject = Object<SubObject>;

int main() {
    auto myObjectPtr = std::make_unique<MyObject>();

    safeExecute(myObjectPtr.get(),
                &MyObject::Get,
                &SubObject::Get,
                &SubSubObject::Get,
                &Target::Bar);
}

edit: I've been playing with the idea of having a more general return type, so I experimented with the option not to call the member function, but to return an std::optional pointer to the object. This lead me to the following code:

#include <memory>
#include <functional>
#include <iostream>
#include <optional>

template <typename T, typename MemFn, typename... Params>
auto safeGetObject(T* ptr, MemFn memFn, Params&&... params)
    -> decltype(safeGetObject(std::invoke(memFn, std::declval<T>()), std::forward<Params>(params)...))
{
    if (ptr != nullptr) return safeGetObject(std::invoke(memFn, ptr), std::forward<Params>(params)...);
    return {};
}

template <typename T, typename MemFn>
auto safeGetObject(T* ptr, MemFn memFn) -> std::optional<decltype(std::invoke(memFn, std::declval<T>()))> {
    if (ptr != nullptr) return std::invoke(memFn, ptr);
    return {};
}

struct Target {
    int Bar(int a, int b) const noexcept {
        return a+b;
    };
};

template<typename T>
class Object {
private:
    std::unique_ptr<T> ptr;
public:
    Object() noexcept : ptr(std::make_unique<T>()) {}

    T* Get() const noexcept { return ptr.get(); }
};

using SubSubObject = Object<Target>;
using SubObject = Object<SubSubObject>;
using MyObject = Object<SubObject>;

int main() {
    auto myObjectPtr = std::make_unique<MyObject>();

    auto optionalTarget = safeGetObject(
        myObjectPtr.get(),
        &MyObject::Get,
        &SubObject::Get,
        &SubSubObject::Get);

    auto result = optionalTarget ? optionalTarget.value()->Bar(3, 4) : -1;
    std::cout << " result " << result << '\n';
}



回答2:


Putting possible design issues aside, you could use an extended version of std::optional. Since not all intefaces are under your control, you would have to wrap the functions were necessary into a free-function. Let's assume you can change the class MyClass of myObjectPointer, but not the classes of the sub-objects.

class MyClass  {
public:
    optional<std::reference_wrapper<SubObjectClass>> getSubObject();
};

optional<std::reference_wrapper<SubSubObjectClass>> getSubSubObject(SubObjectClass& s) {
    SubSubObjectClass* ptr = s.getSubSubObject();

    if (ptr) {
        return std::ref(s.getSubSubObject());
    } else {
        return {};
    }
}

optional<std::reference_wrapper<Target>> getTarget(SubSubObjectCLass& s) {
    ...
}

You can now write something like

optional<MyClass*>  myObjectPointer = ...;
myObjectPointer.and_then(MyClass::getSubObject)
               .and_then(getSubSubObject)
               .and_then(getTarget)
               .map( doSomethingWithTarget ):



回答3:


OK, I might delete my previous answer, because I've been rethinking this, now considering using std::optional and chaining. Your original

myObjectPointer->getSubObject()->getSubSubObject()->getTarget()

is not really reproducible, since operator->() cannot be static. But we can use another operator, like operator>>(). Thus:

#include <memory>
#include <iostream>
#include <optional>
#include <functional>

struct Target {
    int Bar(int a, int b) const noexcept { return a+b; };
};

template<typename T>
class Object {
private:
    T* const ptr;
public:
    Object(T* ptr) noexcept : ptr(ptr) {}
    T* Get() const noexcept { return ptr; }
};

using SubSubObject = Object<Target>;
using SubObject = Object<SubSubObject>;
using MyObject = Object<SubObject>;

template <typename T>
auto makeOptional(T* ptr) -> std::optional< std::reference_wrapper<T>> {
    if (ptr) return std::ref(*ptr);
    return {};
}

template <typename T, typename MemFn>
auto operator>> (std::optional<std::reference_wrapper<T>> optObj, MemFn memFn)
-> std::optional< std::reference_wrapper<std::remove_pointer_t<decltype(std::invoke(memFn, std::declval<T>()))>>> {
    if (optObj) return makeOptional(std::invoke(memFn, *optObj));
    return {};
}


int main() {
    {
        //complete
        auto TargetPtr = std::make_unique<Target>();
        auto subSubObjectPtr = std::make_unique<SubSubObject>(TargetPtr.get());
        auto subObjectPtr = std::make_unique<SubObject>(subSubObjectPtr.get());
        auto myObjectPtr = std::make_unique<MyObject>(subObjectPtr.get());

        auto optionalMyObject = makeOptional(myObjectPtr.get());

        auto optionalTarget = optionalMyObject >> &MyObject::Get >> &SubObject::Get >> &SubSubObject::Get;

        auto result = (optionalTarget) ? optionalTarget->get().Bar(3, 4) : -1;

        std::cout << "result is " << result << '\n';
    }
    {
        // incomplete
        auto subObjectPtr = std::make_unique<SubObject>(nullptr);
        auto myObjectPtr = std::make_unique<MyObject>(subObjectPtr.get());

        auto optionalMyObject = makeOptional(myObjectPtr.get());

        auto optionalTarget = optionalMyObject >> &MyObject::Get >> &SubObject::Get >> &SubSubObject::Get;

        auto result = (optionalTarget) ? optionalTarget->get().Bar(3, 4) : -1;

        std::cout << "result is " << result << '\n';
    }
}

will work... Let me know if this is what you're looking for.


edit: I've also tried putting it in a wrapper class

#include <memory>
#include <iostream>
#include <functional>
#include <optional>

struct Target {
    constexpr int Bar(int a, int b) const noexcept { return a + b; };
};

template<typename T>
class Object {
private:
    T* const ptr;
public:
    constexpr Object(T* const ptr) noexcept : ptr(ptr) {}
    constexpr T* Get() const noexcept { return ptr; }
};

using SubSubObject = Object<Target>;
using SubObject = Object<SubSubObject>;
using MyObject = Object<SubObject>;

template<typename T>
class ObjectWrapper {
private:
    std::optional<std::reference_wrapper<T>> optRefObj{};
public:
    constexpr ObjectWrapper(T* ptr) noexcept
        : optRefObj(ptr ? std::make_optional(std::ref(*ptr)) : std::nullopt)
    {}

    template<typename MemFn>
    constexpr auto operator>>(MemFn memFn) const noexcept {
        return ObjectWrapper<std::remove_pointer_t<decltype(std::invoke(memFn, std::declval<T>()))>>
            (optRefObj ? std::invoke(memFn, *optRefObj) : nullptr);
    }

    constexpr operator bool() const noexcept { return optRefObj.has_value(); }

    constexpr T* Get() noexcept { return optRefObj ? &optRefObj->get() : nullptr; }
};

int main() {
    {
        //complete
        auto const TargetPtr = std::make_unique<Target>();
        auto const subSubObjectPtr = std::make_unique<SubSubObject>(TargetPtr.get());
        auto const subObjectPtr = std::make_unique<SubObject>(subSubObjectPtr.get());
        auto const myObjectPtr = std::make_unique<MyObject>(subObjectPtr.get());

        auto const myObjWrp = ObjectWrapper(myObjectPtr.get());

        auto optionalTarget = myObjWrp >> &MyObject::Get >> &SubObject::Get >> &SubSubObject::Get;

        auto const result = optionalTarget ? optionalTarget.Get()->Bar(3, 4) : -1;

        std::cout << "result is " << result << '\n';
    }
    {
        // incomplete
        auto const subObjectPtr = std::make_unique<SubObject>(nullptr);
        auto const myObjectPtr = std::make_unique<MyObject>(subObjectPtr.get());

        auto const myObjWrp = ObjectWrapper(myObjectPtr.get());

        auto optionalTarget = myObjWrp >> &MyObject::Get >> &SubObject::Get >> &SubSubObject::Get;

        auto const result = optionalTarget ? optionalTarget.Get()->Bar(3, 4) : -1;

        std::cout << "result is " << result << '\n';
    }
}


来源:https://stackoverflow.com/questions/58598168/is-there-a-simple-way-to-check-unsafe-expression-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!