Hi all,
Having problems designing a template-class. I'll describe my scenario
first then show what I've come up with so far:
Need a class to provide pointer/array-like access to an area of physical
memory
located on a piece of custom hardware - this memory is only accessible
using machine specific i/o so I want to hide all this in a class. I'm
imagining
that the class will provide behaviour similar to a vector or string class -
that
is, provide operator[] access, maybe pointer-like behaviour using ++ and --
operators and the * dereference operator.
The class might be used like:
pmem<char> physmem;
physmem[0] = 'A'; // store 'A' at offset 0
physmem++;
physmem[0] = 'B'; // store 'B' at offset 1
char ch = *physmem;
and maybe even
cout << physmem;
i.e. physmem somehow "decays" to a char* pointer..
I'm having alot of difficulty deciding what kind of facility I should
provide,
really due to lack of experience with C++
my so far class looks like:
template <class type>
class pmem
{
public:
pmem() : index(0) {}
type & operator(size_t offset);
const type &operator(size_t offset) const;
private:
size_t index;
type *view;
// private members to map/unmap the physical memory
// into the "view" buffer
}
The class must map/unmap the physical memory in views - i.e. very similar
to a segmented memory architecture, so the [] operators need to handle this
and
provide a strictly controlled "linear" access to this segmented model.
Within the physical memory there are memory regions which would be
easily represented by normal C structures - what I want to avoid is code
like this:
struct mystruct
{
int member1;
int member2;
};
mystruct ms;
physmem.read(&ms , sizeof(mystruct));
cout << ms.member1 << endl;
and replace it with something similar to:
pmem<mystruct *> physmem;
cout << physmem->member1;
Hope I've provided a good enough example here..
Does anyone have any suggestions as to best practises for this type of
thing, good
books to read? I can code the thing up quite happily, its the design stage
which is
hindering me at the moment.
TIA
James 12 1826
James Brown wrote: Hi all,
Having problems designing a template-class. I'll describe my scenario first then show what I've come up with so far:
Need a class to provide pointer/array-like access to an area of physical memory located on a piece of custom hardware - this memory is only accessible using machine specific i/o so I want to hide all this in a class. I'm imagining that the class will provide behaviour similar to a vector or string class - that is, provide operator[] access, maybe pointer-like behaviour using ++ and -- operators and the * dereference operator.
The class might be used like:
pmem<char> physmem;
physmem[0] = 'A'; // store 'A' at offset 0 physmem++; physmem[0] = 'B'; // store 'B' at offset 1
char ch = *physmem;
and maybe even
cout << physmem;
i.e. physmem somehow "decays" to a char* pointer..
In C++, you can think about 'string's instead of char * .
It is much cleaner . The STL 'string' and 'sstream' could really be
useful instead of dealing with raw char pointers. I'm having alot of difficulty deciding what kind of facility I should provide, really due to lack of experience with C++
my so far class looks like:
template <class type>
You can use -
template <ctypename type> for better readability . It is a matter of
style, but nevertheless improves readability of the code.
Again as a metter of style, typenames more often than not, begin with
a capital letter.
class pmem { public: pmem() : index(0) {}
type & operator(size_t offset);
Did u mean to support constructs like -
'
A' = physmem[0] ; // appearing on the R.H.S.
when you write this signature.
That hinders the readability of the code again.
const type &operator(size_t offset) const;
fine. but what happens if offset is outside the 'segment' .
How are you going to handle it.
Are you going to throw custom exceptions from your code ? That would
be natural. And in case, you are throwing some - it is a good idea to
mention them in the throw clause here , so that anyone who uses the API
would know what exceptions to catch and deal accordingly. private: size_t index; type *view;
// private members to map/unmap the physical memory // into the "view" buffer }
The class must map/unmap the physical memory in views - i.e. very similar to a segmented memory architecture, so the [] operators need to handle this and provide a strictly controlled "linear" access to this segmented model.
Within the physical memory there are memory regions which would be easily represented by normal C structures - what I want to avoid is code like this:
struct mystruct { int member1; int member2; };
mystruct ms; physmem.read(&ms , sizeof(mystruct));
cout << ms.member1 << endl;
and replace it with something similar to:
pmem<mystruct *> physmem; cout << physmem->member1;
Hope I've provided a good enough example here.. Does anyone have any suggestions as to best practises for this type of thing, good books to read?
http://accu.org/bookreviews/public/r.../0sb/index.htm .
Look in the sections related to beginner C++ and advanced C++ .
I can code the thing up quite happily, its the design stage which is hindering me at the moment.
It is always a good thing to spend a lot of time in designing things
since coding can be wrapped up soon once you are clear about the
specifications of the class(es).
--
Karthik.
Karthik Kumar wrote:
You can use -
template <ctypename type> for better readability . It is a matter of style, but nevertheless improves readability of the code.
Oops ! I meant -
template <typename T> .
--
Karthik.
"James Brown" <remove_james_dot_brown7_at_virgin_dot_net> wrote: Hi all,
Having problems designing a template-class. I'll describe my scenario first then show what I've come up with so far:
Need a class to provide pointer/array-like access to an area of physical memory located on a piece of custom hardware - this memory is only accessible using machine specific i/o so I want to hide all this in a class. I'm imagining that the class will provide behaviour similar to a vector or string class - that is, provide operator[] access, maybe pointer-like behaviour using ++ and -- operators and the * dereference operator.
The class might be used like:
pmem<char> physmem;
physmem[0] = 'A'; // store 'A' at offset 0 physmem++; physmem[0] = 'B'; // store 'B' at offset 1
char ch = *physmem;
and maybe even
cout << physmem;
i.e. physmem somehow "decays" to a char* pointer..
So far, this looks like you want to provide some sort of an iterator for an
anonymous container. In this case, I would suggest to mimick the interface
of random access iterators to some degree. If you do that, you will be able
to use the generic algorithms on those. Be sure to have a value that
represents the past-end position of the container.
I'm having alot of difficulty deciding what kind of facility I should provide, really due to lack of experience with C++
my so far class looks like:
template <class type> class pmem { public: pmem() : index(0) {}
type & operator(size_t offset); const type &operator(size_t offset) const;
This does not match your descritption above. I was expecting to see
stuff like:
type& operator* ();
const type& operator* () const;
type& operator[] ( std::size_t );
const type& operator[] ( std::size_t ) const;
std::ptrdiff_t operator- ( pmem const & other ) const;
pmem& operator+=( std::ptrdiff_t );
pmem& operator++();
...
and I would suggest to include some typedefs like
typedef type value_type;
typedef type* pointer_type;
...
private: size_t index; type *view;
// private members to map/unmap the physical memory // into the "view" buffer }
The class must map/unmap the physical memory in views - i.e. very similar to a segmented memory architecture, so the [] operators need to handle this and provide a strictly controlled "linear" access to this segmented model.
Within the physical memory there are memory regions which would be easily represented by normal C structures - what I want to avoid is code like this:
struct mystruct { int member1; int member2; };
mystruct ms; physmem.read(&ms , sizeof(mystruct));
cout << ms.member1 << endl;
and replace it with something similar to:
pmem<mystruct *> physmem;
You are confusing me. Do you want
pmem<mystruct> physmem
instead?
cout << physmem->member1;
Hope I've provided a good enough example here.. Does anyone have any suggestions as to best practises for this type of thing, good books to read? I can code the thing up quite happily, its the design stage which is hindering me at the moment.
Best
Kai-Uwe Bux
"Kai-Uwe Bux" <jk********@gmx.net> wrote in message
news:ci**********@murdoch.acc.Virginia.EDU... "James Brown" <remove_james_dot_brown7_at_virgin_dot_net> wrote:
Hi all,
Having problems designing a template-class. I'll describe my scenario first then show what I've come up with so far:
Need a class to provide pointer/array-like access to an area of physical memory located on a piece of custom hardware - this memory is only accessible using machine specific i/o so I want to hide all this in a class. I'm imagining that the class will provide behaviour similar to a vector or string class - that is, provide operator[] access, maybe pointer-like behaviour using ++ and -- operators and the * dereference operator.
The class might be used like:
pmem<char> physmem;
physmem[0] = 'A'; // store 'A' at offset 0 physmem++; physmem[0] = 'B'; // store 'B' at offset 1
char ch = *physmem;
and maybe even
cout << physmem;
i.e. physmem somehow "decays" to a char* pointer..
So far, this looks like you want to provide some sort of an iterator for an anonymous container. In this case, I would suggest to mimick the interface of random access iterators to some degree. If you do that, you will be able to use the generic algorithms on those. Be sure to have a value that represents the past-end position of the container.
I'm having alot of difficulty deciding what kind of facility I should provide, really due to lack of experience with C++
my so far class looks like:
template <class type> class pmem { public: pmem() : index(0) {}
type & operator(size_t offset); const type &operator(size_t offset) const;
This does not match your descritption above. I was expecting to see stuff like:
type& operator* (); const type& operator* () const;
type& operator[] ( std::size_t ); const type& operator[] ( std::size_t ) const;
std::ptrdiff_t operator- ( pmem const & other ) const;
pmem& operator+=( std::ptrdiff_t );
pmem& operator++(); ...
and I would suggest to include some typedefs like
typedef type value_type; typedef type* pointer_type; ... private: size_t index; type *view;
// private members to map/unmap the physical memory // into the "view" buffer }
The class must map/unmap the physical memory in views - i.e. very similar to a segmented memory architecture, so the [] operators need to handle this and provide a strictly controlled "linear" access to this segmented model.
Within the physical memory there are memory regions which would be easily represented by normal C structures - what I want to avoid is code like this:
struct mystruct { int member1; int member2; };
mystruct ms; physmem.read(&ms , sizeof(mystruct));
cout << ms.member1 << endl;
and replace it with something similar to:
pmem<mystruct *> physmem;
You are confusing me. Do you want
pmem<mystruct> physmem
instead?
cout << physmem->member1;
Hope I've provided a good enough example here.. Does anyone have any suggestions as to best practises for this type of thing, good books to read? I can code the thing up quite happily, its the design stage which is hindering me at the moment.
Best
Kai-Uwe Bux
Hi,
thanks for the suggestions,
yes, I think an iterator-like class would be a good idea, especially one
that
can be used with algorithms...the trouble I have is because I have very
rarely used
this facility in C++ I don't know how to design something similar..but your
suggestion
is useful to me regardless..
My idea with the member-selection operator is probably incorrect - to be
honest
I'm not sure if I need pmem<mystruct> or pmem<mystruct *>, basically I'm a
C programmer who's very comfortable with array/pointer syntax, but unuse to
the
C++ way of achieving the same effect.
James
"Karthik Kumar" <ka*******************@yahoo.com> wrote in message
news:41535223$1@darkstar... Karthik Kumar wrote:
You can use -
template <ctypename type> for better readability . It is a matter of style, but nevertheless improves readability of the code.
Oops ! I meant -
template <typename T> .
-- Karthik.
Yes, I thought you meant that - this is syntax that I'm not familiar with,
does template <typename X> an exact replacement for template <class X>
if so why do two constructs exist for the same result?
thks,
James
"Karthik Kumar" <ka*******************@yahoo.com> wrote in message
news:415351e8$1@darkstar... James Brown wrote: Hi all,
Having problems designing a template-class. I'll describe my scenario first then show what I've come up with so far:
Need a class to provide pointer/array-like access to an area of physical memory located on a piece of custom hardware - this memory is only accessible using machine specific i/o so I want to hide all this in a class. I'm imagining that the class will provide behaviour similar to a vector or string class - that is, provide operator[] access, maybe pointer-like behaviour using ++ and -- operators and the * dereference operator.
The class might be used like:
pmem<char> physmem;
physmem[0] = 'A'; // store 'A' at offset 0 physmem++; physmem[0] = 'B'; // store 'B' at offset 1
char ch = *physmem;
and maybe even
cout << physmem;
i.e. physmem somehow "decays" to a char* pointer.. In C++, you can think about 'string's instead of char * . It is much cleaner . The STL 'string' and 'sstream' could really be useful instead of dealing with raw char pointers.
I'm having alot of difficulty deciding what kind of facility I should provide, really due to lack of experience with C++
my so far class looks like:
template <class type>
You can use -
template <ctypename type> for better readability . It is a matter of style, but nevertheless improves readability of the code.
Again as a metter of style, typenames more often than not, begin with a capital letter.
class pmem { public: pmem() : index(0) {}
type & operator(size_t offset);
Did u mean to support constructs like - ' A' = physmem[0] ; // appearing on the R.H.S.
when you write this signature.
Yes, I also want to achieve "normal" array subscripting. That hinders the readability of the code again.
const type &operator(size_t offset) const;
fine. but what happens if offset is outside the 'segment' . How are you going to handle it.
To be honest I'm not sure - all instances of my class will share a
cache of currently mapped-in pages for performance reasons, but yes there
is a potential problem when accessing offsets outside the current segment -
to address this issue the [] operator would map/unmap segments as
required.
The one big worry I have is, what do I do when I try to access an
"element" of my class which straddles a segment boundary. Perhaps it is
not possible to represent a segmented view in the manner I desire.. Are you going to throw custom exceptions from your code ? That would be natural. And in case, you are throwing some - it is a good idea to mention them in the throw clause here , so that anyone who uses the API would know what exceptions to catch and deal accordingly.
Yup exceptions sound like a good idea thanks :-)
private: size_t index; type *view;
// private members to map/unmap the physical memory // into the "view" buffer }
The class must map/unmap the physical memory in views - i.e. very similar to a segmented memory architecture, so the [] operators need to handle this and provide a strictly controlled "linear" access to this segmented model.
Within the physical memory there are memory regions which would be easily represented by normal C structures - what I want to avoid is code like this:
struct mystruct { int member1; int member2; };
mystruct ms; physmem.read(&ms , sizeof(mystruct));
cout << ms.member1 << endl;
and replace it with something similar to:
pmem<mystruct *> physmem; cout << physmem->member1;
Hope I've provided a good enough example here.. Does anyone have any suggestions as to best practises for this type of thing, good books to read?
http://accu.org/bookreviews/public/r.../0sb/index.htm .
Look in the sections related to beginner C++ and advanced C++ .
I can code the thing up quite happily, its the design stage which is hindering me at the moment.
It is always a good thing to spend a lot of time in designing things since coding can be wrapped up soon once you are clear about the specifications of the class(es).
-- Karthik.
Thanks,
James
> My idea with the member-selection operator is probably incorrect - to be honest I'm not sure if I need pmem<mystruct> or pmem<mystruct *>, basically I'm a C programmer who's very comfortable with array/pointer syntax, but unuse to the C++ way of achieving the same effect.
Your class is a container (of sorts). Do you see it containing pointers,
pmem<mystruct *>, or structs, pmem<mystruct>. My guess would be structs.
john
"James Brown" <remove_james_dot_brown7_at_virgin_dot_net> wrote in message
news:41**********************@news.dial.pipex.com. .. "Karthik Kumar" <ka*******************@yahoo.com> wrote in message news:41535223$1@darkstar... Karthik Kumar wrote:
You can use -
template <ctypename type> for better readability . It is a matter of style, but nevertheless improves readability of the code.
Oops ! I meant -
template <typename T> .
-- Karthik.
Yes, I thought you meant that - this is syntax that I'm not familiar with, does template <typename X> an exact replacement for template <class X> if so why do two constructs exist for the same result?
thks, James
It's an exact equivalent. class is traditional, but someone thought that T
in template <class T> doesn't have to be a class (it could be an int for
instance) so template <typename T> was added.
john
"John Harrison" <jo*************@hotmail.com> wrote in message
news:2r*************@uni-berlin.de... My idea with the member-selection operator is probably incorrect - to be honest I'm not sure if I need pmem<mystruct> or pmem<mystruct *>, basically I'm a C programmer who's very comfortable with array/pointer syntax, but unuse to the C++ way of achieving the same effect.
Your class is a container (of sorts). Do you see it containing pointers, pmem<mystruct *>, or structs, pmem<mystruct>. My guess would be structs.
john
Hi John,
thanks for your reply,
I see the underlying memory region containing a (primarily) structures
but also a few pointers as well, this is why I was trying to use the
template
syntax so that I could choose the most appropriate form when necessary..
but yes, a structured layout is my preferred way of accessing the memory.
thanks
james
"James Brown" <remove_james_dot_brown7_at_virgin_dot_net> wrote in message
news:41**********************@news.dial.pipex.com. .. "Kai-Uwe Bux" <jk********@gmx.net> wrote in message news:ci**********@murdoch.acc.Virginia.EDU... "James Brown" <remove_james_dot_brown7_at_virgin_dot_net> wrote:
Hi all,
Having problems designing a template-class. I'll describe my scenario first then show what I've come up with so far:
Need a class to provide pointer/array-like access to an area of
physical memory
....
yes, I think an iterator-like class would be a good idea, especially one that can be used with algorithms...the trouble I have is because I have very rarely used this facility in C++ I don't know how to design something similar..but
your suggestion is useful to me regardless..
This is a perfect application for an iterator_facade from the boost library.
See http://www.boost.org/libs/iterator/d...or_facade.html. You would
derive from an iterator_facade specialization and implement the methods:
dereference
equal
increment
decrement
advance
distance_to
The iterator_facade has all of the boiler plate necessary to create a proper
iterator. This is a header-only library, so you just need to include the
iterator_facade header file.
Jeff F
"James Brown" <remove_james_dot_brown7_at_virgin_dot_net> wrote: yes, I think an iterator-like class would be a good idea, especially one that can be used with algorithms...the trouble I have is because I have very rarely used this facility in C++ I don't know how to design something similar..but your suggestion is useful to me regardless..
Basically, an iterator class provides the following interface (I may have
forgotten something):
#include <memory>
template < typename ValueType >
class iterator {
public:
// ================
// | formal stuff |
// ================
/*
actually, these are implementation defined. The references and pointers
could be realized by fancy proxy objects, etc.
*/
typedef ValueType value_type;
typedef ValueType& reference;
typedef const ValueType& const_reference;
typedef ValueType* pointer;
typedef const ValueType* const_pointer;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
// =====================
// | booooooring stuff |
// =====================
// default constructor
iterator ( void );
// copy constructor
iterator ( const iterator & other );
// assignment operator
iterator& operator= ( const iterator & other );
// destructor
~iterator ( void );
// ==================
// | the real stuff |
// ==================
// dereferencing, makes our class look somewhat like ValueType*.
pointer operator* ( void );
const_pointer operator* ( void ) const;
// optional:
reference operator[] ( size_type );
const_reference operator[] ( size_type ) const;
// moving
iterator operator++ ( void );
iterator operator++ ( int );
iterator operator-- ( void );
iterator operator-- ( int );
iterator operator+= ( difference_type );
iterator operator-= ( difference_type );
iterator operator+ ( difference_type ) const;
iterator operator- ( difference_type ) const;
// measuring distances:
bool operator== ( const iterator & other ) const;
bool operator!= ( const iterator & other ) const;
bool operator< ( const iterator & other ) const;
bool operator<= ( const iterator & other ) const;
bool operator> ( const iterator & other ) const;
bool operator>= ( const iterator & other ) const;
difference_type operator- ( const iterator & other ) const;
}; // iterator<>
As you can see, it mimicks a ValueType* that traverses an array.
Note that there is a lot of redundancy, i.e., many of these methods can be
implemented in terms of others. My understanding is that Boost has a
library that provides that for you.
Of course, you will have to implement the most interesting stuff yourself:
the operator* that actually realizes access to the underlying data.
In addition, you will want to specialize the template iterator_traits<> for
your particular iterator. I think that boost also helps with that but I
have no first hand experience with that.
Finally, you need a method to get an iterator object to the beginning of
the range.
My idea with the member-selection operator is probably incorrect - to be honest I'm not sure if I need pmem<mystruct> or pmem<mystruct *>, basically I'm a C programmer who's very comfortable with array/pointer syntax, but unuse to the C++ way of achieving the same effect.
I guess, you want iterator< mystruct >. From the callers perspective, it
behaves like mystruct*.
Best
Kai-Uwe Bux
In article <2r*************@uni-berlin.de>,
John Harrison <jo*************@hotmail.com> wrote: "James Brown" <remove_james_dot_brown7_at_virgin_dot_net> wrote in message news:41**********************@news.dial.pipex.com ... "Karthik Kumar" <ka*******************@yahoo.com> wrote in message news:41535223$1@darkstar... Karthik Kumar wrote: You can use -
template <ctypename type> for better readability . It is a matter of style, but nevertheless improves readability of the code.
Oops ! I meant -
template <typename T> .
Yes, I thought you meant that - this is syntax that I'm not familiar with, does template <typename X> an exact replacement for template <class X> if so why do two constructs exist for the same result?
It's an exact equivalent. class is traditional, but someone thought that T in template <class T> doesn't have to be a class (it could be an int for instance) so template <typename T> was added.
This may be good bedtime reading http://www.comeaucomputing.com/techt...ates/#typename
for some typename overview.
--
Greg Comeau / Comeau C++ 4.3.3, for C++03 core language support
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: E G |
last post by:
Hi!
I am having problems in designing a class. First, I have a base class
that allocates a 3D data set and allows some other mathematical
operations with it, something like this:
template...
|
by: Nobody |
last post by:
This is sort of my first attempt at writing a template container class, just
wanted some feedback if everything looks kosher or if there can be any
improvements. This is a template class for a...
|
by: James Brown |
last post by:
Hi again,
I'm referring back to my previous posting of the same title,
with everyone's help I've now got a better understanding of what my
goals are and I have a class which now looks like:
...
|
by: Charulatha Kalluri |
last post by:
Hi,
I'm implementing a Matrix class, as part of a project. This is the interface
I've designed:
class Matrix( )
|
by: mast2as |
last post by:
Hi guys
Here's the class I try to compile (see below). By itself when I have a
test.cc file for example that creates an object which is an instance of
the class SpectralProfile, it compiles...
|
by: Hamilton Woods |
last post by:
Diehards,
I developed a template matrix class back around 1992 using Borland C++ 4.5
(ancestor of C++ Builder) and haven't touched it until a few days ago. I
pulled it from the freezer and...
|
by: charles.lobo |
last post by:
Hi,
I have recently begun using templates in C++ and have found it to be
quite useful. However, hearing stories of code bloat and assorted
problems I decided to write a couple of small programs...
|
by: vectorizor |
last post by:
Hello all,
I am attempting to vectorize few template functions with the Intel
compiler, but without much success so far. Ok granted, this question
is not 100% c++, but it is related enough that...
|
by: weaknessforcats |
last post by:
Introduction
Polymorphism is the official term for Object-Oriented Programming (OOP). Polymorphism is implemented in C++ by virtual functions.
This article uses a simple example hierarchy which...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
|
by: SueHopson |
last post by:
Hi All,
I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...
| |