473,657 Members | 2,453 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(vo id *dest, const void *phys, size_t len);
void phys_writemem(v oid *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<mystruc t> 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(vo id *dest, const void *phys, size_t len);
void phys_writemem(v oid *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::opera tor= 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 1288
"James Brown" <remove_james_d ot_brown7_at_vi rgin_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<mystruc t> 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<mystruc t> 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::opera tor= 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********@mai l.ru> wrote in message
news:87******** ****@begemoth.r u...
"James Brown" <remove_james_d ot_brown7_at_vi rgin_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<mystruc t> 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<mystruc t> 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::opera tor= 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
2135
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 <typename T> class BasicArray {
12
1885
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 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
13
16719
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
2534
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. # include <iostream> # include <memory> class CallbackBase { public: virtual void operator()() const { };
3
3749
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 thawed it out. I built a console app using Microsoft Visual C++ 6 (VC++) and it worked great. Only one line in the header file had to be commented out. I built a console app using Borland C++ Builder 5. The linker complained of references to...
272
14008
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 dimensional arrays from std::vectors ??? I want to use normal Array Syntax.
1
10085
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 you may have seen many times in one form or another. An analysis of this example produces several problems that are not obvious but which will seriously limit your ability to use hierarchies like the example in a real program. Then, the article...
3
3076
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 over four files: two headers and two implamentation files, but uses the include trick mentioned on another thread to connect the header and implamentation files together. The base class header is #ifndef _BASEDATA_H_ #define _BASEDATA_H_ ...
4
7939
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 this vector can be determined at the time an object of the class is constructed, and is assured not to change over the lifetime of the object. Now this class is part of an intrusive memory pool management scheme, and while it's okay for the...
0
8420
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8842
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...
0
8740
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8516
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
8617
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
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...
1
2743
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
2
1733
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.