使用C ++模板与C结构的反省? [英] Using C++ Templates with C structs for introspection?

查看:111
本文介绍了使用C ++模板与C结构的反省?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做C ++为具有一切写在C公司的一些工作(用C不是我一个:(可选),他们有一些数据结构非常相似(即它们都有字段,如姓名,地​​址,等等。但是,不管是什么原因没有他们使用立足一切了常见的结构(使做什么地狱)。Anywho,我需要做这些结构是在内存中,并通过这一切到一个表的全系统的分析,不算太差,但表必须包括条目所有变量的各个领域,即使他们没有现场(结构b可有场潜伏,而是一个结构不 - 在表中的每个实例的条目必须有潜伏期空项

所以,我的问题是,有没有一种方法来确定在运行时是否已传递到模板函数的结构有一个特定的领域?或将我必须写一些黑魔法宏,它会为我吗? (这个问题基本上是我不能使用模板特)

谢谢!如果您有任何疑问,请随时问!

下面是什么,我在想一个SNIPPIT ...

 一个结构
{
  字符名称[256];
  INT指数;
  漂浮的Percision;
};结构乙
{
  INT指数;
  字符名称[256];
  INT延迟;
};/ *更恼人的类似结构...请注意,上述所有的在被编译为C文件中定义 - 而不是C ++ * /结构进入
{
  字符名称[256];
  INT指数;
  漂浮的Percision;
  INT延迟;
  / *更多字段特定于仅1个或多个结构* /
};模板< typename的T>结构进入gatherFrom(T * PTR)
{
  入口进入;  的strcpy(entry.name,ptr->名,strlen的(ptr->名));
  entry.index = ptr->指数;
  / *像这样的事情吧? * /
  entry.percision = type_contains_field(的Percision)? ptr->的Percision:-1;
}诠释的main()
{
  结构A中的;
  结构B B;  / *初始化。* /  条目E = gatherFrom(一);
  项E2 = gatherFrom(B);  返回0;
}


解决方案

  

别人用C写的一切(使用C不是我:(一个选项)。


首先,我想引述Linus Torvalds公司不得不说一下这个问题:


 来源:Linus Torvalds公司<&托瓦尔兹LT; AT> linux-foundation.org>
主题:回复:[RFC]转换builin-mailinfo.c使用的更好的字符串库。
新闻组:gmane.comp.version-control.git
日期:2007-09-06十七时50分28秒格林尼治标准​​时间(2年,14周16小时36分钟前)C ++是一个可怕的语言。它是由这样一个事实:很多更加可怕
不合格的程序员使用它,到这种地步是非常非常
更容易产生与它总与彻底的废话。坦率地说,即使
C的选择是做*不*,但保持了C ++程序员了,
这本身将是一个巨大的理由来使用C.

http://harmful.cat-v.org/software/c++/linus



  

他们有一些数据结构非常相似(也就是它们都具有字段,如姓名,地​​址,等等。但是,不管是什么原因没有他们使用基地的共同结构一切折(让做什么地狱)。


他们可能有过这种非常健全的原因。把公共字段到一个单一的基础结构(类)可能听起来像一个伟大的想法。但它使您是否要发生重大变化适用于结构的一个事情真的很难,同时不影响剩余(更换某些领域,改变类型等)。 OOP肯定不是做事的唯一正确的方法。


  

所以,我的问题是,有没有一种方法来确定在运行时是否已传递到模板函数的结构有一个特定的领域?


没有,这是不可能的。无论是在C和C ++中,因为在创建二进制当即将所有类型的信息被丢弃。有既不反射,也没有反省C或C ++。那么,在技术上的调试信息的编译器生成的的提供这些信息,但没有语言内置功能来访问此。同时这种调试信息的依赖在编译的时候进行分析,而不是在运行时。 C ++有RTTI,但是这仅是一个非常粗糙的系统,以确定一个实例是关闭的哪个类。它不与类或结构成员的帮助。

但是,为什么你关心在运行时无论如何要做到这一点?


  

Anywho,我需要做的这些结构是在内存中的一个全系统的分析,并通过这一切到一个表。


您应该真正高兴的是,你要分析C和不是C ++。因为C是真的,真的很容易解析(不像C ++这是非常困难解析,主要是因为这些该死的模板)。尤其是结构。我只是写一个小而简单的脚本,即提取从C源的所有结构定义。不过,由于结构是固定大小的,他们往往包含指向动态分配的数据。除非你想修补分配,我想分析一下这个最简单的方法,就是通过挂钩到一个调试器和记录每一个独特的对象,其指针被分配到一个结构成员的内存使用情况。

I'm doing some work in C++ for a company that has everything else written in C (using C isn't an option for me :( ). They have a number of data structures that are VERY similar (i.e., they all have fields such as "name", "address", etc. But, for whatever reason there isn't a common structure that they used to base everything else off of (makes doing anything hell). Anywho, I need to do a system-wide analysis of these structs that are in memory, and through it all into a table. Not too bad, but the table has to include entries for all the fields of all the variables, even if they don't have the field (struct b may have field "latency", but struct a doesn't - in the table the entry for each instance of a must have an empty entry for "latency".

So, my question is, is there a way to determine at runtime if a structure that has been passed into a template function has a specific field? Or will I have to write some black magic macro that does it for me? (The problem is basically that I can't use template specialization)

Thanks! If you have any questions please feel free to ask!

Here's a snippit of what I was thinking...

struct A
{
  char name[256];
  int index;
  float percision;
};

struct B
{
  int index;
  char name[256];
  int latency;
};

/* More annoying similar structs... note that all of the above are defined in files that were compiled as C - not C++ */

struct Entry
{
  char name[256];
  int index;
  float percision;
  int latency;
  /* more fields that are specific to only 1 or more structure */
};

template<typename T> struct Entry gatherFrom( T *ptr )
{
  Entry entry;

  strcpy( entry.name, ptr->name, strlen( ptr->name ) );
  entry.index = ptr->index;
  /* Something like this perhaps? */
  entry.percision = type_contains_field( "percision" ) ? ptr->percision : -1;
}

int main()
{
  struct A a;
  struct B b;

  /* initialization.. */

  Entry e  = gatherFrom( a );
  Entry e2 = gatherFrom ( b );

  return 0;
}

解决方案

everything else written in C (using C isn't an option for me :( ).

First I'd like to quote what Linus Torvalds had to say about this issue:


From: Linus Torvalds <torvalds <at> linux-foundation.org>
Subject: Re: [RFC] Convert builin-mailinfo.c to use The Better String Library.
Newsgroups: gmane.comp.version-control.git
Date: 2007-09-06 17:50:28 GMT (2 years, 14 weeks, 16 hours and 36 minutes ago)

C++ is a horrible language. It's made more horrible by the fact that a lot 
of substandard programmers use it, to the point where it's much much 
easier to generate total and utter crap with it. Quite frankly, even if 
the choice of C were to do *nothing* but keep the C++ programmers out, 
that in itself would be a huge reason to use C.

http://harmful.cat-v.org/software/c++/linus


They have a number of data structures that are VERY similar (i.e., they all have fields such as "name", "address", etc. But, for whatever reason there isn't a common structure that they used to base everything else off of (makes doing anything hell).

They may have had very sound reasons for this. Putting common fields into a single base structure (class) may sound like a great idea. But it makes things really difficult if you want to apply major changes to one of the structures (replace some fields, change types, etc.) while leaving the rest intact. OOP is certainly not the one true way to do things.

So, my question is, is there a way to determine at runtime if a structure that has been passed into a template function has a specific field?

No this is not possible. Neither in C nor in C++, because all information about types gets discarded when the binary is created. There's neither reflection nor introspection in C or C++. Well, technically the debug information the compiler emits does provide this information, but there's no language builtin feature to access this. Also this sort of debug information relies on analysis performed at compile time, not at runtime. C++ has RTTI, but this is only a very coarse system to identify which class an instance is off. It does not help with class or struct members.

But why do you care to do this at runtime anyway?

Anywho, I need to do a system-wide analysis of these structs that are in memory, and through it all into a table.

You should be actually happy that you have to analyse C and not C++. Because C is really, really easy to parse (unlike C++ which is tremendously difficult to parse, mostly because of those darn templates). Especially structs. I'd just write a small and simple script, that extracts all the struct definitions from the C sources. However since structs are of constant size, they often contain pointers to dynamically allocated data. And unless you want to patch your allocator, I think the most easy way to analyse this, is by hooking into a debugger and record the memory usage of every unique object whose pointer is assigned to a struct member.

这篇关于使用C ++模板与C结构的反省?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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