STIR  6.3.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 
26 START_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)
49 template <class T, class T2>
50 std::enable_if_t<!has_iterator_v<T>>
51 assign(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
57 template <class T, class T2>
58 std::enable_if_t<has_full_iterator_v<T>>
59 assign(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
66 template <class T, class T2>
67 std::enable_if_t<has_iterator_and_no_full_iterator<T>::value>
68 assign(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.
75 inline void
76 assign(double& x, const int y)
77 {
78  x = static_cast<double>(y);
79 }
80 
81 inline void
82 assign(float& x, const int y)
83 {
84  x = static_cast<float>(y);
85 }
87 
88 END_NAMESPACE_STIR
89 
90 #endif
defines various type traits, checking for iterators etc