Changeset 25934 in webkit for trunk/JavaScriptCore/wtf/OwnPtr.h


Ignore:
Timestamp:
Oct 2, 2007, 1:49:13 PM (18 years ago)
Author:
darin
Message:

JavaScriptCore:

Reviewed by Adam.

  • add support for GDI objects to OwnPtr; I plan to use this to fix some GDI handle leaks
  • kjs/grammar.y: Change parser to avoid macros that conflict with macros defined in Windows system headers: THIS, DELETE, VOID, IN, and CONST. This is needed because OwnPtr.h will now include <windows.h>.
  • kjs/keywords.table: Ditto.
  • wtf/OwnPtr.h: For PLATFORM(WIN), add support so that OwnPtr can be a GDI handle, and it will call DeleteObject. Also change to use the RemovePointer technique used by RetainPtr, so you can say OwnPtr<HBITMAP> rather than having to pass in the type pointed to by HBITMAP.
  • wtf/OwnPtrWin.cpp: Added. (WebCore::deleteOwnedPtr): Put this in a separate file so that we don't have to include <windows.h> in OwnPtr.h.

WebCore:

Reviewed by Adam.

  • add support for GDI objects to OwnPtr; I plan to use this to fix some GDI handle leaks
  • css/CSSGrammar.y: Change parser to avoid macro that conflicts with macro defined in Windows system headers: FLOAT. This is needed because OwnPtr.h will now include <windows.h>.
  • css/CSSParser.cpp: (WebCore::CSSParser::lex): Ditto.
  • css/tokenizer.flex: Ditto.
  • platform/win/CursorWin.cpp: (WebCore::Cursor::Cursor): Use OwnPtr for a few HBITMAP objects as a test case.
  • platform/win/COMPtr.h: (COMPtr::COMPtr): Fix so that we can use Query with other COMPtr. Before this would not compile if it was ever instantiated.

win:

Reviewed by Adam.

  • started using the new OwnPtr everywhere we do DeleteObject
  • WebNodeHighlight.cpp: (WebNodeHighlight::updateWindow):
  • WebView.cpp: (WebView::WebView): (WebView::deleteBackingStore): (WebView::ensureBackingStore): (WebView::addToDirtyRegion): (WebView::scrollBackingStore): (WebView::updateBackingStore): (WebView::paint): (WebView::paintIntoBackingStore): (WebView::paintIntoWindow):
  • WebView.h:

Also set svn:eol-style on all .vcproj to CRLF.

Also added svn:ignore of *.user for WebKitInitializer.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/wtf/OwnPtr.h

    r17437 r25934  
    11// -*- mode: c++; c-basic-offset: 4 -*-
    22/*
    3  *  Copyright (C) 2006 Apple Computer, Inc.
     3 *  Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
    44 *
    55 *  This library is free software; you can redistribute it and/or
     
    2727#include <wtf/Noncopyable.h>
    2828
     29#if PLATFORM(WIN)
     30
     31typedef struct HBITMAP__* HBITMAP;
     32typedef struct HBRUSH__* HBRUSH;
     33typedef struct HFONT__* HFONT;
     34typedef struct HPALETTE__* HPALETTE;
     35typedef struct HPEN__* HPEN;
     36typedef struct HRGN__* HRGN;
     37
     38#endif
     39
    2940namespace WTF {
     41
     42    // Unlike most of our smart pointers, OwnPtr can take either the pointer type or the pointed-to type.
     43
     44    // FIXME: Share a single RemovePointer class template with RetainPtr.
     45    template <typename T> struct OwnPtrRemovePointer { typedef T type; };
     46    template <typename T> struct OwnPtrRemovePointer<T*> { typedef T type; };
     47
     48    template <typename T> inline void deleteOwnedPtr(T* ptr)
     49    {
     50        typedef char known[sizeof(T) ? 1 : -1];
     51        if (sizeof(known))
     52            delete ptr;
     53    }
     54
     55#if PLATFORM(WIN)
     56    void deleteOwnedPtr(HBITMAP);
     57    void deleteOwnedPtr(HBRUSH);
     58    void deleteOwnedPtr(HFONT);
     59    void deleteOwnedPtr(HPALETTE);
     60    void deleteOwnedPtr(HPEN);
     61    void deleteOwnedPtr(HRGN);
     62#endif
    3063
    3164    template <typename T> class OwnPtr : Noncopyable {
    3265    public:
    33         explicit OwnPtr(T* ptr = 0) : m_ptr(ptr) { }
    34         ~OwnPtr() { safeDelete(); }
     66        typedef typename OwnPtrRemovePointer<T>::type ValueType;
     67        typedef ValueType* PtrType;
    3568
    36         T* get() const { return m_ptr; }
    37         T* release() { T* ptr = m_ptr; m_ptr = 0; return ptr; }
     69        explicit OwnPtr(PtrType ptr = 0) : m_ptr(ptr) { }
     70        ~OwnPtr() { deleteOwnedPtr(m_ptr); }
    3871
    39         void set(T* ptr) { ASSERT(!ptr || m_ptr != ptr); safeDelete(); m_ptr = ptr; }
    40         void clear() { safeDelete(); m_ptr = 0; }
     72        PtrType get() const { return m_ptr; }
     73        PtrType release() { PtrType ptr = m_ptr; m_ptr = 0; return ptr; }
    4174
    42         T& operator*() const { ASSERT(m_ptr); return *m_ptr; }
    43         T* operator->() const { ASSERT(m_ptr); return m_ptr; }
     75        void set(PtrType ptr) { ASSERT(!ptr || m_ptr != ptr); deleteOwnedPtr(m_ptr); m_ptr = ptr; }
     76        void clear() { deleteOwnedPtr(m_ptr); m_ptr = 0; }
     77
     78        ValueType& operator*() const { ASSERT(m_ptr); return *m_ptr; }
     79        PtrType operator->() const { ASSERT(m_ptr); return m_ptr; }
    4480
    4581        bool operator!() const { return !m_ptr; }
    4682
    4783        // This conversion operator allows implicit conversion to bool but not to other integer types.
    48         typedef T* (OwnPtr::*UnspecifiedBoolType)() const;
     84        typedef PtrType (OwnPtr::*UnspecifiedBoolType)() const;
    4985        operator UnspecifiedBoolType() const { return m_ptr ? &OwnPtr::get : 0; }
    5086
     
    5288
    5389    private:
    54         void safeDelete() { typedef char known[sizeof(T) ? 1 : -1]; if (sizeof(known)) delete m_ptr; }
    55 
    56         T* m_ptr;
     90        PtrType m_ptr;
    5791    };
    5892   
    5993    template <typename T> inline void swap(OwnPtr<T>& a, OwnPtr<T>& b) { a.swap(b); }
    6094
    61     template <typename T> inline T* getPtr(const OwnPtr<T>& p)
     95    template <typename T> inline typename OwnPtr<T>::PtrType getPtr(const OwnPtr<T>& p)
    6296    {
    6397        return p.get();
Note: See TracChangeset for help on using the changeset viewer.