From: Amos Jeffries Date: Sat, 6 Aug 2016 04:49:55 +0000 (+1200) Subject: Fix clang errors with global InstanceId initialization X-Git-Tag: SQUID_4_0_14~53 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=f2e4148049d39c62a156b933805d5fe7e1653fd2;p=thirdparty%2Fsquid.git Fix clang errors with global InstanceId initialization Move static member Last into change() method to avoid initialization order errors when a caller uses a global InstanceId object before the library instantiating its template is initialized. Also, convert the Prefix static member to a constant string in a prefix() method to avoid the same issue there. --- diff --git a/src/adaptation/icap/Xaction.cc b/src/adaptation/icap/Xaction.cc index fca9f8507b..180bf04574 100644 --- a/src/adaptation/icap/Xaction.cc +++ b/src/adaptation/icap/Xaction.cc @@ -669,7 +669,7 @@ const char *Adaptation::Icap::Xaction::status() const fillPendingStatus(buf); buf.append("/", 1); fillDoneStatus(buf); - buf.appendf(" %s%u]", id.Prefix, id.value); + buf.appendf(" %s%u]", id.prefix(), id.value); buf.terminate(); return buf.content(); diff --git a/src/base/AsyncJob.cc b/src/base/AsyncJob.cc index 96f110183a..b7c0e18f27 100644 --- a/src/base/AsyncJob.cc +++ b/src/base/AsyncJob.cc @@ -165,7 +165,7 @@ const char *AsyncJob::status() const if (stopReason != NULL) { buf.appendf("Stopped, reason:%s", stopReason); } - buf.appendf(" %s%u]", id.Prefix, id.value); + buf.appendf(" %s%u]", id.prefix(), id.value); buf.terminate(); return buf.content(); diff --git a/src/base/InstanceId.h b/src/base/InstanceId.h index c64f9df13d..2e2c457d92 100644 --- a/src/base/InstanceId.h +++ b/src/base/InstanceId.h @@ -25,35 +25,41 @@ class InstanceId public: typedef unsigned int Value; ///< id storage type; \todo: parameterize? - InstanceId(): value(++Last ? Last : ++Last) {} + InstanceId(): value(0) {change();} operator Value() const { return value; } bool operator ==(const InstanceId &o) const { return value == o.value; } bool operator !=(const InstanceId &o) const { return !(*this == o); } - void change() {value = ++Last ? Last : ++Last;} + void change(); - /// prints Prefix followed by ID value; \todo: use HEX for value printing? + /// prints class-pecific prefix followed by ID value; \todo: use HEX for value printing? std::ostream &print(std::ostream &os) const; + /// returns the class-pecific prefix + const char * const prefix() const; + public: - static const char *Prefix; ///< Class shorthand string for debugging Value value; ///< instance identifier private: InstanceId(const InstanceId& right); ///< not implemented; IDs are unique InstanceId& operator=(const InstanceId &right); ///< not implemented - -private: - static Value Last; ///< the last used ID value }; /// convenience macro to instantiate Class-specific stuff in .cc files -#define InstanceIdDefinitions(Class, prefix) \ - template<> const char *InstanceId::Prefix = prefix; \ - template<> InstanceId::Value InstanceId::Last = 0; \ +#define InstanceIdDefinitions(Class, pfx) \ + template<> const char * const \ + InstanceId::prefix() const { \ + return pfx; \ + } \ template<> std::ostream & \ InstanceId::print(std::ostream &os) const { \ - return os << Prefix << value; \ + return os << pfx << value; \ + } \ + template<> void \ + InstanceId::change() { \ + static InstanceId::Value Last = 0; \ + value = ++Last ? Last : ++Last; \ } /// print the id diff --git a/src/security/PeerConnector.cc b/src/security/PeerConnector.cc index 5d3022636d..cc4c608d8c 100644 --- a/src/security/PeerConnector.cc +++ b/src/security/PeerConnector.cc @@ -534,7 +534,7 @@ Security::PeerConnector::status() const } if (serverConn != NULL) buf.appendf(" FD %d", serverConn->fd); - buf.appendf(" %s%u]", id.Prefix, id.value); + buf.appendf(" %s%u]", id.prefix(), id.value); buf.terminate(); return buf.content();