考虑这样的枚举:
type
TTypeOfData = (
[XmlName('ABC')] todABC,
[XmlName('DEF')] todDEF,
[XmlName('GHI')] todGHI
);
Run Code Online (Sandbox Code Playgroud)
其中XmlName是一个自定义属性,用于为此枚举的成员定义序列化字符串.
如何浏览附加到此枚举的每个成员的属性?
我需要获取表单类型列表,但仅适用于从给定基本表单派生的类型.
我使用Delphi 2010和增强型RTTI来浏览类型
我目前的代码是:
rc := TRTTIContext.Create;
rtyps := rc.GetTypes;
for rtyp in rtyps do
begin
if not(rtyp.IsInstance) then Continue;
// Now I need to check if rtyp.AsInstance.MetaclassType is derived from TMyBaseForm
end;
Run Code Online (Sandbox Code Playgroud)
我不想实例化对象并使用'is'运算符,因为它不会及时执行.
作为当前的解决方法,我测试是否在RTTI上下文中找到了在TMyBaseForm中引入的方法:
if (rtyp.GetMethod('MyMethod') = nil) then Continue;
Run Code Online (Sandbox Code Playgroud)
但这不是一个干净的解决方案,因为如果在另一个类分支中引入了具有相同名称的方法,则会导致问题.
所以,我的问题是:是否有一种常规方法来检测类类型是否来自另一个类类型?
谢谢,
我记得在禁用了RTTI和异常的平台上编码,而在其他启用了它们的平台上编码.但是,我不记得在一个能够启用一个并禁用另一个的平台上进行编码.
这两个概念之间是否存在任何依赖关系?换句话说,异常需要RTTI才能运行吗?或者相反?
我在互联网上的某个地方找到了一个简单的解决方案,没有内置的C++ RTTI.
template <typename T>
class Identity {
public:
static int64_t id()
{
static int64_t dummy;
return reinterpret_cast<int64_t>(&dummy);
}
};
Run Code Online (Sandbox Code Playgroud)
当我们需要一些类ID时,我们只需使用:
Identity<OurClass>::id();
Run Code Online (Sandbox Code Playgroud)
我想知道,有没有碰撞?它可以为不同的类返回相同的ID,还是为相同的类返回不同的ID?我用g ++尝试了这个带有不同优化值的代码,一切似乎都没问题.
这是有效的代码与较新的Delphi版本?
// handle HTTP request "example.com/products?ProductID=123"
procedure TMyRESTfulService.HandleRequest([QueryParam] ProductID: string);
Run Code Online (Sandbox Code Playgroud)
在此示例中,参数"ProductID"归属于[QueryParam].如果这是Delphi中的有效代码,那么还必须有一种方法来编写基于RTTI的代码来查找属性参数类型信息.
查看我之前的问题使用Delphi的属性语言功能可以注释哪些语言元素?,列出了一些已报告使用属性的语言元素.此列表中缺少参数的属性.
以下是典型的实现type_info::operator==:
#if _PLATFORM_SUPPORTS_UNIQUE_TYPEINFO
bool operator==(const type_info& __rhs) const {
return __mangled_name == __rhs.__mangled_name;
}
#else
bool operator==(const type_info& __rhs) const {
return __mangled_name == __rhs.__mangled_name ||
strcmp(__mangled_name, __rhs.__mangled_name) == 0;
}
#endif
Run Code Online (Sandbox Code Playgroud)
在libstdc ++中它受控制__GXX_MERGED_TYPEINFO_NAMES,
在libc ++中_LIBCPP_NONUNIQUE_RTTI_BIT,
MSVC总是比较字符串.
什么是不比较字符串的平台?
今天我在 C++ 中捕获异常时遇到了一个奇怪的行为,有人可以向我解释一下吗?代码片段
#include <iostream>
#include <string>
#include <exception>
int main() {
try {
std::stod("notanumber");
} catch (const std::invalid_argument&) {
std::cerr << "std::invalid_argument" << std::endl;
} catch (const std::out_of_range&) {
std::cerr << "std::out_of_range" << std::endl;
} catch (const std::exception&) {
std::cerr << "Caught by ancestor" << std::endl;
} catch (...) {
auto ptr = std::current_exception();
auto type = __cxxabiv1::__cxa_current_exception_type();
std::cerr << type->name() << std::endl;
std::cerr << "..." << std::endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
写入输出
St16invalid_argument
...
Run Code Online (Sandbox Code Playgroud)
环境详情
C++ 14, disabled …Run Code Online (Sandbox Code Playgroud) 是否有一种简单的方法可以复制父组件下的所有子组件,包括它们的已发布属性?
例如:
当然这是最重要的因素,它应该复制我在TPanel上放弃的任何新组件,而不是在正常情况下修改代码.
我听说过RTTI,但实际上从未使用过它.有任何想法吗?
typeidC++中的运算符返回一个类的对象,该对象std::type_info可以生成其文本名称.但是,我只想为任何多态类获取唯一的数字标识符.(在单个程序运行范围内是唯一的 - 不一定在运行之间)
在实践中,我可以取消引用指针并读取vptr内容 - 但这既不优雅也不便携.我更喜欢便携式的方式.
我可以以typeid某种方式使用运算符为类提供"安全"的数字标识符吗?例如,对于给定类的std::type_info每个typeid调用,我可以指望结果结构的地址是相同的吗?或者name()指针本身?
我有一个Delphi泛型类,它使用泛型类型的参数公开一个函数.在这个函数中,我需要将泛型类型的实例传递给另一个期望Variant类型的对象.与此类似:
type
IMyInterface = interface
DoStuff(Value: Variant);
end;
TMyClass<T> = class
FMyIntf: IMyInterface
procedure DoStuff(SomeValue: T);
end;
[...]
procedure MyClass<T>.DoStuff(SomeValue: T);
begin
FMyIntf.DoStuff((*convert SomeValue to Variant here*));
end;
Run Code Online (Sandbox Code Playgroud)
我尝试使用Rtti.TValue.From(SomeValue).AsVariant.这适用于整体类型,但对布尔人来说却是爆炸性的.我不太明白为什么,因为通常我能够为Variant分配一个布尔值...
有没有更好的方法进行此转换?我只需要它用于简单的内置类型(不包括枚举和记录)