SIRF  3.6.0
xgadgetron_utilities.h
Go to the documentation of this file.
1 /*
2 SyneRBI Synergistic Image Reconstruction Framework (SIRF)
3 Copyright 2015 - 2019 Rutherford Appleton Laboratory STFC
4 
5 This is software developed for the Collaborative Computational
6 Project in Synergistic Reconstruction for Biomedical Imaging (formerly CCP PETMR)
7 (http://www.ccpsynerbi.ac.uk/).
8 
9 Licensed under the Apache License, Version 2.0 (the "License");
10 you may not use this file except in compliance with the License.
11 You may obtain a copy of the License at
12 http://www.apache.org/licenses/LICENSE-2.0
13 Unless required by applicable law or agreed to in writing, software
14 distributed under the License is distributed on an "AS IS" BASIS,
15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 See the License for the specific language governing permissions and
17 limitations under the License.
18 
19 */
20 
30 #ifndef XGADGETRON_UTILITIES
31 #define XGADGETRON_UTILITIES
32 
33 #include <chrono>
34 #include <complex>
35 #include <mutex>
36 //#include <boost/thread/mutex.hpp>
37 
38 #include "sirf/Gadgetron/cgadgetron_shared_ptr.h"
39 
40 namespace sirf {
41 
43  public:
44  static long long milliseconds()
45  {
46  auto now = std::chrono::system_clock::now();
47  auto ms = std::chrono::duration_cast < std::chrono::milliseconds >
48  (now.time_since_epoch());
49  return (long long)ms.count();
50  }
51  static std::string scratch_file_name()
52  {
53  static int calls = 0;
54  char buff[128];
55  long long int ms = xGadgetronUtilities::milliseconds();
56  calls++;
57 #ifdef _MSC_VER
58  sprintf_s(buff, 128, "tmp_%d_%lld.h5", calls, ms);
59 #else
60  sprintf(buff, "tmp_%d_%lld.h5", calls, ms);
61 #endif
62  return std::string(buff);
63  }
64  template<typename T>
65  static void convert_complex(std::complex<T> z, unsigned short& t)
66  {
67  t = (unsigned short)z.real();
68  }
69  template<typename T>
70  static void convert_complex(std::complex<T> z, short& t)
71  {
72  t = (short)z.real();
73  }
74  template<typename T>
75  static void convert_complex(std::complex<T> z, unsigned int& t)
76  {
77  t = (unsigned int)z.real();
78  }
79  template<typename T>
80  static void convert_complex(std::complex<T> z, int& t)
81  {
82  t = (int)z.real();
83  }
84  template<typename T>
85  static void convert_complex(std::complex<T> z, float& t)
86  {
87  t = (float)z.real();
88  }
89  template<typename T>
90  static void convert_complex(std::complex<T> z, complex_float_t& t)
91  {
92  t = (complex_float_t)z;
93  }
94  template<typename T>
95  static void convert_complex(std::complex<T> z, double& t)
96  {
97  t = (double)z.real();
98  }
99  template<typename T>
100  static void convert_complex(std::complex<T> z, complex_double_t& t)
101  {
102  t = (complex_double_t)z;
103  }
104 
105  };
106 
107  class Mutex {
108  public:
109  Mutex()
110  {
111  init_();
112  }
113  std::mutex& operator()()
114  {
115  return *sptr_mutex_.get();
116  }
117  gadgetron::shared_ptr<std::mutex> sptr()
118  {
119  return sptr_mutex_;
120  }
121  void lock()
122  {
123  sptr_mutex_->lock();
124  }
125  void unlock()
126  {
127  sptr_mutex_->unlock();
128  }
129  private:
130  static gadgetron::shared_ptr<std::mutex> sptr_mutex_;
131  static void init_()
132  {
133  static bool initialized = false;
134  if (!initialized) {
135  sptr_mutex_ = gadgetron::shared_ptr<std::mutex>(new std::mutex);
136  initialized = true;
137  }
138  }
139  };
140 
141 }
142 
143 #endif
Definition: xgadgetron_utilities.h:107
Definition: xgadgetron_utilities.h:42
Abstract data container.
Definition: GeometricalInfo.cpp:141