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_(0){}
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  pName = new char[ std::strlen(name().value()) + 1];
105  ArrayHolder t(pName);
106  pNameHolder.swap(t);
107  std::strcpy(pName, name().value());
108  }
109  name_ = NameTag(pName);
110  ownMemory_ = true;
111  pNameHolder.release();
112 }
113 
114 void
116 {
117  if(kBlank[0] != name().value()[0]) {
118  delete [] const_cast<char*>(name().value());
119  }
120 }
121 
122 //
123 // const member functions
124 //
125 bool
126 DataKey::operator==(const DataKey& iRHS) const
127 {
128  return ((type_ == iRHS.type_) &&
129  (name_ == iRHS.name_));
130 }
131 
132 bool
133 DataKey::operator<(const DataKey& iRHS) const
134 {
135  return (type_ < iRHS.type_) ||
136  ((type_ == iRHS.type_) && (name_ < iRHS.name_));
137 /*
138  if(type_ < iRHS.type_) {
139  return true;
140  } else if (type_ == iRHS.type_) {
141  if(name_ < iRHS.name_) {
142  return true;
143  }
144  return false;
145  */
146 }
147 
148 //
149 // static member functions
150 //
151  }
152 }
const char * ptr_
Definition: DataKey.cc:90
static const char kBlank[]
Definition: DataKey.cc:25
bool operator<(const DataKey &iRHS) const
Definition: DataKey.cc:133
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
void swap(DataKey &a, DataKey &b)
Definition: DataKey.h:100
const DataKey & operator=(const DataKey &)
Definition: DataKey.cc:52
bool operator==(const DataKey &iRHS) const
Definition: DataKey.cc:126
friend void swap(DataKey &, DataKey &)
Definition: DataKey.h:100
const NameTag & name() const
Definition: DataKey.h:67
const char * value() const
Definition: DataKeyTags.h:40