-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdirectives.html
More file actions
191 lines (180 loc) · 9.93 KB
/
directives.html
File metadata and controls
191 lines (180 loc) · 9.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Directives — cpp-netlib v0.10.0</title>
<link rel="stylesheet" href="../_static/pyramid.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../',
VERSION: '0.10.0',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/underscore.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<link rel="top" title="cpp-netlib v0.10.0" href="../index.html" />
<link rel="up" title="Techniques" href="../techniques.html" />
<link rel="next" title="Static and dynamic polymorphism" href="polymorphism.html" />
<link rel="prev" title="Tag metafunctions" href="tag_metafunctions.html" />
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Neuton&subset=latin" type="text/css" media="screen" charset="utf-8" />
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Nobile:regular,italic,bold,bolditalic&subset=latin" type="text/css" media="screen" charset="utf-8" />
<!--[if lte IE 6]>
<link rel="stylesheet" href="../_static/ie6.css" type="text/css" media="screen" charset="utf-8" />
<![endif]-->
</head>
<body>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="polymorphism.html" title="Static and dynamic polymorphism"
accesskey="N">next</a></li>
<li class="right" >
<a href="tag_metafunctions.html" title="Tag metafunctions"
accesskey="P">previous</a> |</li>
<li><a href="../contents.html">cpp-netlib v0.10.0</a> »</li>
<li><a href="../techniques.html" accesskey="U">Techniques</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="directives">
<h1>Directives<a class="headerlink" href="#directives" title="Permalink to this headline">¶</a></h1>
<p>The <tt class="xref py py-mod docutils literal"><span class="pre">cpp-netlib</span></tt> uses a technique for allowing message-passing
semantics in a chainable fashion in the form of directives. The basic
concept for directives is, in a general sense, an encapsulated
transformation that can be applied to objects that abide by the
directive protocol.</p>
<p>Using the object-oriented notion of message passing, where an object
accepts a message (usually a function call) we define a simple DSEL in
order for the protocol to be supported by certain object types. In the
<tt class="xref py py-mod docutils literal"><span class="pre">cpp-netlib</span></tt> the protocol implemented is similar to that of the
standard iostream formatting system:</p>
<div class="highlight-c++"><div class="highlight"><pre><span class="n">object</span> <span class="o"><<</span> <span class="n">directive1</span><span class="p">(...)</span>
<span class="o"><<</span> <span class="n">directive2</span><span class="p">(...)</span>
<span class="p">...</span>
<span class="o"><<</span> <span class="n">directiveN</span><span class="p">(...);</span>
</pre></div>
</div>
<p>In <tt class="xref py py-mod docutils literal"><span class="pre">cpp-netlib</span></tt> the directives are simple function objects that
take a target object as reference and returns a reference to the same
object as a result. In code the directive pattern looks like the
following:</p>
<div class="highlight-c++"><div class="highlight"><pre><span class="k">struct</span> <span class="n">directive_type</span> <span class="p">{</span>
<span class="k">template</span> <span class="o"><</span><span class="k">class</span> <span class="nc">Input</span><span class="o">></span>
<span class="n">Input</span> <span class="o">&</span> <span class="k">operator</span><span class="p">()(</span><span class="n">Input</span> <span class="o">&</span> <span class="n">input</span><span class="p">)</span> <span class="k">const</span> <span class="p">{</span>
<span class="c1">// do something to input</span>
<span class="k">return</span> <span class="n">input</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">};</span>
</pre></div>
</div>
<p>To simplify directive creation, usually factory or generator functions
are defined to return concrete objects of the directive’s type.</p>
<div class="highlight-c++"><div class="highlight"><pre><span class="kr">inline</span>
<span class="n">directive_type</span> <span class="nf">directive</span><span class="p">(...)</span> <span class="p">{</span>
<span class="k">return</span> <span class="n">directive_type</span><span class="p">();</span>
<span class="p">}</span>
</pre></div>
</div>
<p>The trivial implementation of the directive protocol then boils down
to the specialization of the shift-left operator on the target type.</p>
<div class="highlight-c++"><div class="highlight"><pre><span class="k">template</span> <span class="o"><</span><span class="k">class</span> <span class="nc">Directive</span><span class="o">></span>
<span class="kr">inline</span> <span class="n">target_type</span> <span class="o">&</span> <span class="k">operator</span><span class="o"><<</span>
<span class="p">(</span><span class="n">target_type</span> <span class="o">&</span> <span class="n">x</span><span class="p">,</span> <span class="n">Directive</span> <span class="k">const</span> <span class="o">&</span> <span class="n">f</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="n">f</span><span class="p">(</span><span class="n">x</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>
</div>
<div class="admonition-todo admonition" id="index-0">
<p class="first admonition-title">Todo</p>
<p class="last">An example using a directive.</p>
</div>
<p>The rationale for implementing directives include the following:</p>
<blockquote>
<div><ul class="simple">
<li><strong>Encapsulation</strong> - by moving logic into the directive types the
target object’s interface can remain rudimentary and even hidden
to the user’s immediate attention. Adding this layer of
indirection also allows for changing the underlying
implementations while maintaining the same syntactic and semantic
properties.</li>
<li><strong>Flexibility</strong> - by allowing the creation of directives that are
independent from the target object’s type, generic operations can
be applied based on the concept being modeled by the target
type. The flexibility also afforded comes in the directive’s
generator function, which can also generate different concrete
directive specializations based on parameters to the function.</li>
<li><strong>Extensibility</strong> - because the directives are independent of the
target object’s type, new directives can be added and supported
without having to change the target object at all.</li>
<li><strong>Reuse</strong> - truly generic directives can then be used for a broad
set of target object types that model the same concepts supported
by the directive. Because the directives are self-contained
objects, the state and other object references it keeps are only
accessible to it and can be re-used in different contexts as well.</li>
</ul>
</div></blockquote>
<p>Extending a system that uses directives is trivial in header-only
systems because new directives are simply additive. The protocol is
simple and can be applied to a broad class of situations.</p>
<p>In a header-only library, the static nature of the wiring and chaining
of the operations lends itself to compiler abuse. A deep enough
nesting of the directives can lead to prolonged compilation times.</p>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<h4>Previous topic</h4>
<p class="topless"><a href="tag_metafunctions.html"
title="previous chapter">Tag metafunctions</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="polymorphism.html"
title="next chapter">Static and dynamic polymorphism</a></p>
<div id="searchbox" style="display: none">
<h3>Quick search</h3>
<form class="search" action="../search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="polymorphism.html" title="Static and dynamic polymorphism"
>next</a></li>
<li class="right" >
<a href="tag_metafunctions.html" title="Tag metafunctions"
>previous</a> |</li>
<li><a href="../contents.html">cpp-netlib v0.10.0</a> »</li>
<li><a href="../techniques.html" >Techniques</a> »</li>
</ul>
</div>
<div class="footer">
© Copyright 2008-2013, Glyn Matthews, Dean Michael Berris; 2013 Google, Inc..
Last updated on Jun 28, 2013.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2b1.
</div>
</body>
</html>