473,395 Members | 1,404 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,395 software developers and data experts.

Designing a template class for memory access (part 2)

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:

template <typename type>
class PhysMem
{
public:
type & operator [] (size_t index);
type operator * ( );
type * operator -> ();
private:
type *PhysMem;

void phys_readmem(void *dest, const void *phys, size_t len);
void phys_writemem(void *phys, const void *src, size_t len);
};
I have listed the three main public interfaces that I am interested in,
as I basically want my class to behave like a pointer to a memory region.
So if I had a "normal" pointer to somewhere in memory, it would do this:

struct mystruct
{
int member1, member2;
};

mystruct *ptr = (mystruct *) 0x12345678; // put in my memory region here

mystruct[0].member1 = 1; // use the pointer to address my memory region
mystruct->member1 = 2;

And if I had a "PhysMem" template class object, then it would do exactly the
same:

PhysMem<mystruct> ptr;
ptr->member1 = 2; // etc

This (in theory) works great with my current class design -
OK so the problem I face is as follows. I have an existing "C" interface to
my
physical memory, using two functions:

void phys_readmem(void *dest, const void *phys, size_t len);
void phys_writemem(void *phys, const void *src, size_t len);

This function takes care of mapping/unmapping physical memory, and what
happens when
memory accesses cross segment boundaries etc. I somehow need to build my C++
class around this C interface, the problem being I don't know how. Take for
example
the member-selection operator:

template <typename type>
type * PhysMem<type>::operator -> ()
{
return ???;
}

The function above must return a pointer to some memory. The problem is,
I ideally want to use the phys_writemem or phys_readmem routine, but where
do I copy the memory to
so I can return it? I need some kind of intermediate buffer, but how do I
manage the
lifetime of this buffer? And how do I handle a write-operation?

i.e. ptr->member1 = some_value;

I need to have control over the mystruct::operator= and invoke the
phys_writemem routine
when appropriate - the problem is, I have returned a general pointer in the
operator-> function
and it is too late at this stage.

I hope I have explained my thoughts clearly enough for someone to
have a stab at this one.

TIA,
James

Jul 22 '05 #1
2 1278
"James Brown" <remove_james_dot_brown7_at_virgin_dot_net> writes:
Hi again, Hello!
I somehow need to build my C++
class around this C interface, the problem being I don't know how. Take for
example
the member-selection operator:

template <typename type>
type * PhysMem<type>::operator -> ()
{
return ???;
}

The function above must return a pointer to some memory. The problem is,
I ideally want to use the phys_writemem or phys_readmem routine, but where
do I copy the memory to
so I can return it? I need some kind of intermediate buffer, but how do I
manage the
lifetime of this buffer? Well, one idea: one PhysMem object should correspond to one object in your
memory. This address should be given to PhysMem constructor. Then include
an instance of the object in the PhysMem instance:

template <typename type>
class PhysMem
{
public:
// These functions can be used to modify object
type & operator [] (size_t index);
type& operator* (); // I think this should return a reference
type * operator-> ();

// For const PhysMem - non modifing functions:
const type& operator[] (size_t index) const;
const type& operator* () const;
const type* operator-> () const;

private:
type buffer; // so we have memory to copy to
};
And how do I handle a write-operation?
i.e. ptr->member1 = some_value; That's up to you.
1. E.g. you can add a function flush() to PhysMem class, which
will write the object to the physical memory, e.g.:

PhysMem<mystruct> foo(0x123456789);
foo->member1 = 1;
foo->member2 = 2;
foo.flush(); // Write occurs here

Also add call to flush() to the PhysMem destructor.

2. Change
type* operator-> ();
to
PhysMemFlusher<type> operator-> ();
where PhysMemFlusher is

template <typename type>
class PhysMemFlusher
{
type* object;
public:
PhysMemFlusher(type* object)
: object(object)
{ }

~PhysMemFlusher()
{
// Write an object to phys_mem
}

type* operator-> ()
{
return object;
}
};

So, consider following code:
PhysMem<mystruct> foo(0x123456789);
foo->member1 = 1;

foo::operator-> returns an object of class PhysMemFlusher, then
compiler applies operator-> to that object and gets type* object (that
we want to change). Than it applies operator-> to get the member you
need, assigns new value to the member. Than it destoys the PhysMemFlusher
object, thus causing to write your object to phys_mem.
I need to have control over the mystruct::operator= and invoke the Nope. you need to control operator= of mystruct members.
phys_writemem routine
when appropriate - the problem is, I have returned a general pointer in the
operator-> function
and it is too late at this stage. I think the second solution will not require to change client code.
I hope I have explained my thoughts clearly enough for someone to
have a stab at this one.

Hope this will help you.

--
WBR, Max Vasin.
JID: ma******@jabber.ru
ICQ: 276438891
Jul 22 '05 #2

"Max Vasin" <ma********@mail.ru> wrote in message
news:87************@begemoth.ru...
"James Brown" <remove_james_dot_brown7_at_virgin_dot_net> writes:
Hi again, Hello!

I somehow need to build my C++
class around this C interface, the problem being I don't know how. Take
for
example
the member-selection operator:

template <typename type>
type * PhysMem<type>::operator -> ()
{
return ???;
}

The function above must return a pointer to some memory. The problem is,
I ideally want to use the phys_writemem or phys_readmem routine, but
where
do I copy the memory to
so I can return it? I need some kind of intermediate buffer, but how do I
manage the
lifetime of this buffer?

Well, one idea: one PhysMem object should correspond to one object in your
memory. This address should be given to PhysMem constructor. Then include
an instance of the object in the PhysMem instance:

template <typename type>
class PhysMem
{
public:
// These functions can be used to modify object
type & operator [] (size_t index);
type& operator* (); // I think this should return a reference
type * operator-> ();

// For const PhysMem - non modifing functions:
const type& operator[] (size_t index) const;
const type& operator* () const;
const type* operator-> () const;

private:
type buffer; // so we have memory to copy to
};
And how do I handle a write-operation?
i.e. ptr->member1 = some_value;

That's up to you.
1. E.g. you can add a function flush() to PhysMem class, which
will write the object to the physical memory, e.g.:

PhysMem<mystruct> foo(0x123456789);
foo->member1 = 1;
foo->member2 = 2;
foo.flush(); // Write occurs here

Also add call to flush() to the PhysMem destructor.

2. Change
type* operator-> ();
to
PhysMemFlusher<type> operator-> ();
where PhysMemFlusher is

template <typename type>
class PhysMemFlusher
{
type* object;
public:
PhysMemFlusher(type* object)
: object(object)
{ }

~PhysMemFlusher()
{
// Write an object to phys_mem
}

type* operator-> ()
{
return object;
}
};

So, consider following code:
PhysMem<mystruct> foo(0x123456789);
foo->member1 = 1;

foo::operator-> returns an object of class PhysMemFlusher, then
compiler applies operator-> to that object and gets type* object (that
we want to change). Than it applies operator-> to get the member you
need, assigns new value to the member. Than it destoys the PhysMemFlusher
object, thus causing to write your object to phys_mem.
I need to have control over the mystruct::operator= and invoke the

Nope. you need to control operator= of mystruct members.
phys_writemem routine
when appropriate - the problem is, I have returned a general pointer in
the
operator-> function
and it is too late at this stage.

I think the second solution will not require to change client code.
I hope I have explained my thoughts clearly enough for someone to
have a stab at this one.

Hope this will help you.

--
WBR, Max Vasin.
JID: ma******@jabber.ru
ICQ: 276438891


Hi Max,
Yes your suggestions are certainly helpful to me, I'm still trying to digest
what you
have detailed but it seems a good way forward for me now, I'll let the group
know how I get on.

thanks,
James
Jul 22 '05 #3

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

Similar topics

6
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...
12
by: James Brown | last post by:
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...
13
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( )
10
by: ma740988 | last post by:
Part of my confusion here is certainly my ignorance of templates and std::auto_ptr. Two topics, I've perused but need to really _delve_ into. In any event, consider the 'test' source. #...
3
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...
272
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two...
1
weaknessforcats
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...
3
by: djsuson | last post by:
I'm trying to set up an inheritance tree that also uses templates. This is an attempt to simplify some previous code that was filled with redundancies. I'm working with g++ 3.4.6. The code is split...
4
by: alan | last post by:
AFAIK a template parameter must be completely determinable by the compiler at compile time, is this correct? Currently my problem is, I have a class which contains a std::vector. The size of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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:
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
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,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.