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 //
22 // constants, enums and typedefs
23 //
24 
25 namespace {
26 constexpr char kBlank[] = {'\0'};
27 }
28 
29 namespace edm::eventsetup {
30 
31 DataKey::DataKey() = default;
32 
34 {
35  //An exception safe implementation is
36  DataKey temp(rhs);
37  swap(temp);
38 
39  return *this;
40 }
41 
42 //
43 // member functions
44 //
45 void
47 {
49  // unqualified swap is used for user defined classes.
50  // The using directive is needed so that std::swap will be used if there is no other matching swap.
51  using std::swap;
52  swap(type_, iOther.type_);
53  swap(name_, iOther.name_);
54 }
55 
56  namespace {
57  //used for exception safety
58  class ArrayHolder {
59  public:
60  ArrayHolder() = default;
61 
62  void swap(ArrayHolder& iOther) {
63  const char* t = iOther.ptr_;
64  iOther.ptr_ = ptr_;
65  ptr_ = t;
66  }
67  ArrayHolder(const char* iPtr): ptr_(iPtr) {}
68  ~ArrayHolder() { delete [] ptr_; }
69  void release() { ptr_=nullptr;}
70  private:
71  const char* ptr_{nullptr};
72  };
73  }
74 
75 void
77 {
78  //empty string is the most common case, so handle it special
79 
80  char* pName = const_cast<char*>(kBlank);
81  //NOTE: if in the future additional tags are added then
82  // I should make sure that pName gets deleted in the case
83  // where an exception is thrown
84  ArrayHolder pNameHolder;
85  if(kBlank[0] != name().value()[0]) {
86  size_t const nBytes = std::strlen(name().value()) + 1;
87  pName = new char[nBytes];
88  ArrayHolder t(pName);
89  pNameHolder.swap(t);
90  std::strncpy(pName, name().value(), nBytes);
91  }
92  name_ = NameTag(pName);
93  ownMemory_ = true;
94  pNameHolder.release();
95 }
96 
97 void
99 {
100  if(kBlank[0] != name().value()[0]) {
101  delete [] const_cast<char*>(name().value());
102  }
103 }
104 
105 //
106 // const member functions
107 //
108 bool
109 DataKey::operator==(const DataKey& iRHS) const
110 {
111  return ((type_ == iRHS.type_) &&
112  (name_ == iRHS.name_));
113 }
114 
115 bool
116 DataKey::operator<(const DataKey& iRHS) const
117 {
118  return (type_ < iRHS.type_) ||
119  ((type_ == iRHS.type_) && (name_ < iRHS.name_));
120 }
121 }
const char * ptr_
Definition: DataKey.cc:71
#define constexpr
bool operator<(const DataKey &iRHS) const
Definition: DataKey.cc:116
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
DataKey & operator=(const DataKey &)
Definition: DataKey.cc:33
bool operator==(const DataKey &iRHS) const
Definition: DataKey.cc:109
friend void swap(DataKey &, DataKey &)
Definition: DataKey.h:98
const NameTag & name() const
Definition: DataKey.h:65
const char * value() const
Definition: DataKeyTags.h:39