Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:23:37

0001 #ifndef PhysicsTools_MVAComputer_AtomicId_h
0002 #define PhysicsTools_MVAComputer_AtomicId_h
0003 // -*- C++ -*-
0004 //
0005 // Package:     MVAComputer
0006 // Class  :     AtomicID
0007 //
0008 
0009 //
0010 // Author:  Christophe Saout <christophe.saout@cern.ch>
0011 // Created:     Sat Apr 24 15:18 CEST 2007
0012 //
0013 
0014 #include <ostream>
0015 #include <string>
0016 
0017 namespace PhysicsTools {
0018 
0019   /** \class AtomicId
0020  *
0021  * \short Cheap generic unique keyword identifier class.
0022  *
0023  * AtomicId is a lightweight class intended to be used for key values
0024  * e.g. in STL maps. An atomic identifier can be transparently constructed
0025  * from and converted back to a string, but an instance of AtomicId
0026  * does not occupy and additional memory and the comparator operators are
0027  * very cheap. An AtomicId instance requires the size of a pointer and is
0028  * therefore suited for direct inlining.
0029  *
0030  ************************************************************/
0031   class AtomicId {
0032   public:
0033     inline AtomicId() throw() : string(nullptr) {}
0034     inline AtomicId(const AtomicId &orig) throw() : string(orig.string) {}
0035     /// constructs an AtomicId from a C string
0036     inline AtomicId(const char *arg) throw() : string(lookup(arg)) {}
0037     /// constructs an AtomicId from a STL string
0038     inline AtomicId(const std::string &arg) throw() : string(lookup(arg.c_str())) {}
0039     inline ~AtomicId() throw() {}
0040 
0041     inline AtomicId &operator=(const AtomicId &orig) throw() {
0042       string = orig.string;
0043       return *this;
0044     }
0045 
0046     /// implicit cast to a C string
0047     inline operator const char *() const throw() { return string; }
0048 
0049     /// null value check operator
0050     inline operator bool() const throw() { return string != nullptr; }
0051 
0052     /// implicit cast to a STL string
0053     inline operator std::string() const throw() { return std::string(string); }
0054 
0055     inline bool operator==(const AtomicId &second) const throw() { return string == second.string; }
0056     inline bool operator!=(const AtomicId &second) const throw() { return string != second.string; }
0057     inline bool operator<(const AtomicId &second) const throw() { return string < second.string; }
0058     inline bool operator<=(const AtomicId &second) const throw() { return string <= second.string; }
0059     inline bool operator>(const AtomicId &second) const throw() { return string > second.string; }
0060     inline bool operator>=(const AtomicId &second) const throw() { return string >= second.string; }
0061 
0062   private:
0063     static AtomicId build(const char *arg) throw() {
0064       AtomicId q;
0065       q.string = arg;
0066       return q;
0067     }
0068     static const char *lookup(const char *arg) throw();
0069 
0070     const char *string;
0071   };
0072 
0073   /// STL streaming operator
0074   inline std::ostream &operator<<(std::ostream &os, const PhysicsTools::AtomicId &id) { return os << (const char *)id; }
0075 
0076 }  // namespace PhysicsTools
0077 
0078 #endif  // PhysicsTools_MVAComputer_AtomicId_h