• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2019, The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef TEEUI_STATIC_VEC_H_
19 #define TEEUI_STATIC_VEC_H_
20 
21 #include <stddef.h>  // for size_t
22 
23 #ifdef TEEUI_USE_STD_VECTOR
24 #include <vector>
25 #endif
26 
27 namespace teeui {
28 
29 /**
30  * teeui::static_vec leads a double life.
31  *
32  * When compiling with TEEUI_USE_STD_VECTOR it is just an alias for std::vector. HAL services using
33  * this library must use this option for safe handling of message buffers.
34  *
35  * When compiling without TEEUI_USE_STD_VECTOR this class works more like a span that does not
36  * actually own the buffer if wraps. This is the behavior expected by generic_operation.h, which
37  * is used inside a heap-less implementation of a confirmationui trusted app.
38  */
39 #ifndef TEEUI_USE_STD_VECTOR
40 template <typename T> class static_vec {
41   private:
42     T* data_;
43     size_t size_;
44 
45   public:
static_vec()46     static_vec() : data_(nullptr), size_(0) {}
static_vec(T * begin,T * end)47     static_vec(T* begin, T* end) : data_(begin), size_(end - begin) {}
static_vec(T (& arr)[s])48     template <size_t s> static_vec(T (&arr)[s]) : data_(&arr[0]), size_(s) {}
49     static_vec(const static_vec&) = default;
50     static_vec(static_vec&&) = default;
51     static_vec& operator=(const static_vec&) = default;
52     static_vec& operator=(static_vec&&) = default;
53 
data()54     T* data() { return data_; }
data()55     const T* data() const { return data_; }
size()56     size_t size() const { return size_; }
57 
begin()58     T* begin() { return data_; }
end()59     T* end() { return data_ + size_; }
begin()60     const T* begin() const { return data_; }
end()61     const T* end() const { return data_ + size_; }
62 };
63 #else
64 template <typename T> using static_vec = std::vector<T>;
65 #endif
66 
67 }  // namespace teeui
68 
69 #endif  // TEEUI_STATIC_VEC_H_
70