1 /*
2  * Copyright 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef STAGEFRIGHT_CODEC2_ERRNO_UTILS_H_
18 #define STAGEFRIGHT_CODEC2_ERRNO_UTILS_H_
19 
20 #include <errno.h>
21 #include <C2.h>
22 
23 // standard ERRNO mappings
24 template<int N> constexpr c2_status_t _c2_errno2status_impl();
25 template<> constexpr c2_status_t _c2_errno2status_impl<0>()       { return C2_OK; }
26 template<> constexpr c2_status_t _c2_errno2status_impl<EINVAL>()  { return C2_BAD_VALUE; }
27 template<> constexpr c2_status_t _c2_errno2status_impl<EACCES>()  { return C2_REFUSED; }
28 template<> constexpr c2_status_t _c2_errno2status_impl<EPERM>()   { return C2_REFUSED; }
29 template<> constexpr c2_status_t _c2_errno2status_impl<ENOMEM>()  { return C2_NO_MEMORY; }
30 
31 // map standard errno-s to the equivalent c2_status_t
32 template<int... N> struct _c2_map_errno_impl;
33 template<int E, int ... N> struct _c2_map_errno_impl<E, N...> {
34     static c2_status_t map(int result) {
35         if (result == E) {
36             return _c2_errno2status_impl <E>();
37         } else {
38             return _c2_map_errno_impl<N...>::map(result);
39         }
40     }
41 };
42 template<> struct _c2_map_errno_impl<> {
43     static c2_status_t map(int result) {
44         return result == 0 ? C2_OK : C2_CORRUPTED;
45     }
46 };
47 
48 template<int... N>
49 c2_status_t c2_map_errno(int result) {
50     return _c2_map_errno_impl<N...>::map(result);
51 }
52 
53 #endif // STAGEFRIGHT_CODEC2_ERRNO_UTILS_H_
54 
55