使用get()函数-C++ Hazelcast客户端IMAP问题
在获取HazelCat键之前,在获得Hazelcast 5.0或更高版本中的密钥之前,有没有办法知道数据类型?
具体来说,我的C ++代码是:
hazelcast::client::imap* mapInstance = hz->get_map("MapName").get().get();;
boost::optional<bool> v;
try{
v = mapInstance->get<std::string, bool>("key2get").get();
}
catch (...) {
//print something
}
如果要获得的Hazelcast键是布尔值,则代码可完美。否则(例如,标签是双重),代码无法处理Try-Catch例程和崩溃。出现弹出窗口消息报告:“ abort()”,
谢谢您的帮助。
Is there a way to know the data type before of a hazelcat key before getting the key in Hazelcast 5.0 or later versions?
Specifically, my C++ code is:
hazelcast::client::imap* mapInstance = hz->get_map("MapName").get().get();;
boost::optional<bool> v;
try{
v = mapInstance->get<std::string, bool>("key2get").get();
}
catch (...) {
//print something
}
If the hazelcast key to get is a boolean, the code works perfectly. Otherwise (for example, the tag is a double), the code cannot handle the try-catch routine and crash. A pop-up windows message appears reporting: "Abort()"
Thank you in advance for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我刚刚在您的代码示例中意识到您未在行
Hazelcast :: client :: imap* mapinstance = hz.get_map(“ mapName”)。get()。get();
您获得临时shared_ptr的原始指针,一旦您进入下一行,它可能会立即变得无效。您可以通过以下方式更改它:auto mapInstance = hz.get_map(“ mapName”)。get();
这很可能是流产的原因。通常,您应该成功地使用catch(...)
来成功捕获异常,并且不会导致流产。C ++ API强烈键入,这意味着您需要在方法模板参数上提供期望的类型。但是,我们有一个解决方案来处理这些情况,您可能不知道之前不知道数据类型,否则榛子数据结构可能在值中可能具有混合的类型。对于这些,您可以使用特殊类型
typed_data
。这种特殊类型使您可以访问类型信息并根据接收类型做出运行时决策。您不需要使用异常处理。这是一个示例代码(刚刚提交了代码示例的PR 在这里也):I just realized in your code sample that you are not using the shared_ptr properly at line
hazelcast::client::imap* mapInstance = hz.get_map("MapName").get().get();
You get the raw pointer of the temporary shared_ptr which may become invalid as soon as you go to the next line. Can you please change it with:auto mapInstance = hz.get_map("MapName").get();
This is most probably the reason for the abort. Normally, you should be successfully catching the exception withcatch(...)
and it should not cause abort.The C++ API is strongly typed meaning that you require to provide the type you expect on the method template parameter. However, we have a solution for handling these situations where you may not know the data type before hand or the hazelcast data structure may have mixed types in the values. For these you can use the special type
typed_data
. This special type allows you to access the type information and make runtime decisions based on the received type. You do not need to use exception handling. Here is an example code (Just submitted a PR for a code sample here as well):