Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Design

The Adapter Pattern


The Role and Implementation of the Traits Classes

As used here, the traits classes define a unified interface for both the visitors and the data-batches, and adapt to the interfaces of the existing classes. As such, they are the "adapter" classes at a compile-time level.

As stated above, it is possible to re-group traits classes according to certain traits of each class. For instance, the traits class itself can be derived from a variable set of classes that is determined by the actual traits of each class. For example, if some of our data-batch classes have a visit function that takes a pointer to the visitor, rather than a reference, that could be implemented as follows:

template  < typename T >
struct accepts_pointer : boost::false_type
{};
namespace Details
{
	template < typename DataBatchType, typename VisitorType, typename AcceptsPointer >
	struct DataBatchTraits_visit
	{
		static void visit(DataBatchType * data_batch)
		{
			VisitorType visitor(VisitorTraits< VisitorType >::create());
			data_batch->acceptVisitor(visitor);
		}
	};

	template < typename DataBatchType, typename VisitorType >
	struct DataBatchTraits_visit< DataBatchType, VisitorType, boost::true_type >
	{
		static void visit(DataBatchType * data_batch)
		{
			VisitorType visitor(VisitorTraits< VisitorType >::create());
			data_batch->acceptVisitor(&visitor);
		}
	};
}

template < typename DataBatchType, typename VisitorType >
struct DataBatchTraits : Details::DataBatchTraits_visit< DataBatchType, VisitorType, typename accepts_pointer< DataBatchType >::type >
{
};

in which case the meta-function accepts_pointer determines which version of DataBatchTraits_visit is used. By default, accepts_pointer "returns" false (or rather: boost::false_type), but a specialization of accepts_pointer like this:

template <>
struct accepts_pointer< DataBatchType2 > : boost::true_type
{};

indicating that DataBatchType2 implements an acceptVisitor function that takes a pointer to the visitor rather than a reference, would change the traits for DataBatchType2 accordingly. Hence, only one class of traits is necessary, which derives from any number of little traits classes according to any number of compile-time flags, implemented as compile-time meta-functions.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.