CMS 3D CMS Logo

DataKey.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: Framework
4 // Class : DataKey
5 //
6 // Implementation:
7 // <Notes on implementation>
8 //
9 // Author: Chris Jones
10 // Created: Thu Mar 31 14:31:13 EST 2005
11 //
12 
13 // system include files
14 #include <memory>
15 #include <cstring>
16 
17 // user include files
19 
20 //
21 // constants, enums and typedefs
22 //
23 
24 namespace {
25  constexpr char kBlank[] = {'\0'};
26 }
27 
28 namespace edm::eventsetup {
29 
30  DataKey::DataKey() = default;
31 
33  //An exception safe implementation is
34  DataKey temp(rhs);
35  swap(temp);
36 
37  return *this;
38  }
39 
41  //An exception safe implementation is
42  DataKey temp(std::move(rhs));
43  swap(temp);
44 
45  return *this;
46  }
47 
48  //
49  // member functions
50  //
51  void DataKey::swap(DataKey& iOther) {
53  // unqualified swap is used for user defined classes.
54  // The using directive is needed so that std::swap will be used if there is no other matching swap.
55  using std::swap;
56  swap(type_, iOther.type_);
57  swap(name_, iOther.name_);
58  }
59 
60  namespace {
61  //used for exception safety
62  class ArrayHolder {
63  public:
64  ArrayHolder() = default;
65 
66  void swap(ArrayHolder& iOther) {
67  const char* t = iOther.ptr_;
68  iOther.ptr_ = ptr_;
69  ptr_ = t;
70  }
71  ArrayHolder(const char* iPtr) : ptr_(iPtr) {}
72  ~ArrayHolder() { delete[] ptr_; }
73  void release() { ptr_ = nullptr; }
74 
75  private:
76  const char* ptr_{nullptr};
77  };
78  } // namespace
79 
81  //empty string is the most common case, so handle it special
82 
83  char* pName = const_cast<char*>(kBlank);
84  //NOTE: if in the future additional tags are added then
85  // I should make sure that pName gets deleted in the case
86  // where an exception is thrown
87  ArrayHolder pNameHolder;
88  if (kBlank[0] != name().value()[0]) {
89  size_t const nBytes = std::strlen(name().value()) + 1;
90  pName = new char[nBytes];
91  ArrayHolder t(pName);
92  pNameHolder.swap(t);
93  std::strncpy(pName, name().value(), nBytes);
94  }
95  name_ = NameTag(pName);
96  ownMemory_ = true;
97  pNameHolder.release();
98  }
99 
101  if (kBlank[0] != name().value()[0]) {
102  delete[] const_cast<char*>(name().value());
103  }
104  }
105 
106  //
107  // const member functions
108  //
109  bool DataKey::operator==(const DataKey& iRHS) const { return ((type_ == iRHS.type_) && (name_ == iRHS.name_)); }
110 
111  bool DataKey::operator<(const DataKey& iRHS) const {
112  return (type_ < iRHS.type_) || ((type_ == iRHS.type_) && (name_ < iRHS.name_));
113  }
114 } // namespace edm::eventsetup
const char * ptr_
Definition: DataKey.cc:76
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
void swap(DataKey &a, DataKey &b)
Definition: DataKey.h:85
DataKey & operator=(const DataKey &)
Definition: DataKey.cc:32
bool operator==(const DataKey &iRHS) const
Definition: DataKey.cc:109
friend void swap(DataKey &, DataKey &)
Definition: DataKey.h:85
const NameTag & name() const
Definition: DataKey.h:53
bool operator<(const DataKey &iRHS) const
Definition: DataKey.cc:111
def move(src, dest)
Definition: eostools.py:511
const char * value() const
Definition: DataKeyTags.h:39