D - more or less power than C++?
Andrew Fedoniouk
news at terrainformatica.com
Sat Mar 4 00:13:19 PST 2006
Here is the real life example:
This is C++ wrapper of DOM element
from my HTMLayout SDK:
It wraps HELEMENT which is in fact reference counted
handler of HTML DOM object.
namespace dom
{
class element
{
protected:
HELEMENT he;
void use(HELEMENT h) { he = (HTMLayout_UseElement(h) == HLDOM_OK)? h:
0; }
void unuse() { if(he) HTMLayout_UnuseElement(he); he = 0; }
void set(HELEMENT h) { unuse(); use(h); }
public:
element(): he(0) { }
element(HELEMENT h) { use(h); }
element(const element& e) { use(e.he); }
operator HELEMENT() const { return he; }
~element() { unuse(); }
element& operator = (HELEMENT h) { set(h); return *this; }
element& operator = (const element& e) { set(e.he); return *this; }
....
}
}
Usage of this in C++ (code of DOM element event handler, aka "behavior"):
virtual BOOL on_mouse(HELEMENT he, HELEMENT target,
UINT event_type, POINT pt, UINT mouseButtons, UINT keyboardStates )
{
dom::element el = he;
// 'use' handler - add_ref it to prevent from deletion
if(!el.is_valid())
return false;
dom::element root = el.root(); // get root element (<html>)
dom::element editbox = root.find_first("input[type='text']");
// get first edit box using CSS selector
editbox.text("Hello world!"); // set text to it
.....
}
As you may see it is mission critical here to have ctor/dtors/assignment in
structs.
It is simply not feasible (realisticly) to call manually each time use/unuse
methods for all objects
in the function. Dead corner.
I really don't know how to design lightweight wrapper for HTMLayout for D.
<g>
Any real advices?
Andrew.
More information about the Digitalmars-d
mailing list