Mercurial > hg > orthanc-stone
comparison Framework/Messages/IObserver.h @ 1048:f6be9412e42a
cleaning up IObservable.h
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 10 Oct 2019 14:11:52 +0200 |
parents | 92a043b8e431 |
children | e713f1a99861 ca2058bd74ef |
comparison
equal
deleted
inserted
replaced
1047:efc5b62b9539 | 1048:f6be9412e42a |
---|---|
20 | 20 |
21 | 21 |
22 #pragma once | 22 #pragma once |
23 | 23 |
24 #include "MessageBroker.h" | 24 #include "MessageBroker.h" |
25 #include "IMessage.h" | |
26 | |
27 #include <Core/Toolbox.h> | |
28 | 25 |
29 namespace OrthancStone | 26 namespace OrthancStone |
30 { | 27 { |
31 class IObserver : public boost::noncopyable | 28 class IObserver : public boost::noncopyable |
32 { | 29 { |
33 private: | 30 private: |
34 MessageBroker& broker_; | 31 MessageBroker& broker_; |
35 // the following is a UUID that is used to disambiguate different observers | 32 // the following is a UUID that is used to disambiguate different observers |
36 // that may have the same address | 33 // that may have the same address |
37 char fingerprint_[37]; | 34 char fingerprint_[37]; |
35 | |
38 public: | 36 public: |
39 IObserver(MessageBroker& broker) | 37 IObserver(MessageBroker& broker); |
40 : broker_(broker) | |
41 , fingerprint_() | |
42 { | |
43 // we store the fingerprint_ as a char array to avoid problems when | |
44 // reading it in a deceased object. | |
45 // remember this is panic-level code to track zombie object usage | |
46 std::string fingerprint = Orthanc::Toolbox::GenerateUuid(); | |
47 const char* fingerprintRaw = fingerprint.c_str(); | |
48 memcpy(fingerprint_, fingerprintRaw, 37); | |
49 broker_.Register(*this); | |
50 } | |
51 | 38 |
52 virtual ~IObserver() | 39 virtual ~IObserver(); |
53 { | |
54 try | |
55 { | |
56 LOG(TRACE) << "IObserver(" << std::hex << this << std::dec << ")::~IObserver : fingerprint_ == " << fingerprint_; | |
57 const char* deadMarker = "deadbeef-dead-dead-0000-0000deadbeef"; | |
58 ORTHANC_ASSERT(strlen(deadMarker) == 36); | |
59 memcpy(fingerprint_, deadMarker, 37); | |
60 broker_.Unregister(*this); | |
61 } | |
62 catch (const Orthanc::OrthancException& e) | |
63 { | |
64 if (e.HasDetails()) | |
65 { | |
66 LOG(ERROR) << "OrthancException in ~IObserver: " << e.What() << " Details: " << e.GetDetails(); | |
67 } | |
68 else | |
69 { | |
70 LOG(ERROR) << "OrthancException in ~IObserver: " << e.What(); | |
71 } | |
72 } | |
73 catch (const std::exception& e) | |
74 { | |
75 LOG(ERROR) << "std::exception in ~IObserver: " << e.what(); | |
76 } | |
77 catch (...) | |
78 { | |
79 LOG(ERROR) << "Unknown exception in ~IObserver"; | |
80 } | |
81 } | |
82 | 40 |
83 const char* GetFingerprint() const | 41 const char* GetFingerprint() const |
84 { | 42 { |
85 return fingerprint_; | 43 return fingerprint_; |
86 } | 44 } |
87 | 45 |
88 bool DoesFingerprintLookGood() const | 46 bool DoesFingerprintLookGood() const; |
89 { | |
90 for (size_t i = 0; i < 36; ++i) { | |
91 bool ok = false; | |
92 if (fingerprint_[i] >= 'a' && fingerprint_[i] <= 'f') | |
93 ok = true; | |
94 if (fingerprint_[i] >= '0' && fingerprint_[i] <= '9') | |
95 ok = true; | |
96 if (fingerprint_[i] == '-') | |
97 ok = true; | |
98 if (!ok) | |
99 return false; | |
100 } | |
101 return fingerprint_[36] == 0; | |
102 } | |
103 | 47 |
104 MessageBroker& GetBroker() const | 48 MessageBroker& GetBroker() const |
105 { | 49 { |
106 return broker_; | 50 return broker_; |
107 } | 51 } |