Skip to content

Commit 6406cd8

Browse files
authored
Merge pull request #48140 from Dr15Jones/constCastDataKey
Removed const_cast in DataKey
2 parents 8b01c18 + 6233d1c commit 6406cd8

File tree

1 file changed

+6
-30
lines changed

1 file changed

+6
-30
lines changed

FWCore/Framework/src/DataKey.cc

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -57,40 +57,16 @@ namespace edm::eventsetup {
5757
swap(name_, iOther.name_);
5858
}
5959

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-
8060
void DataKey::makeCopyOfMemory() {
8161
//empty string is the most common case, so handle it special
8262

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;
63+
char const* pName = kBlank;
64+
std::unique_ptr<char[]> pNameHolder;
8865
if (kBlank[0] != name().value()[0]) {
8966
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);
67+
pNameHolder.reset(new char[nBytes]);
68+
pName = pNameHolder.get();
69+
std::strncpy(pNameHolder.get(), name().value(), nBytes);
9470
}
9571
name_ = NameTag(pName);
9672
ownMemory_ = true;
@@ -99,7 +75,7 @@ namespace edm::eventsetup {
9975

10076
void DataKey::deleteMemory() {
10177
if (kBlank[0] != name().value()[0]) {
102-
delete[] const_cast<char*>(name().value());
78+
delete[] (name().value());
10379
}
10480
}
10581

0 commit comments

Comments
 (0)