1/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15/// \file
16/// This provides a few C++ helpers that are useful for manipulating C
17/// structures in C++.
18#ifndef TENSORFLOW_LITE_CONTEXT_UTIL_H_
19#define TENSORFLOW_LITE_CONTEXT_UTIL_H_
20
21#include <stddef.h>
22
23#include "tensorflow/lite/c/common.h"
24
25namespace tflite {
26
27/// Provides a range iterable wrapper for TfLiteIntArray* (C lists) that TfLite
28/// C api uses.
29// Can't use the google array_view, since we can't depend on even
30// absl for embedded device reasons.
31class TfLiteIntArrayView {
32 public:
33 /// Construct a view of a TfLiteIntArray*. Note, `int_array` should be
34 /// non-null and this view does not take ownership of it.
35 explicit TfLiteIntArrayView(const TfLiteIntArray* int_array)
36 : int_array_(int_array) {}
37
38 TfLiteIntArrayView(const TfLiteIntArrayView&) = default;
39 TfLiteIntArrayView& operator=(const TfLiteIntArrayView& rhs) = default;
40
41 typedef const int* const_iterator;
42 const_iterator begin() const { return int_array_->data; }
43 const_iterator end() const { return &int_array_->data[int_array_->size]; }
44 size_t size() const { return end() - begin(); }
45 int operator[](size_t pos) const { return int_array_->data[pos]; }
46
47 private:
48 const TfLiteIntArray* int_array_;
49};
50
51} // namespace tflite
52
53#endif // TENSORFLOW_LITE_CONTEXT_UTIL_H_
54