CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_14/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 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       size_t const nBytes = std::strlen(name().value()) + 1;
00105       pName = new char[nBytes];
00106       ArrayHolder t(pName);
00107       pNameHolder.swap(t);
00108       std::strncpy(pName, name().value(), nBytes);
00109    }
00110    name_ = NameTag(pName);
00111    ownMemory_ = true;
00112    pNameHolder.release();
00113 }
00114 
00115 void
00116 DataKey::deleteMemory()
00117 {
00118    if(kBlank[0] != name().value()[0]) {
00119       delete [] const_cast<char*>(name().value());
00120    }
00121 }
00122 
00123 //
00124 // const member functions
00125 //
00126 bool
00127 DataKey::operator==(const DataKey& iRHS) const 
00128 {
00129    return ((type_ == iRHS.type_) &&
00130             (name_ == iRHS.name_));
00131 }
00132 
00133 bool
00134 DataKey::operator<(const DataKey& iRHS) const 
00135 {
00136    return (type_ < iRHS.type_) ||
00137    ((type_ == iRHS.type_) && (name_ < iRHS.name_));
00138 /*
00139    if(type_ < iRHS.type_) {
00140       return true;
00141    } else if (type_ == iRHS.type_) {
00142       if(name_ < iRHS.name_) {
00143          return true;
00144    }
00145    return false;
00146       */
00147 }
00148 
00149 //
00150 // static member functions
00151 //
00152    }
00153 }