]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Added safe HttpMsg pointer wrapper that locks and unlocks the message.
authorAlex Rousskov <rousskov@measurement-factory.com>
Mon, 30 Aug 2010 22:55:02 +0000 (16:55 -0600)
committerAlex Rousskov <rousskov@measurement-factory.com>
Mon, 30 Aug 2010 22:55:02 +0000 (16:55 -0600)
This class will not be needed if we switch to refcounting HttpMsg. Meanwhile,
it allows to pass message pointers in AsyncCalls and avoid auto_ptr<> in
exception-sensitive code.

src/HttpMsg.h

index 3d536bb0294fb49d3c76542df2e03d00a7e85c2b..9c4374851b0c5c626ba87e3ed43f62190dcdf80c 100644 (file)
@@ -167,4 +167,41 @@ SQUIDCEXTERN int httpMsgIsolateHeaders(const char **parse_start, int len, const
 #define HTTPMSGUNLOCK(a) if(a){(a)->_unlock();(a)=NULL;}
 #define HTTPMSGLOCK(a) (a)->_lock()
 
+// TODO: replace HTTPMSGLOCK with general RefCounting and delete this class
+/// safe HttpMsg pointer wrapper that locks and unlocks the message
+template <class Msg>
+class HttpMsgPointerT
+{
+public:
+    HttpMsgPointerT(): msg(NULL) {}
+    explicit HttpMsgPointerT(Msg *m): msg(m) { lock(); }
+    virtual ~HttpMsgPointerT() { unlock(); }
+
+    HttpMsgPointerT(const HttpMsgPointerT &p): msg(p.msg) { lock(); }
+    HttpMsgPointerT &operator =(const HttpMsgPointerT &p)
+        { if (msg != p.msg) { unlock(); msg = p.msg; lock(); } return *this; }
+
+    Msg &operator *() { return *msg; }
+    const Msg &operator *() const { return *msg; }
+    Msg *operator ->() { return msg; }
+    const Msg *operator ->() const { return msg; }
+    operator Msg *() { return msg; }
+    operator const Msg *() const { return msg; }
+    // add more as needed
+
+    void lock() { if (msg) HTTPMSGLOCK(msg); } ///< prevent msg destruction
+    void unlock() { HTTPMSGUNLOCK(msg); } ///< allows/causes msg destruction
+
+private:
+    Msg *msg;
+};
+
+/// convenience wrapper to create HttpMsgPointerT<> object based on msg type
+template <class Msg>
+inline
+HttpMsgPointerT<Msg> HttpMsgPointer(Msg *msg)
+{
+    return HttpMsgPointerT<Msg>(msg);
+}
+
 #endif /* SQUID_HTTPMSG_H */