STIR 6.4.0
assign.h
Go to the documentation of this file.
1
2/*
3 Copyright (C) 2005- 2008, Hammersmith Imanet Ltd
4 Copyright (C) 2025, University College London
5 This file is part of STIR.
6
7 SPDX-License-Identifier: Apache-2.0
8
9 See STIR/LICENSE.txt for details
10*/
11
12#ifndef __stir_assign_H__
13#define __stir_assign_H__
14
23#include "stir/type_traits.h"
24#include <typeinfo>
25
26START_NAMESPACE_STIR
27
46
47// generic implementation, used whenever there is no specialisation
48// Note that std::enable_if_t without 2nd argument defaults to `void` (if the first argument is true, of course)
49template <class T, class T2>
50std::enable_if_t<!has_iterator_v<T>>
51assign(T& x, const T2& y)
52{
53 x = y;
54}
55
56// implementation when the first argument has a (STIR) full iterator, e.g. Array
57template <class T, class T2>
58std::enable_if_t<has_full_iterator_v<T>>
59assign(T& v, const T2& y)
60{
61 for (auto iter = v.begin_all(); iter != v.end_all(); ++iter)
62 assign(*iter, y);
63}
64
65// implementation for normal iterators
66template <class T, class T2>
67std::enable_if_t<has_iterator_and_no_full_iterator<T>::value>
68assign(T& v, const T2& y)
69{
70 for (auto& i : v)
71 assign(i, y);
72}
73
74// a few common cases given explictly here such that we don't get conversion warnings all the time.
75inline void
76assign(double& x, const int y)
77{
78 x = static_cast<double>(y);
79}
80
81inline void
82assign(float& x, const int y)
83{
84 x = static_cast<float>(y);
85}
87
88END_NAMESPACE_STIR
89
90#endif
defines various type traits, checking for iterators etc