CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/FWCore/Framework/src/DataKey.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:     Framework
00004 // Class  :     DataKey
00005 // 
00006 // Implementation:
00007 //     <Notes on implementation>
00008 //
00009 // Author:      Chris Jones
00010 // Created:     Thu Mar 31 14:31:13 EST 2005
00011 //
00012 
00013 // system include files
00014 #include <memory>
00015 #include <cstring>
00016 
00017 // user include files
00018 #include "FWCore/Framework/interface/DataKey.h"
00019 
00020 
00021 //
00022 // constants, enums and typedefs
00023 //
00024 
00025 static const char kBlank[] = {'\0'};
00026 
00027 namespace edm {
00028    namespace eventsetup {
00029 //
00030 // static data member definitions
00031 //
00032 
00033 //
00034 // constructors and destructor
00035 //
00036 DataKey::DataKey(): type_(), name_(), ownMemory_(false)
00037 {
00038 }
00039 
00040 // DataKey::DataKey(const DataKey& rhs)
00041 // {
00042 //    // do actual copying here;
00043 // }
00044 
00045 //DataKey::~DataKey()
00046 //{
00047 //}
00048 
00049 //
00050 // assignment operators
00051 //
00052 const DataKey& DataKey::operator=(const DataKey& rhs)
00053 {
00054    //An exception safe implementation is
00055    DataKey temp(rhs);
00056    swap(temp);
00057 
00058    return *this;
00059 }
00060 
00061 //
00062 // member functions
00063 //
00064 void
00065 DataKey::swap(DataKey& iOther)
00066 {
00067    std::swap(ownMemory_, iOther.ownMemory_);
00068    // unqualified swap is used for user defined classes.
00069    // The using directive is needed so that std::swap will be used if there is no other matching swap.
00070    using std::swap;
00071    swap(type_, iOther.type_);
00072    swap(name_, iOther.name_);
00073 }
00074 
00075       namespace {
00076          //used for exception safety
00077          class ArrayHolder {
00078          public:
00079             ArrayHolder():ptr_(0){}
00080             
00081             void swap(ArrayHolder& iOther) {
00082                const char* t = iOther.ptr_;
00083                iOther.ptr_ = ptr_;
00084                ptr_ = t;
00085             }
00086             ArrayHolder(const char* iPtr): ptr_(iPtr) {}
00087             ~ArrayHolder() { delete [] ptr_; }
00088             void release() { ptr_=0;}
00089          private:
00090             const char* ptr_;
00091          };
00092       }
00093 void 
00094 DataKey::makeCopyOfMemory()
00095 {
00096    //empty string is the most common case, so handle it special
00097    
00098    char* pName = const_cast<char*>(kBlank);
00099    //NOTE: if in the future additional tags are added then 
00100    // I should make sure that pName gets deleted in the case
00101    // where an exception is thrown
00102    ArrayHolder pNameHolder;
00103    if(kBlank[0] != name().value()[0]) {
00104       pName = new char[ std::strlen(name().value()) + 1];
00105       ArrayHolder t(pName);
00106       pNameHolder.swap(t);
00107       std::strcpy(pName, name().value());
00108    }
00109    name_ = NameTag(pName);
00110    ownMemory_ = true;
00111    pNameHolder.release();
00112 }
00113 
00114 void
00115 DataKey::deleteMemory()
00116 {
00117    if(kBlank[0] != name().value()[0]) {
00118       delete [] const_cast<char*>(name().value());
00119    }
00120 }
00121 
00122 //
00123 // const member functions
00124 //
00125 bool
00126 DataKey::operator==(const DataKey& iRHS) const 
00127 {
00128    return ((type_ == iRHS.type_) &&
00129             (name_ == iRHS.name_));
00130 }
00131 
00132 bool
00133 DataKey::operator<(const DataKey& iRHS) const 
00134 {
00135    return (type_ < iRHS.type_) ||
00136    ((type_ == iRHS.type_) && (name_ < iRHS.name_));
00137 /*
00138    if(type_ < iRHS.type_) {
00139       return true;
00140    } else if (type_ == iRHS.type_) {
00141       if(name_ < iRHS.name_) {
00142          return true;
00143    }
00144    return false;
00145       */
00146 }
00147 
00148 //
00149 // static member functions
00150 //
00151    }
00152 }