gpt4 book ai didi

c++ - GStreamer GError boost::: system::error_code?

转载 作者:行者123 更新时间:2023-12-02 10:29:41 24 4
gpt4 key购买 nike

寻找一种聪明的方式来转换gstreamer / glib GError:

struct GError {
GQuark domain;
gint code;
gchar* message;
};
到boost::system::error_code。通常,您可以创建一个类别并注册一个枚举来增强。 GStreamer使用枚举,但是代码以int形式返回。 GQuark是一个类别。
这主要是为了标准化整个应用程序中的错误处理。

最佳答案

GQuark is a category.


我想你的意思是比喻。

Quarks are associations between strings and integer identifiers. Given either the string or the Quark identifier it is possible to retrieve the other.


就像是内在的弦,也就是原子。因此, GQuark实际上代表一个字符串,您希望将其视为类别。好吧。
类别是静态的
好。通常。至少,类别是具有全局对象标识的单例。因此,假设您静态地知道需要哪些域,则可以为这些域创建类别。
将它们映射到域的文本表示形式可以实现最稳定的映射。
单一类别
首先,这里是一个类别:
using boost::system::error_code;
using boost::system::error_condition;

enum class MyCoreError {
failed, // a general error which doesn't fit in any other category. Make sure you add a custom message to the error call.
too_lazy, // do not use this except as a placeholder for deciding where to go while developing code.
not_implemented, // use this when you do not want to implement this functionality yet.
state_change, // used for state change errors.
pad, // used for pad-related errors.
thread, // used for thread-related errors.
negotiation, // used for negotiation-related errors.
event, // used for event-related errors.
seek, // used for seek-related errors.
caps, // used for caps-related errors.
tag, // used for negotiation-related errors.
missing_plugin, // used if a plugin is missing.
clock, // used for clock related errors.
disabled, // used if functionality has been disabled at compile time.
num_errors, // the number of core error types.
};

namespace boost::system { template<> struct is_error_code_enum<MyCoreError> : std::true_type {}; }

namespace detail {
class my_core_category : public boost::system::error_category {
public:
const char* name() const noexcept override {
return g_quark_to_string(GST_CORE_ERROR);
}
std::string message(int ev) const override {
switch (static_cast<MyCoreError>(ev)) {
case MyCoreError::failed: return "a general error which doesn't fit in any other category. Make sure you add a custom message to the error call.";
case MyCoreError::too_lazy: return "do not use this except as a placeholder for deciding where to go while developing code.";
case MyCoreError::not_implemented: return "use this when you do not want to implement this functionality yet.";
case MyCoreError::state_change: return "used for state change errors.";
case MyCoreError::pad: return "used for pad-related errors.";
case MyCoreError::thread: return "used for thread-related errors.";
case MyCoreError::negotiation: return "used for negotiation-related errors.";
case MyCoreError::event: return "used for event-related errors.";
case MyCoreError::seek: return "used for seek-related errors.";
case MyCoreError::caps: return "used for caps-related errors.";
case MyCoreError::tag: return "used for negotiation-related errors.";
case MyCoreError::missing_plugin: return "used if a plugin is missing.";
case MyCoreError::clock: return "used for clock related errors.";
case MyCoreError::disabled: return "used if functionality has been disabled at compile time.";
case MyCoreError::num_errors: return "the number of core error types.";
default: return "unknown core error";
}
}
error_condition default_error_condition(int ev) const noexcept override {
return error_condition{ ev, *this };
}
bool equivalent(int ev, error_condition const& condition) const noexcept override {
return condition.value() == ev && &condition.category() == this;
}
bool equivalent(error_code const& error, int ev) const noexcept override {
return error.value() == ev && &error.category() == this;
}
};
}
error_code make_error_code(MyCoreError se)
{
static detail::my_core_category const cat{};
return error_code{static_cast<std::underlying_type<MyCoreError>::type>(se), cat};
}
现在你可以
auto code = MyCoreError::missing_plugin;
throw boost::system::system_error(make_error_code(code));
它会神奇地做正确的事。
一般错误翻译:
对于从API错误转换的一般情况:
boost::system::error_code make_error_code(GError const& err) {
if (err.domain == GST_CORE_ERROR) {
return make_error_code(MyCoreError(err.code));
}
if (err.domain == GST_LIBRARY_ERROR) {
return make_error_code(MyLibraryError(err.code));
}
if (err.domain == GST_RESOURCE_ERROR) {
return make_error_code(MyResourceError(err.code));
}
if (err.domain == GST_STREAM_ERROR) {
return make_error_code(MyStreamError(err.code));
}
// decide on how to represent unknown errors? Just throw, or translate into
// MyCoreError::failed?
}

关于c++ - GStreamer GError boost::: system::error_code?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62782777/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com