J
James Brown
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
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