The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 4,262 other subscribers

Hidden Features in Delphi related topics (from StackOverflow, until the diamond moderators kill these too)

Posted by jpluimers on 2014/02/20

There are (soon probably “were”) a few very interesting Q&A threads on Stack Overflow in the “Hidden Secrets of” series on Delphi related topics.

I sort of can get (but don’t agree: there is a very good voting system to de-emphasize material that is not useful, but who am I to argue with the minority of “the world is black and white, we just follow the rules” diamondss) that these get closed, but cannot get that very useful material gets deleted for anyone with less than 10-thousand reputation.

–jeroen

@Jeroen & David, I’ve deleted my off-topic comments from here. Could you do the same, please ? I’ve also asked moderators to delete my meta question as it seems the users there are not even humans. Never mind. Stack Overflow is not what it was few years ago as I observe. It’s getting worse. Another piece to this mosaic was running the portuguese version of Stack Overflow ideal for cross posting between the sites.

8 Responses to “Hidden Features in Delphi related topics (from StackOverflow, until the diamond moderators kill these too)”

  1. Malom said

    An observation that I’ve never seen elsewhere is OBJECT (not CLASS) support for interfaces.
    Here is a stupid but working example code for that.

    unit uObjectInterface;

    interface

    type

    TInterfacedBase = object // Not CLASS
    private
    FInterfaceTable: Pointer;
    FRefCount: Integer;
    public
    constructor Init;
    destructor Done;
    function GetInterface: IInterface;
    function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
    function _AddRef: Integer; stdcall;
    function _Release: Integer; stdcall;
    end;

    ITest = interface
    function Foo: string;
    end;

    // TTest = object(ITest) // Must have an ancestor OBJECT type or gives a syntax error
    TTest = object(TInterfacedBase, ITest)
    public
    constructor Init;
    function Foo: string;
    end;

    implementation

    uses Windows;

    { TInterfacedBase }

    function TInterfacedBase._AddRef: Integer;
    begin
    Result := InterlockedIncrement(FRefCount);
    end;

    function TInterfacedBase._Release: Integer;
    begin
    Result := InterlockedDecrement(FRefCount);
    if Result = 0 then
    Done;
    end;

    function TInterfacedBase.QueryInterface(const IID: TGUID; out Obj): HResult;
    begin
    Result := E_NOINTERFACE;
    end;

    destructor TInterfacedBase.Done;
    begin
    // Nothing to do since there was no memory allocation here. It is a stack object.
    end;

    constructor TInterfacedBase.Init;
    const
    cInterfaceTable: array[0..2] of Pointer = (
    @TInterfacedBase.QueryInterface,
    @TInterfacedBase._AddRef,
    @TInterfacedBase._Release
    );
    begin
    FInterfaceTable := @cInterfaceTable;
    FRefCount := 0;
    end;

    function TInterfacedBase.GetInterface: IInterface;
    begin
    Result := IInterface(@FInterfaceTable);
    end;

    { TTest }

    function TTest.Foo: string;
    begin
    Result := ‘TTest.Foo’;
    end;

    procedure ObjectInterfaceTest;
    var
    TestObj: TTest;
    TestIntf: ITest;
    S: string;
    begin
    TestObj.Init;
    TestIntf := ITest(TestObj.GetInterface);
    S := TestIntf.Foo;
    Assert(S = TestObj.Foo);
    TestObj.Done;
    end;

    constructor TTest.Init;
    const
    cInterfaceTable: array[0..2+1] of Pointer = (
    @TInterfacedBase.QueryInterface,
    @TInterfacedBase._AddRef,
    @TInterfacedBase._Release,
    @TTest.Foo
    );
    begin
    FInterfaceTable := @cInterfaceTable;
    FRefCount := 0;
    end;

    initialization

    ObjectInterfaceTest;

    end.

  2. Mike P said

    I am less excited about SO than in the beginning because of how quick moderators are to close questions. I use it less as well.

  3. David M said

    Jeroen, I can see it’s closed – but where it is flagged for deletion? I agree deleting would remove a great resource, but when reading the question page I couldn’t see anything about deleting it.

  4. The hidden questions from StackOverflow :)

  5. […] copy editing. Original question by Johan and others on May 19 2011 at 18:34. Post inspired by Jeroen W. Pluimers’ […]

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.