1 // This file is part of OpenCV project. 2 // It is subject to the license terms in the LICENSE file found in the top-level directory 3 // of this distribution and at http://opencv.org/license.html. 4 // 5 // Tencent is pleased to support the open source community by making WeChat QRCode available. 6 // Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved. 7 // 8 // Modified from ZXing. Copyright ZXing authors. 9 // Licensed under the Apache License, Version 2.0 (the "License"). 10 11 #ifndef __ZXING_COMMON_ARRAY_HPP__ 12 #define __ZXING_COMMON_ARRAY_HPP__ 13 14 #include "counted.hpp" 15 16 namespace zxing { 17 18 template <typename T> 19 class Array : public Counted { 20 protected: 21 public: 22 std::vector<T> values_; Array()23 Array() {} Array(int n)24 explicit Array(int n) : Counted(), values_(n, T()) {} Array(T const * ts,int n)25 Array(T const *ts, int n) : Counted(), values_(ts, ts + n) {} Array(T const * ts,T const * te)26 Array(T const *ts, T const *te) : Counted(), values_(ts, te) {} Array(T v,int n)27 Array(T v, int n) : Counted(), values_(n, v) {} Array(std::vector<T> & v)28 explicit Array(std::vector<T> &v) : Counted(), values_(v) {} Array(Array<T> & other)29 Array(Array<T> &other) : Counted(), values_(other.values_) {} Array(Array<T> * other)30 explicit Array(Array<T> *other) : Counted(), values_(other->values_) {} ~Array()31 virtual ~Array() {} operator =(const Array<T> & other)32 Array<T> &operator=(const Array<T> &other) { 33 values_ = other.values_; 34 return *this; 35 } operator =(const std::vector<T> & array)36 Array<T> &operator=(const std::vector<T> &array) { 37 values_ = array; 38 return *this; 39 } operator [](int i) const40 T const &operator[](int i) const { return values_[i]; } operator [](int i)41 T &operator[](int i) { return values_[i]; } size() const42 int size() const { return values_.size(); } empty() const43 bool empty() const { return values_.size() == 0; } values() const44 std::vector<T> const &values() const { return values_; } values()45 std::vector<T> &values() { return values_; } 46 data()47 T *data() { 48 // return values_.data(); 49 return &values_[0]; 50 } append(T value)51 void append(T value) { values_.push_back(value); } 52 }; 53 54 template <typename T> 55 class ArrayRef : public Counted { 56 private: 57 public: 58 Array<T> *array_; ArrayRef()59 ArrayRef() : array_(0) {} ArrayRef(int n)60 explicit ArrayRef(int n) : array_(0) { reset(new Array<T>(n)); } ArrayRef(T * ts,int n)61 ArrayRef(T *ts, int n) : array_(0) { reset(new Array<T>(ts, n)); } ArrayRef(Array<T> * a)62 explicit ArrayRef(Array<T> *a) : array_(0) { reset(a); } ArrayRef(const ArrayRef & other)63 ArrayRef(const ArrayRef &other) : Counted(), array_(0) { reset(other.array_); } 64 ~ArrayRef()65 ~ArrayRef() { 66 if (array_) { 67 array_->release(); 68 } 69 array_ = 0; 70 } 71 operator [](int i) const72 T const &operator[](int i) const { return (*array_)[i]; } 73 operator [](int i)74 T &operator[](int i) { return (*array_)[i]; } 75 reset(Array<T> * a)76 void reset(Array<T> *a) { 77 if (a) { 78 a->retain(); 79 } 80 if (array_) { 81 array_->release(); 82 } 83 array_ = a; 84 } reset(const ArrayRef<T> & other)85 void reset(const ArrayRef<T> &other) { reset(other.array_); } operator =(const ArrayRef<T> & other)86 ArrayRef<T> &operator=(const ArrayRef<T> &other) { 87 reset(other); 88 return *this; 89 } operator =(Array<T> * a)90 ArrayRef<T> &operator=(Array<T> *a) { 91 reset(a); 92 return *this; 93 } 94 operator *() const95 Array<T> &operator*() const { return *array_; } 96 operator ->() const97 Array<T> *operator->() const { return array_; } 98 operator bool() const99 operator bool() const { return array_ != 0; } operator !() const100 bool operator!() const { return array_ == 0; } 101 data()102 T *data() { return array_->data(); } 103 clear()104 void clear() { 105 T *ptr = array_->data(); 106 memset(ptr, 0, array_->size()); 107 } append(T value)108 void append(T value) { array_->append(value); } 109 }; 110 111 } // namespace zxing 112 113 #endif // __ZXING_COMMON_ARRAY_HPP__ 114