Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

Yet Another C#-style Delegate Class in Standard C++

, 6 Jan 2006
This article describes a C#-style delegate class completely written in standard C++.
<html>
<head>
<title>Delegate&lt;R (T1, T2, ..., TN)&gt;</title>
<meta http-equiv=Content-Language content=en-us>
<meta http-equiv=Content-Type content="text/html; charset=us-ascii">
<style> <!-- body { font-family: Verdana; font-size: 10pt }
	code { font-family: Lucida Console; font-size: 10pt }
	h3 { color: #0000FF }
	pre { font-family: Lucida Console; font-size: 10pt; border: 0.5pt solid #808080; padding: 6pt; background-color: #F0F0F0 }
	--> 
	</style>
</head>
<body>

<h1>Delegate&lt;R (T1, T2, ..., TN)&gt; Class</h1>
<p>Represents a function object which encapsulates an invocation list, each of which is a C++ 
callable entity (static function, member function or function object).</p>

<h2>Synopsis</h2>
<pre>
<font color="blue">namespace</font> Acf {

<font color="blue">template</font> &lt;<font color="blue">class</font> TSignature&gt;
<font color="blue">class</font> Delegate; <font color=darkgreen>// no body</font>

<font color="blue">template</font> &lt;<font color="blue">class</font> R, <font color="blue">class</font> T1, <font color="blue">class</font> T2, ..., <font color="blue">class</font> TN&gt;
<font color="blue">class</font> Delegate&lt;R (T1, T2, ..., TN)&gt; {
<font color="darkgreen">// Constructor/Destructor</font>
<font color="blue">public</font>:
    Delegate();
    <font color="blue">template</font> &lt;<font color="blue">class</font> TFunctor&gt;
    Delegate(<font color="blue">const</font> TFunctor&amp; f);
    <font color="blue">template</font> &lt;<font color="blue">class</font> TPtr, <font color="blue">class</font> TFunctionPtr&gt;
    Delegate(<FONT color=blue>const</FONT> TPtr&amp; obj, TFunctionPtr mfp);
    Delegate(<font color="blue">const</font> Delegate&amp; d);
    ~Delegate();

<font color=darkgreen>// Properties</font>
<font color="blue">public</font>:
    <font color="blue">bool</font> IsEmpty() <font color="blue">const</font>;
    <font color="blue">bool</font> IsMulticast() <font color="blue">const</font>;

<font color=darkgreen>// Methods</font>
<font color="blue">public</font>:
    <font color="blue">template</font> &lt;<font color="blue">class</font> TFunctor&gt;
    <font color="blue">void</font> Add(<font color="blue">const</font> TFunctor&amp; f);
    <font color="blue">template</font> &lt;<font color="blue">class</font> TPtr, <font color="blue">class</font> TFunctionPtr&gt;
    <font color="blue">void</font> Add(<FONT color=blue>const</FONT> TPtr&amp; obj, TFunctionPtr mfp);

    <font color="blue">template</font> &lt;<font color="blue">class</font> TFunctor&gt;
    <font color="blue">bool</font> Remove(<font color="blue">const</font> TFunctor&amp; f);
    <font color="blue">template</font> &lt;<font color="blue">class</font> TPtr, <font color="blue">class</font> TFunctionPtr&gt;
    <font color="blue">bool</font> Remove(<FONT color=blue>const</FONT> TPtr&amp; obj, TFunctionPtr mfp);

    <font color="blue">void</font> Clear();

<font color=darkgreen>// Operators</font>
<font color="blue">public</font>:
    <font color="blue">operator</font> <font color="blue">bool</font>() <font color="blue">const</font>;
    <font color="blue">bool</font> <font color="blue">operator</font>!() <font color="blue">const</font>;

    <font color="blue">template</font> &lt;<font color="blue">class</font> TFunctor&gt;
    Delegate&amp; <font color="blue">operator</font>=(<font color="blue">const</font> TFunctor&amp; f);
    Delegate&amp; <font color="blue">operator</font>=(<font color="blue">const</font> Delegate&amp; d);

    <font color="blue">template</font> &lt;<font color="blue">class</font> TFunctor&gt;
    Delegate&amp; <font color="blue">operator</font>+=(<font color="blue">const</font> TFunctor&amp; f);

    <font color="blue">template</font> &lt;<font color="blue">class</font> TFunctor&gt;
    <font color="blue">friend</font> Delegate <font color="blue">operator</font>+(<font color="blue">const</font> Delegate&amp; d, <font color="blue">const</font> TFunctor&amp; f);
    <font color="blue">template</font> &lt;<font color="blue">class</font> TFunctor&gt;
    <font color="blue">friend</font> Delegate <font color="blue">operator</font>+(<font color="blue">const</font> TFunctor&amp; f, <font color="blue">const</font> Delegate&amp; d);

    <font color="blue">template</font> &lt;<font color="blue">class</font> TFunctor&gt;
    Delegate&amp; <font color="blue">operator</font>-=(<font color="blue">const</font> TFunctor&amp; f);

    <font color="blue">template</font> &lt;<font color="blue">class</font> TFunctor&gt;
    Delegate <font color="blue">operator</font>-(<font color="blue">const</font> TFunctor&amp; f) <FONT color=blue>const</FONT>;

    <font color="blue">template</font> &lt;<font color="blue">class</font> TFunctor&gt;
    <font color="blue">friend</font> <font color="blue">bool</font> <font color="blue">operator</font>==(<font color="blue">const</font> Delegate&amp; d, <font color="blue">const</font> TFunctor&amp; f);
    <font color="blue">template</font> &lt;<font color="blue">class</font> TFunctor&gt;
    <font color="blue">friend</font> <font color="blue">bool</font> <font color="blue">operator</font>==(<font color="blue">const</font> TFunctor&amp; f, <font color="blue">const</font> Delegate&amp; d);

    <font color="blue">template</font> &lt;<font color="blue">class</font> TFunctor&gt;
    <font color="blue">friend</font> <font color="blue">bool</font> <font color="blue">operator</font>!=(<font color="blue">const</font> Delegate&amp; d, <font color="blue">const</font> TFunctor&amp; f);
    <font color="blue">template</font> &lt;<font color="blue">class</font> TFunctor&gt;
    <font color="blue">friend</font> <font color="blue">bool</font> <font color="blue">operator</font>!=(<font color="blue">const</font> TFunctor&amp; f, <font color="blue">const</font> Delegate&amp; d);

    R <font color="blue">operator</font>()(T1, T2, ..., TN) <font color="blue">const</font>;
};

} <font color=darkgreen>// namespace Acf</font>
</pre>

<h2>Examples</h2>
<p><b>1. Using delegates</b></p>
<pre>
<font color="blue">static</font> <font color="blue">void</font> H() { ... }

<font color="blue">class</font> Foo {
<font color="blue">public</font>:
    <font color="blue">void</font> G() { ... }
};

<font color="blue">class</font> Foo2 {
<font color="blue">public</font>:
    <font color="blue">void</font> <font color="blue">operator</font>()(<font color="blue">int</font> n) { ... }
};

<font color=darkgreen>// Create delegate a which attaches to a static function</font>
Delegate&lt;<font color="blue">void</font> ()&gt; a(&amp;H);
assert(a == &amp;H);

<font color=darkgreen>// Create delegate b which attaches to an instance and a member function</font>
Foo foo;
Delegate&lt;<font color="blue">void</font> ()&gt; b(&amp;foo, &amp;Foo::G);
assert(b == std::make_pair(&amp;foo, &amp;Foo::G);

<font color=darkgreen>// Create delegate c from a</font>
Delegate&lt;<font color="blue">void</font> ()&gt; c = a;
assert(c == &amp;H);

<font color=darkgreen>// Create delegate d which attaches to a function object</font>
Delegate&lt;<font color="blue">void</font> (<font color="blue">int</font>)&gt; d(Foo2());

<font color=darkgreen>// Call delegate</font>
a();
d(<font color="darkred">100</font>);

<font color=darkgreen>// Combine/remove delegates</font>
d += &amp;H;
d += std::make_pair(&amp;foo, &amp;Foo::G);
d -= std::make_pair(&amp;foo, &amp;Foo::G);
d -= &amp;H;
</pre>

<p><b>2. Using events</b></p>
<pre><font color="blue">static</font> <font color="blue">void</font> Hello(Button*, ClickEventArgs* args) { ... }

<font color="blue">typedef</font> Delegate&lt;<font color="blue">void</font> (Button*, ClickEventArgs*)&gt;    ClickEventHandler;

<font color="blue">class</font> Button {
<font color="blue">public</font>:
    ClickEventHandler Click;
    <font color="blue">void</font> OnClick() { <font color="blue">this</font>-&gt;Click(<font color="blue">this</font>, NULL); }
};

Button b;
b.Click += &amp;Hello;
b.OnClick();
b.Click -= &amp;Hello;
</pre>

</body>
</html>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Yingle Jia
Web Developer
China China
Yingle Jia is a software engineer located in Beijing, China. He currently works at IBM CSDL (China Software Development Lab). His interests include C++/COM/C#/.NET/XML, etc. He likes coding and writing.
 
He is the creator of ACF (Another C++ Framework) project. See http://acfproj.sourceforge.net/.
 
He also has a blog at http://blogs.wwwcoder.com/yljia/

| Advertise | Privacy | Mobile
Web02 | 2.8.140902.1 | Last Updated 6 Jan 2006
Article Copyright 2005 by Yingle Jia
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid