为内存访问设计模板类 [英] Designing a template class for memory access

查看:56
本文介绍了为内存访问设计模板类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


设计模板类时遇到问题。我先描述一下我的场景

然后再展示我到目前为止所得到的:


需要一个类来提供指针/数组 - 喜欢进入一个物理区域

内存

位于一块定制硬件上 - 这个内存只能通过机器特定的i / $
所以我想在课堂上隐藏所有这些。我是

想象

该类将提供类似于向量或字符串类的行为 -



是,提供operator []访问,可能是使用++的指针式行为和 -

运算符和* dereference运算符。


类可能会被用作:


pmem< char>物理;


physmem [0] =''A''; //在偏移0处存储''A''

physmem ++;

physmem [0] =''B''; //在偏移1处存储''B''

char ch = * physmem;


甚至可能


cout<<物理;


即物理以某种方式衰变到一个char *指针..


我很难决定我应该提供什么样的设施

提供,

真的是由于缺乏C ++经验


我到目前为止的课程看起来像:


模板< class type>

class pmem

{

public:

pmem():index(0){}


类型& operator(size_t offset);

const type& operator(size_t offset)const;


private:

size_t index;

类型*查看;


//私人会员将物理内存映射/取消映射

//进入视图 ;缓冲区

}


类必须在视图中映射/取消映射物理内存 - 即非常相似

到分段内存架构,所以[]运营商需要处理这个



提供严格控制的线性。访问这个分段模型。


在物理内存中有一些内存区域可以很容易用正常的C结构表示 - 我想要避免的是代码

是这样的:


struct mystruct

{

int member1;

int member2;

};


mystruct ms;

physmem.read(& ms,sizeof( mystruct));


cout<< ms.member1<<结束;


并用类似的东西替换它:


pmem< mystruct *> physmem;

cout<< physmem-> member1;


希望我在这里提供了一个足够好的例子..

有没有人对这方面的最佳实践有任何建议类型

的东西,好的

书籍可读吗?我可以非常愉快地编写这个东西,它的设计阶段

这是目前阻碍我的b / b



TIA

詹姆斯


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



推荐答案

James Brown写道:
James Brown wrote:
大家好,

设计模板类时遇到问题。我将描述我的场景
首先显示我到目前为止所提出的内容:

需要一个类来提供指向物理区域的指针/数组访问<位于一块自定义硬件上的内存
- 这个内存只能通过机器特定的i / o访问
,所以我想在课堂上隐藏所有这些内容。我想:
这个类会提供类似于向量或字符串类的行为 -

,提供operator []访问,可能是指针式的行为使用++和 -
运算符和* dereference运算符。

该类可能被用作:

pmem< char> physmem;

physmem [0] =''A''; //在偏移0处存储''A'
physmem ++;
physmem [0] ='''B''; //将''B''存储在偏移1处

char ch = * physmem;

甚至可能

cout<<物理;

即物理以某种方式腐烂 to char * pointer ..


在C ++中,你可以考虑''string'而不是char *。

它更干净。 STL''字符串''和''sstream''真的可以是有用的,而不是处理原始的char指针。

我有很多困难决定我应该提供什么样的设施,
真的是由于缺乏C ++的经验

我到目前为止的课程看起来像:

模板<类类型>


您可以使用 -


模板< ctypename类型>为了更好的可读性。这是一个

样式的问题,但仍然提高了代码的可读性。


再次作为风格的元素,类型名称通常是,从头开始

a大写字母。

class pmem
{
公开:
pmem():index(0){}

类型& operator(size_t offset);


你的意思是支持像 -

''

A''= physmem [0]; //当你写这个签名时,出现在RHS上





这会再次阻碍代码的可读性。

const type& operator(size_t offset)const;



罚款。但是如果偏移在''段'之外会发生什么。

你打算如何处理它。


你打算抛出自定义异常吗?你的代码?那将是b / b
是自然的。如果你扔了一些 - 这是一个好主意

在这里的throw子句中提到它们,所以任何使用API​​

的人都会知道什么是例外相应地捕获和处理。


私人:
size_t索引;
类型*视图;

//私人会员来映射/将物理内存取消映射到视图中。缓冲区

这个类必须在视图中映射/取消映射物理内存 - 即与分段内存架构非常相似,所以[]运算符需要处理这个提供严格控制的线性和线性。访问这个分段模型。

在物理内存中有一些内存区域可以很容易地用普通的C结构表示 - 我想要避免的是代码
,如下所示:

struct mystruct
{
int member1;
int member2;
};

mystruct ms;
physmem.read(& ms,sizeof(mystruct));

cout<< ms.member1<<用类似的东西代替它:

pmem< mystruct *> physmem;
cout<< physmem-> member1;

希望我在这里提供了一个足够好的例子..
有没有人对这种类型的东西的最佳实践有任何建议,好书
读书?

http:// accu。 org / bookreviews / public / r ... / 0sb / index.htm


查看与初学者C ++和高级C ++相关的部分。


我可以非常愉快地编写这个东西,它的设计阶段
目前阻碍了我。
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.




花大量时间设计东西总是一件好事

因为一旦你明白了这个类的

规格,编码就可以很快完成(es)。


-

Karthik。



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 Ku​​mar写道:
Karthik Kumar wrote:


您可以使用 -

模板< ctypename type>为了更好的可读性。这是一个
风格的问题,但仍然提高了代码的可读性。


You can use -

template <ctypename type> for better readability . It is a matter of
style, but nevertheless improves readability of the code.




哎呀!我的意思是 -


模板< typename T> 。


-

Karthik。



Oops ! I meant -

template <typename T> .

--
Karthik.


" James Brown" < remove_james_dot_brown7_at_virgin_dot_net>写道:
"James Brown" <remove_james_dot_brown7_at_virgin_dot_net> wrote:
大家好,

设计模板类时遇到问题。我将描述我的场景
首先显示我到目前为止所提出的内容:

需要一个类来提供指向物理区域的指针/数组访问<位于一块自定义硬件上的内存
- 这个内存只能通过机器特定的i / o访问
,所以我想在课堂上隐藏所有这些内容。我想知道该类将提供类似于向量或字符串类的行为
-
,提供operator []访问,可能使用指针式行为++和
- 运算符和* dereference运算符。

该类可能被用作:

pmem< char> physmem;

physmem [0] =''A''; //在偏移0处存储''A'
physmem ++;
physmem [0] ='''B''; //将''B''存储在偏移1处

char ch = * physmem;

甚至可能

cout<<物理;

即物理以某种方式腐烂到一个char *指针..


到目前为止,这看起来像是要为

匿名容器提供某种迭代器。在这种情况下,我建议在某种程度上模仿随机访问迭代器的界面

。如果你这样做,你将能够使用通用算法来获得这些算法。确保有一个价值

代表容器的过去位置。

我很难决定我应该使用哪种设施真的是由于缺乏C ++经验

我到目前为止的课程看起来像:

模板<类类型>
类pmem
{
公开:
pmem():index(0){}

类型& operator(size_t offset);
const type& operator(size_t offset)const;


这与上面的descritption不符。我期待看到

之类的东西:


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 ++();

...


我建议包含一些类型的定义,比如


typedef type value_type;

typedef type * pointer_type;

...

private:
size_t index;
type * view ;

//私人成员将物理内存映射/取消映射
//进入视图缓冲区

这个类必须在视图中映射/取消映射物理内存 - 即与分段内存架构非常相似,所以[]运算符需要处理
这和
提供严格控制的线性和线性。访问这个分段模型。

在物理内存中有一些内存区域可以很容易地用普通的C结构表示 - 我想要避免的是代码
,如下所示:

struct mystruct
{
int member1;
int member2;
};

mystruct ms;
physmem.read(& ms,sizeof(mystruct));

cout<< ms.member1<<用类似的东西代替它:

pmem< mystruct *> physmem;


你让我很困惑。你想要


pmem< mystruct>物理


代替?

cout<< physmem-> member1;

希望我在这里提供了一个足够好的例子..
有没有人对这种类型的东西的最佳实践有任何建议,好书
读书?我可以非常愉快地编写这个东西,它的设计阶段
目前阻碍了我。
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


Best

Kai-Uwe Bux


这篇关于为内存访问设计模板类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆