summaryrefslogtreecommitdiff
path: root/src/lib/eina_cxx/eina_error.hh
blob: 1199e694b9bdea9e49ae88cd84d733dd4aaad535 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#ifndef _EINA_ERROR_HH
#define _EINA_ERROR_HH

#include <Eina.h>

#include <system_error>

namespace efl { namespace eina {

using std::errc;
using std::system_error;
using std::error_code;
using std::error_condition;
typedef std::error_category system_error_category;

inline system_error_category const& get_generic_category()
{
  return ::std::generic_category();
}
inline system_error_category const& get_system_category()
{
  return ::std::system_category();
}

enum error_type {};

struct error_category : system_error_category
{
  const char* name() const throw()
  {
    return "eina";
  }

  bool equivalent(int code, eina::error_condition const& condition) const throw()
  {
    return code == condition.value();
  }

  bool equivalent(eina::error_code const& code, int condition) const throw()
  {
    return code.value() == condition;
  }

  std::string message(int condition) const
  {
    const char* e = ::eina_error_msg_get(condition);
    return e? e : "::eina_error_msg_get returned NULL. No error message available";
  }
};

inline eina::system_error_category& eina_error_category()
{
  static error_category _error_category;
  return _error_category;
}

inline eina::error_code get_error_code()
{
  Eina_Error error = eina_error_get();
  if(error)
    {
      eina_error_set(0);
      return eina::error_code(error, eina_error_category());
    }
  else
    return eina::error_code();
}

inline eina::error_condition get_error_condition()
{
  Eina_Error error = eina_error_get();
  if(error)
    {
      eina_error_set(0);
      return eina::error_condition(error, eina_error_category());
    }
  else
    return eina::error_condition();
}

inline error_type get_error_code_enum()
{
  return static_cast<error_type>( ::eina_error_get() );
}


inline void throw_on_error()
{
  eina::error_code ec = get_error_code();
  if(ec)
  {
    throw eina::system_error(ec, "EFL Eina Error");
  }
}

} }

namespace std {

template <> struct is_error_condition_enum< ::efl::eina::error_type> : true_type {};
template <> struct is_error_code_enum< ::efl::eina::error_type> : true_type {};

}

#endif