473,788 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,He apStorageclass_ ;

A way to accomplish this may be

template <
class T,
class Class2,
template <classclass StoragePolicy = Storage2 >
class Population : public StoragePolicy<C lass2>
{
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<Tst orage_;
~StdVectorStora ge() {}
};

<
class Class2,
template <classclass StoragePolicy = Storage1 >
class Class : public StoragePolicy<C lass2>
{
// 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 1844
"aaragon" <al************ **@gmail.comwro te:
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.comwro te:
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<valu e_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.comwro te:
Daniel T. wrote:
>"aaragon" <al************ **@gmail.comwro te:
>>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<valu e_type,storage_ typepop_;

How do I accomplish this by using the sequence?
Population< vector< int IntPopulationWi thVector;
Population< list< double DoublePopulatio nWithList;
Population< c_array< char, 10 CharPopulationW ithCArray;

--
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.comwro te:
Daniel T. wrote:
"aaragon" <al************ **@gmail.comwro te:

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<valu e_type,storage_ typepop_;

How do I accomplish this by using the sequence?

Population< vector< int IntPopulationWi thVector;
Population< list< double DoublePopulatio nWithList;
Population< c_array< char, 10 CharPopulationW ithCArray;

--
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<doub le,vector<doubl e>>). 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<v ecS, ValueType>
{
typedef std::vector<Val ueTypetype;
};

template <class ValueType>
struct container_gen<l istS, ValueType>
{
typedef std::list<Value Typetype;
};

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.comwro te:
Daniel T. wrote:
>Population< vector< int IntPopulationWi thVector;
Population< list< double DoublePopulatio nWithList;
Population< c_array< char, 10 CharPopulationW ithCArray;
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<doub le,vector<doubl e>>).
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<v ecS, ValueType>
{
typedef std::vector<Val ueTypetype;
};

template <class ValueType>
struct container_gen<l istS, ValueType>
{
typedef std::list<Value Typetype;
};
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
2420
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 says " You should have empty MySQL database created (create MySQL database if not)" on my hosting server I have MySQL version 3.23.56 and it also has "Schema
7
2665
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 it differently. FOUR QUESTIONS: The background: I got three (3) files
55
3949
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 developing new products (specifically - desktop products) in C#/.NET instead of the usual C++/COM that we do. Since I am not an experienced .NET developer by any definition, I don't have a good grip on the "Pro" part. The argument that I hear...
9
2938
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 a device. The definition of the table is as follows: CREATE TABLE devicedata ( device_id int NOT NULL REFERENCES devices(id), -- id in the device
19
4109
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 the code that implements managing unbound controls on forms given the superior performance of unbound controls in a client/server environment. I can easily understand a newbie using bound controls or someone with a tight deadline. I guess I need...
7
2360
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 WinForms interface and then need to port that to a web app. I am kinda stuck on a design issue and need some suggestions / direction. Basically I have a business layer that I want to use to process any dataentry logic (row focus changes, data...
2
1387
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
2295
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 following files in order (assuming I am running from the us): help\datasource_en_US.htm help\datasource_en.htm help\datasource.htm
25
3138
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 course then the file doesn't get seen by the user. Is there any command that essentially executes the code and then echo's it? something that will take a string like '<body>blah<?php echo 'Hello'; ?></body>' and actually interpret the php
0
9498
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10366
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10112
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5399
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4070
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.