473,238 Members | 3,370 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,238 software developers and data experts.

need help with design issue...

Hello everybody,
I appreciate your taking the time to take a look at this example. I
need some help to start the design of an application. To that purpose
I'm using policy-based design. The idea is to have a Class that stores
elements of Class2 in different ways (arrays in stack memory, arrays in
heap memory, using the std::vector and so on). I would like the user
to customize the creation of a class with Class2 and StoragePolicy like
this

typedef Class<Class2,HeapStorageclass_;

A way to accomplish this may be

template <
class T,
class Class2,
template <classclass StoragePolicy = Storage2 >
class Population : public StoragePolicy<Class2>
{
T* pointee_; // points to actual storage
...
};

However, in this way the user cannot customize in the way given before
but it has to introduce the type that pointee_ points to (and this is
not good because the design should know what pointee_ points to from
the StoragePolicy). Therefore, take 2 becomes:

template <class T>
class StoragePolicy
{
void create(size_t s);
};

template <class T>
struct Storage1
{
void create(size_t s) { storage_ = std::vector<T>(s);}
protected:
std::vector<Tstorage_;
~StdVectorStorage() {}
};

<
class Class2,
template <classclass StoragePolicy = Storage1 >
class Class : public StoragePolicy<Class2>
{
// this->storage_ (the storage_ is inherited from one of the
StoragePolicy classes)
...
};

This is the way I thought it better to solve this problem. Now, the
questions I have are:
1. Is there a better solution for this? More elegant? Maybe with
better performance?
2. In this way, I can't declare the function create() static because I
have a variable in the PolicyClass, right? I tried but I have a
linkage error in the compilation.
3. Now the key issue. Once I have many of these storage policy
classes, I don't know what to do to traverse the containers. It would
be nice to have a random access iterator that traverses the container
as with the standard library. How do I accomplish this?

Once again, thanks for taking the time to read this. I appreciate it.
Best regards,

Alejandro A.

Sep 20 '06 #1
5 1802
"aaragon" <al**************@gmail.comwrote:
Hello everybody,
I appreciate your taking the time to take a look at this example. I
need some help to start the design of an application. To that purpose
I'm using policy-based design. The idea is to have a Class that stores
elements of Class2 in different ways (arrays in stack memory, arrays in
heap memory, using the std::vector and so on).
The simplest, I expect would be to write your class under the assumption
that it is holding a Sequence
(http://www.sgi.com/tech/stl/Sequence.html) 'vector', deque and list
already conform to the Sequence concept and it doesn't take much to
create a C array wrapper that does so as well.

template < typename Sequence >
class Population
{
Sequence sequence;
typedef typename Sequence::value_type value_type;
};

'value_type' is the type of the objects stored in the Sequence.
3. Now the key issue. Once I have many of these storage policy
classes, I don't know what to do to traverse the containers. It would
be nice to have a random access iterator that traverses the container
as with the standard library. How do I accomplish this?
All Sequence classes have 'begin' and 'end' which return iterators, but
you can't assume they are all random access because Sequence doesn't
make that guarantee.

--
There are two things that simply cannot be doubted, logic and perception.
Doubt those, and you no longer*have anyone to discuss your doubts with,
nor any ability to discuss them.
Sep 20 '06 #2

Daniel T. wrote:
"aaragon" <al**************@gmail.comwrote:
Hello everybody,
I appreciate your taking the time to take a look at this example. I
need some help to start the design of an application. To that purpose
I'm using policy-based design. The idea is to have a Class that stores
elements of Class2 in different ways (arrays in stack memory, arrays in
heap memory, using the std::vector and so on).

The simplest, I expect would be to write your class under the assumption
that it is holding a Sequence
(http://www.sgi.com/tech/stl/Sequence.html) 'vector', deque and list
already conform to the Sequence concept and it doesn't take much to
create a C array wrapper that does so as well.
How do you create the wrapper?
>
template < typename Sequence >
class Population
{
Sequence sequence;
typedef typename Sequence::value_type value_type;
};

'value_type' is the type of the objects stored in the Sequence.
Well, but I want the user to specify the value_type, right? by defining
the class as

typedef Population<value_type,storage_typepop_;

How do I accomplish this by using the sequence?
3. Now the key issue. Once I have many of these storage policy
classes, I don't know what to do to traverse the containers. It would
be nice to have a random access iterator that traverses the container
as with the standard library. How do I accomplish this?

All Sequence classes have 'begin' and 'end' which return iterators, but
you can't assume they are all random access because Sequence doesn't
make that guarantee.

--
There are two things that simply cannot be doubted, logic and perception.
Doubt those, and you no longer have anyone to discuss your doubts with,
nor any ability to discuss them.
Sep 20 '06 #3
"aaragon" <al**************@gmail.comwrote:
Daniel T. wrote:
>"aaragon" <al**************@gmail.comwrote:
>>Hello everybody, I appreciate your taking the time to take a look
at this example. I need some help to start the design of an
application. To that purpose I'm using policy-based design. The
idea is to have a Class that stores elements of Class2 in
different ways (arrays in stack memory, arrays in heap memory,
using the std::vector and so on).

The simplest, I expect would be to write your class under the
assumption that it is holding a Sequence
(http://www.sgi.com/tech/stl/Sequence.html) 'vector', deque and
list already conform to the Sequence concept and it doesn't take
much to create a C array wrapper that does so as well.

How do you create the wrapper?
There is one in The C++ Programming Language by Stroustrup. Below is
part of it...

template < typename T, int max >
struct c_array {
typedef T value_type;
typedef T* iterator;
typedef const T* const_iterator;
typedef T& reference;
typedef const T& const_reference;

T v[max];
operator T*() { return v; }

reference operator[](size_t i) { return v[i]; }

iterator begin() { return v; }
iterator end() { return v + max; }

ptrdiff_t size() const { return max; }
};

> template < typename Sequence >
class Population
{
Sequence sequence;
typedef typename Sequence::value_type value_type;
};

'value_type' is the type of the objects stored in the Sequence.

Well, but I want the user to specify the value_type, right? by
defining the class as

typedef Population<value_type,storage_typepop_;

How do I accomplish this by using the sequence?
Population< vector< int IntPopulationWithVector;
Population< list< double DoublePopulationWithList;
Population< c_array< char, 10 CharPopulationWithCArray;

--
There are two things that simply cannot be doubted, logic and perception.
Doubt those, and you no longer*have anyone to discuss your doubts with,
nor any ability to discuss them.
Sep 21 '06 #4

Daniel T. wrote:
"aaragon" <al**************@gmail.comwrote:
Daniel T. wrote:
"aaragon" <al**************@gmail.comwrote:

Hello everybody, I appreciate your taking the time to take a look
at this example. I need some help to start the design of an
application. To that purpose I'm using policy-based design. The
idea is to have a Class that stores elements of Class2 in
different ways (arrays in stack memory, arrays in heap memory,
using the std::vector and so on).

The simplest, I expect would be to write your class under the
assumption that it is holding a Sequence
(http://www.sgi.com/tech/stl/Sequence.html) 'vector', deque and
list already conform to the Sequence concept and it doesn't take
much to create a C array wrapper that does so as well.
How do you create the wrapper?

There is one in The C++ Programming Language by Stroustrup. Below is
part of it...

template < typename T, int max >
struct c_array {
typedef T value_type;
typedef T* iterator;
typedef const T* const_iterator;
typedef T& reference;
typedef const T& const_reference;

T v[max];
operator T*() { return v; }

reference operator[](size_t i) { return v[i]; }

iterator begin() { return v; }
iterator end() { return v + max; }

ptrdiff_t size() const { return max; }
};

template < typename Sequence >
class Population
{
Sequence sequence;
typedef typename Sequence::value_type value_type;
};

'value_type' is the type of the objects stored in the Sequence.
Well, but I want the user to specify the value_type, right? by
defining the class as

typedef Population<value_type,storage_typepop_;

How do I accomplish this by using the sequence?

Population< vector< int IntPopulationWithVector;
Population< list< double DoublePopulationWithList;
Population< c_array< char, 10 CharPopulationWithCArray;

--
There are two things that simply cannot be doubted, logic and perception.
Doubt those, and you no longer have anyone to discuss your doubts with,
nor any ability to discuss them.
GOT IT!!! =)
This is going to be quite helpful... I guess now I have to play with
the template template parameters to avoid including the type twice
(i.e., Population<double,vector<double>>). I have also to include the
header files for vector and list. I could define empty structures to
assign other names to it, right? I saw in the boost library something
like
struct StdVector {};
struct StdList {};

template <class Selector, class ValueType>
struct container_gen { };

template <class ValueType>
struct container_gen<vecS, ValueType>
{
typedef std::vector<ValueTypetype;
};

template <class ValueType>
struct container_gen<listS, ValueType>
{
typedef std::list<ValueTypetype;
};

Well, I think I have all I need now to start coding. Thanks Daniel for
the tips, I appreciate it... =)

Sep 21 '06 #5
"aaragon" <al**************@gmail.comwrote:
Daniel T. wrote:
>Population< vector< int IntPopulationWithVector;
Population< list< double DoublePopulationWithList;
Population< c_array< char, 10 CharPopulationWithCArray;
GOT IT!!! =)
This is going to be quite helpful... I guess now I have to play with
the template template parameters to avoid including the type twice
(i.e., Population<double,vector<double>>).
Apparently, you do not yet have it. I declared three different
Population objects above without ever including the type twice. No need
for template template parameters.
I have also to include the header files for vector and list.
Only if you actually use them and they shouldn't be included in the
population.h file; the client code would have to include them.
I could define empty structures to assign other names to it, right?
No need.
I saw in the boost library something
like
struct StdVector {};
struct StdList {};

template <class Selector, class ValueType>
struct container_gen { };

template <class ValueType>
struct container_gen<vecS, ValueType>
{
typedef std::vector<ValueTypetype;
};

template <class ValueType>
struct container_gen<listS, ValueType>
{
typedef std::list<ValueTypetype;
};
All the above is way more complex than it needs to be.
Well, I think I have all I need now to start coding. Thanks Daniel for
the tips, I appreciate it... =)
Again, check out the web page I posted (here it is again
http://www.sgi.com/tech/stl/Sequence.html) and do this:

template < typename Sequence >
class Population {
Sequence sequence;
//...

Now code the rest of your class as if 'sequence' contains all the
member-functions and typedefs defined on that page (and this page
http://www.sgi.com/tech/stl/Container.html because Sequences are also
Containers.) That's all you need to do. Forget about all that stuff you
have above.

--
There are two things that simply cannot be doubted, logic and perception.
Doubt those, and you no longer*have anyone to discuss your doubts with,
nor any ability to discuss them.
Sep 21 '06 #6

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

Similar topics

10
by: No | last post by:
I have tried to understand MySQL and am afraid it is way over my head, Would someone be willing to help me out please? I have a php script that I am trying to install, in the instructions it...
7
by: nospam | last post by:
Ok, 3rd or is it the 4th time I have asked this question on Partial Types, so, since it seems to me that Partial Types is still in the design or development stages at Microsoft, I am going to ask...
55
by: Alex | last post by:
Hello people, The following is not a troll but a serious request. I found myself in a position where I have to present a Pro/Con list to management and architects in our company with regard to...
9
by: sk | last post by:
I have an applicaton in which I collect data for different parameters for a set of devices. The data are entered into a single table, each set of name, value pairs time-stamped and associated with...
19
by: James Fortune | last post by:
I have a lot of respect for David Fenton and Allen Browne, but I don't understand why people who know how to write code to completely replace a front end do not write something that will automate...
7
by: Jack Addington | last post by:
I've got a fairly simple application implementation that over time is going to get a lot bigger. I'm really trying to implement it in a way that will facilitate the growth. I am first writing a...
2
by: Bob Heitzman | last post by:
I need to store data in, update, and read data from an XML file. An INI structure worked fine: a= 123 b= xyz c= etc
10
by: David Thielen | last post by:
Hi; I have help html pages for each page of my ASP.NET webapp. So for the page datasource.aspx, I have help\datasource.htm. Bu what I want when the hyperlink is clicked, for it to look for the...
25
by: Jon Slaughter | last post by:
I have some code that loads up some php/html files and does a few things to them and ultimately returns an html file with some php code in it. I then pass that file onto the user by using echo. Of...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.