Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:59:08

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
0024 // ********************************************************************
0025 //
0026 //  ---------------------------------------------------------------
0027 //  GEANT4 template class header
0028 //  Class Description:
0029 //      This class delegates which singletons a task references
0030 //  ---------------------------------------------------------------
0031 //  Author: Jonathan Madsen
0032 //  ---------------------------------------------------------------
0033 
0034 #include "G4AutoLock.hh"
0035 #include "G4Threading.hh"
0036 
0037 #include <functional>
0038 #include <map>
0039 #include <memory>
0040 #include <set>
0041 #include <thread>
0042 #include <tuple>
0043 #include <type_traits>
0044 #include <vector>
0045 
0046 namespace G4Traits
0047 {
0048   template <typename T>
0049   struct TaskSingletonKey
0050   {
0051     using type         = G4int;
0052     using compare_type = std::less<type>;
0053   };
0054 }  // namespace G4Traits
0055 
0056 //----------------------------------------------------------------------------//
0057 
0058 /// \class G4TaskSingletonEvaluator
0059 /// \brief This structure must be specialized and use overloads to the
0060 /// constructor
0061 ///
0062 template <typename T>
0063 struct G4TaskSingletonEvaluator;
0064 
0065 //----------------------------------------------------------------------------//
0066 
0067 template <typename T>
0068 class G4TaskSingletonDelegator;
0069 
0070 //----------------------------------------------------------------------------//
0071 
0072 template <typename T>
0073 class G4TaskSingletonData
0074 {
0075   using key_type     = typename G4Traits::TaskSingletonKey<T>::type;
0076   using compare_type = typename G4Traits::TaskSingletonKey<T>::compare_type;
0077 
0078   friend struct G4TaskSingletonEvaluator<T>;
0079   friend class G4TaskSingletonDelegator<T>;
0080   using this_type = G4TaskSingletonData<T>;
0081 
0082  private:
0083   static std::unique_ptr<this_type>& GetInstance()
0084   {
0085     static auto _instance = std::unique_ptr<this_type>(new this_type);
0086     return _instance;
0087   }
0088 
0089  private:
0090   std::map<key_type, T*, compare_type> m_data;
0091 };
0092 
0093 //----------------------------------------------------------------------------//
0094 
0095 template <typename T>
0096 struct G4TaskSingletonEvaluator
0097 {
0098   using key_type  = typename G4Traits::TaskSingletonKey<T>::type;
0099   using data_type = G4TaskSingletonData<T>;
0100 
0101   template <typename... Args>
0102   G4TaskSingletonEvaluator(key_type&, Args&&...)
0103   {
0104     throw std::runtime_error("Not specialized!");
0105   }
0106 };
0107 
0108 //----------------------------------------------------------------------------//
0109 
0110 template <typename T>
0111 class G4TaskSingletonDelegator
0112 {
0113  public:
0114   using pointer        = T*;
0115   using evaluator_type = G4TaskSingletonEvaluator<T>;
0116   using data_type      = G4TaskSingletonData<T>;
0117   using key_type       = typename G4Traits::TaskSingletonKey<T>;
0118 
0119   template <typename... Args>
0120   static void Configure(Args&&... args)
0121   {
0122     auto& _data = data_type::GetInstance();
0123     evaluator_type _eval(std::forward<Args>(args)...);
0124     auto _ptr = _data->m_data.at(_eval.index);
0125     _eval.modifier(_ptr);
0126     T::SetInstance(_data->m_data.at(_key));
0127   }
0128 };
0129 
0130 //----------------------------------------------------------------------------//