G4AttUtils.hh

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 // $Id$
00027 //
00028 // Jane Tinslay September 2006
00029 //
00030 // Visualisation attribute utility functions.
00031 //
00032 #ifndef G4ATTUTILS_HH
00033 #define G4ATTUTILS_HH
00034 
00035 #include "globals.hh"
00036 #include "G4AttDef.hh"
00037 #include "G4AttValue.hh"
00038 #include "G4String.hh"
00039 #include "G4TypeKey.hh"
00040 #include <map>
00041 #include <vector>
00042 
00043 namespace G4AttUtils {
00044     
00045   namespace {
00046 
00047     // Utility class
00048     template <typename T>
00049     class HasName {
00050     public:
00051       HasName(const G4String& name): fName(name) {};
00052       bool operator()(const T& value) const
00053       {return value.GetName() == fName;}
00054     private:
00055       G4String fName;
00056     };
00057 
00058   }
00059   
00060   // Extract attribute definition with given name
00061   template <typename T> 
00062   G4bool ExtractAttDef(const T& object, const G4String& name, G4AttDef& def) 
00063   {
00064     const std::map<G4String, G4AttDef>* attDefs = object.GetAttDefs();
00065 
00066     std::map<G4String, G4AttDef>::const_iterator iter = attDefs->find(name);
00067     if (iter == attDefs->end()) return false;    
00068     
00069     def = iter->second;
00070     
00071     return true;
00072   }
00073   
00074   // Extract attribute value with given name
00075   template <typename T>
00076   G4bool ExtractAttValue(const T& object, const G4String& name, G4AttValue& attVal) 
00077   {
00078     std::vector<G4AttValue>* attValues = object.CreateAttValues();
00079     
00080     std::vector<G4AttValue>::iterator iter = std::find_if(attValues->begin(), attValues->end(), 
00081                                                           HasName<G4AttValue>(name));
00082     if (iter == attValues->end()) return false;
00083     
00084     attVal = *iter;
00085     
00086     // Clean up
00087     delete attValues;
00088 
00089     return true;
00090   }
00091 
00092   // Get G4TypeKey information for old style G4AttDef's
00093   G4TypeKey GetKey(const G4AttDef& def);
00094 
00095 }
00096 
00097 #endif

Generated on Mon May 27 17:47:42 2013 for Geant4 by  doxygen 1.4.7