CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 static const char kBlank[] = {'\0'};
26 
27 namespace edm {
28  namespace eventsetup {
29 //
30 // static data member definitions
31 //
32 
33 //
34 // constructors and destructor
35 //
36 DataKey::DataKey(): type_(), name_(), ownMemory_(false)
37 {
38 }
39 
40 // DataKey::DataKey(const DataKey& rhs)
41 // {
42 // // do actual copying here;
43 // }
44 
45 //DataKey::~DataKey()
46 //{
47 //}
48 
49 //
50 // assignment operators
51 //
53 {
54  //An exception safe implementation is
55  DataKey temp(rhs);
56  swap(temp);
57 
58  return *this;
59 }
60 
61 //
62 // member functions
63 //
64 void
66 {
68  // unqualified swap is used for user defined classes.
69  // The using directive is needed so that std::swap will be used if there is no other matching swap.
70  using std::swap;
71  swap(type_, iOther.type_);
72  swap(name_, iOther.name_);
73 }
74 
75  namespace {
76  //used for exception safety
77  class ArrayHolder {
78  public:
79  ArrayHolder():ptr_(nullptr){}
80 
81  void swap(ArrayHolder& iOther) {
82  const char* t = iOther.ptr_;
83  iOther.ptr_ = ptr_;
84  ptr_ = t;
85  }
86  ArrayHolder(const char* iPtr): ptr_(iPtr) {}
87  ~ArrayHolder() { delete [] ptr_; }
88  void release() { ptr_=0;}
89  private:
90  const char* ptr_;
91  };
92  }
93 void
95 {
96  //empty string is the most common case, so handle it special
97 
98  char* pName = const_cast<char*>(kBlank);
99  //NOTE: if in the future additional tags are added then
100  // I should make sure that pName gets deleted in the case
101  // where an exception is thrown
102  ArrayHolder pNameHolder;
103  if(kBlank[0] != name().value()[0]) {
104  size_t const nBytes = std::strlen(name().value()) + 1;
105  pName = new char[nBytes];
106  ArrayHolder t(pName);
107  pNameHolder.swap(t);
108  std::strncpy(pName, name().value(), nBytes);
109  }
110  name_ = NameTag(pName);
111  ownMemory_ = true;
112  pNameHolder.release();
113 }
114 
115 void
117 {
118  if(kBlank[0] != name().value()[0]) {
119  delete [] const_cast<char*>(name().value());
120  }
121 }
122 
123 //
124 // const member functions
125 //
126 bool
127 DataKey::operator==(const DataKey& iRHS) const
128 {
129  return ((type_ == iRHS.type_) &&
130  (name_ == iRHS.name_));
131 }
132 
133 bool
134 DataKey::operator<(const DataKey& iRHS) const
135 {
136  return (type_ < iRHS.type_) ||
137  ((type_ == iRHS.type_) && (name_ < iRHS.name_));
138 /*
139  if(type_ < iRHS.type_) {
140  return true;
141  } else if (type_ == iRHS.type_) {
142  if(name_ < iRHS.name_) {
143  return true;
144  }
145  return false;
146  */
147 }
148 
149 //
150 // static member functions
151 //
152  }
153 }
const char * ptr_
Definition: DataKey.cc:90
static const char kBlank[]
Definition: DataKey.cc:25
#define nullptr
bool operator<(const DataKey &iRHS) const
Definition: DataKey.cc:134
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
void swap(DataKey &a, DataKey &b)
Definition: DataKey.h:99
DataKey & operator=(const DataKey &)
Definition: DataKey.cc:52
bool operator==(const DataKey &iRHS) const
Definition: DataKey.cc:127
friend void swap(DataKey &, DataKey &)
Definition: DataKey.h:99
const NameTag & name() const
Definition: DataKey.h:66
const char * value() const
Definition: DataKeyTags.h:39
volatile std::atomic< bool > shutdown_flag false