Mantid
Loading...
Searching...
No Matches
DynamicPointerCastHelper.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2024 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7#pragma once
8
9#include <memory>
10#include <string>
11
12namespace Mantid {
13namespace Kernel {
14namespace DynamicPointerCastHelper {
15
16/*
17Will cast a shared_ptr of type U to one of type T using std::dynamic_pointer_cast. If the cast
18is invalid then it will throw an exception. This is useful for avoiding warnings about
19potential null objects coming out of std::dynamic_pointer_cast.
20*/
21template <typename T, typename U>
22std::shared_ptr<T> dynamicPointerCastWithCheck(std::shared_ptr<U> sharedPtr, const std::string &error = "") {
23 auto result = std::dynamic_pointer_cast<T>(sharedPtr);
24 if (result == nullptr) {
25 throw std::invalid_argument(error.empty() ? "Invalid cast" : error);
26 }
27 return result;
28}
29
30} // namespace DynamicPointerCastHelper
31} // namespace Kernel
32} // namespace Mantid
double error
std::shared_ptr< T > dynamicPointerCastWithCheck(std::shared_ptr< U > sharedPtr, const std::string &error="")
Helper class which provides the Collimation Length for SANS instruments.