Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:12:08

0001 // -*- C++ -*-
0002 //
0003 // Package:     Framework
0004 // Class  :     DataKey
0005 //
0006 // Implementation:
0007 //     <Notes on implementation>
0008 //
0009 // Author:      Chris Jones
0010 // Created:     Thu Mar 31 14:31:13 EST 2005
0011 //
0012 
0013 // system include files
0014 #include <memory>
0015 #include <cstring>
0016 
0017 // user include files
0018 #include "FWCore/Framework/interface/DataKey.h"
0019 
0020 //
0021 // constants, enums and typedefs
0022 //
0023 
0024 namespace {
0025   constexpr char kBlank[] = {'\0'};
0026 }
0027 
0028 namespace edm::eventsetup {
0029 
0030   DataKey::DataKey() = default;
0031 
0032   DataKey& DataKey::operator=(const DataKey& rhs) {
0033     //An exception safe implementation is
0034     DataKey temp(rhs);
0035     swap(temp);
0036 
0037     return *this;
0038   }
0039 
0040   DataKey& DataKey::operator=(DataKey&& rhs) {
0041     //An exception safe implementation is
0042     DataKey temp(std::move(rhs));
0043     swap(temp);
0044 
0045     return *this;
0046   }
0047 
0048   //
0049   // member functions
0050   //
0051   void DataKey::swap(DataKey& iOther) {
0052     std::swap(ownMemory_, iOther.ownMemory_);
0053     // unqualified swap is used for user defined classes.
0054     // The using directive is needed so that std::swap will be used if there is no other matching swap.
0055     using std::swap;
0056     swap(type_, iOther.type_);
0057     swap(name_, iOther.name_);
0058   }
0059 
0060   namespace {
0061     //used for exception safety
0062     class ArrayHolder {
0063     public:
0064       ArrayHolder() = default;
0065 
0066       void swap(ArrayHolder& iOther) {
0067         const char* t = iOther.ptr_;
0068         iOther.ptr_ = ptr_;
0069         ptr_ = t;
0070       }
0071       ArrayHolder(const char* iPtr) : ptr_(iPtr) {}
0072       ~ArrayHolder() { delete[] ptr_; }
0073       void release() { ptr_ = nullptr; }
0074 
0075     private:
0076       const char* ptr_{nullptr};
0077     };
0078   }  // namespace
0079 
0080   void DataKey::makeCopyOfMemory() {
0081     //empty string is the most common case, so handle it special
0082 
0083     char* pName = const_cast<char*>(kBlank);
0084     //NOTE: if in the future additional tags are added then
0085     // I should make sure that pName gets deleted in the case
0086     // where an exception is thrown
0087     ArrayHolder pNameHolder;
0088     if (kBlank[0] != name().value()[0]) {
0089       size_t const nBytes = std::strlen(name().value()) + 1;
0090       pName = new char[nBytes];
0091       ArrayHolder t(pName);
0092       pNameHolder.swap(t);
0093       std::strncpy(pName, name().value(), nBytes);
0094     }
0095     name_ = NameTag(pName);
0096     ownMemory_ = true;
0097     pNameHolder.release();
0098   }
0099 
0100   void DataKey::deleteMemory() {
0101     if (kBlank[0] != name().value()[0]) {
0102       delete[] const_cast<char*>(name().value());
0103     }
0104   }
0105 
0106   //
0107   // const member functions
0108   //
0109   bool DataKey::operator==(const DataKey& iRHS) const { return ((type_ == iRHS.type_) && (name_ == iRHS.name_)); }
0110 
0111   bool DataKey::operator<(const DataKey& iRHS) const {
0112     return (type_ < iRHS.type_) || ((type_ == iRHS.type_) && (name_ < iRHS.name_));
0113   }
0114 }  // namespace edm::eventsetup