473,385 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How Best to Incorporate Traits and a Policies

I see the STL using class templates accepting a traits and a policy.
To me, the traits are the "what" and the policy is the "how". How can
a policy know what to do without the traits of the topic? Wouldn't it
be better for containers to take a policy template argument and for
the policy to take a traits template argument? Consider an allocator
(policy) that is going to be used in a multi-threaded environment.
The programmer could use something like the following (simplified)
traits.

template<typename TYPE>
struct NormalTraits
{
typedef TYPE value_type;
typedef value_type *pointer;
typedef std::size_t size_type;
};
template<>
struct NormalTraits<void>
{
private: typedef void TYPE;
typedef TYPE value_type;
typedef value_type *pointer;
typedef std::size_t size_type;
};

template<typename TYPE>
struct VolatileTraits
{
typedef TYPE value_type;
typedef volatile value_type *pointer;
typedef std::size_t size_type;
};
template<>
struct VolatileTraits<void>
{
private: typedef void TYPE;
typedef TYPE value_type;
typedef volatile value_type *pointer;
typedef std::size_t size_type;
};

Now, a programmer could specify the VolatileTraits<to an allocator
policy for a multi-threaded container and the policy would know what
types should be given to the signatures of methods like 'pointer
allocate( size_type, typename
TRAITS::rebind<void>::other::const_pointer )'.

Milburn Young
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jul 18 '07 #1
2 1706
In article <11**********************@z24g2000prh.googlegroups .com>,
Milburn Young <Mi***********@gmail.comwrote:
template<>
struct NormalTraits<void>
{
private: typedef void TYPE;
typedef TYPE value_type;
typedef value_type *pointer;
typedef std::size_t size_type;
};

template<>
struct VolatileTraits<void>
{
private: typedef void TYPE;
typedef TYPE value_type;
typedef volatile value_type *pointer;
typedef std::size_t size_type;
};

Now, a programmer could specify the VolatileTraits<to an allocator
policy for a multi-threaded container and the policy would know what
types should be given to the signatures of methods like 'pointer
allocate( size_type, typename
TRAITS::rebind<void>::other::const_pointer )'.
seems that you have no traits publically accessable if type is void:)

boost and tr1 provide add_volatile and is_volatile templates
which are simple to write if you don't have either.

template <class A,class Bool = is_volatile<A class Allocator;

template<class Aclass Allocator<A,true_type>
{
// volatile allocator
};

template <class Aclass Allocator<A,false_type>
{
// non volatile allocator
};
less is more:)

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jul 19 '07 #2
On Jul 18, 3:59 pm, Milburn Young <Milburn.Yo...@gmail.comwrote:
I see the STL using class templates accepting a traits and a policy.
To me, the traits are the "what" and the policy is the "how".
Any distinction is really arbitrary. In the STL, they are
called traits. In other, more recent literature, they are
called policy. Same thing in the end.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientie objet/
Beratung in objektorientierter Datenverarbeitung
9 place Simard, 78210 St.-Cyr-l'Icole, France, +33 (0)1 30 23 00 34

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jul 19 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Donald 'Paddy' McCarthy | last post by:
Hi, I am having a few problems with a GUI. I am new to traits and wxGlade. I have used wxGlade to create a Form with an embedded space for a CustomWidget. I have the traits demo and would like...
0
by: skscpp | last post by:
What's wrong with the following code? Compiler error is at the bottom. The compiler I am using is gcc version 2.95. ============================= // traits.h =============================...
12
by: Mark A. Gibbs | last post by:
Good day, What is the safest way to get a non-end of file value? char_traits<char_type>::eof() will get me eof, but how can I safely and consistently generate a value that is not eof? should i...
8
by: Jon Slaughter | last post by:
I'm reading Alexandrescu's Modern C++ design about the concept of policies. What I'm wondering is if policies are a common design pattern in C++(OOP) nowdays? It seems like a good idea but I'm...
2
by: Ash | last post by:
Hello everyone ! I am trying to find some sort of a cookbook or more examples for using Enthought Traits to build GUI's. I tried to follow the documentations present at the enthought site, but...
9
by: Bit Byte | last post by:
Can't seem to get my head around the point of a trait class - no matter how many times I read up on it - why not simply use functors or function pointers ? Anyone care to explain this in a...
0
by: Milburn Young | last post by:
I see the STL using class templates accepting a traits and a policy. To me, the traits are the "what" and the policy is the "how". How can a policy know what to do without the traits of the topic?...
2
by: Colin J. Williams | last post by:
Using >easy_install -v -f http://code.enthought.com/enstaller/eggs/source enthought.traits The result is: .... many lines ....
5
by: greek_bill | last post by:
Hi, I'm trying to develop a system where I can register some data/ information about a class. For example // ClassInfo.h template <class T> struct ClassInfo { static const std::string ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.