1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-21 19:33:03 +04:00

Merge pull request #28909 from chacha21:autobuffer_api

Better AutoBuffer API #28909

Mimic std::vector<> to help replacing std::vector<T> by cv::AutoBuffer<T> when possible

### Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [ ] There is a reference to the original bug report and related work
- [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
This commit is contained in:
Pierre Chatelier
2026-05-05 15:31:13 +02:00
committed by GitHub
parent f7a467f882
commit 808d2d596c
+33 -1
View File
@@ -102,11 +102,18 @@ template<typename _Tp, size_t fixed_size = 1024/sizeof(_Tp)+8> class AutoBuffer
{
public:
typedef _Tp value_type;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef const _Tp* const_iterator;
typedef _Tp* iterator;
typedef const _Tp& const_reference;
typedef _Tp& reference;
//! the default constructor
AutoBuffer();
//! constructor taking the real buffer size
explicit AutoBuffer(size_t _size);
AutoBuffer(size_t _size, const _Tp& value);
//! the copy constructor
AutoBuffer(const AutoBuffer<_Tp, fixed_size>& buf);
@@ -140,7 +147,25 @@ public:
//! returns a reference to the element at specified location. No bounds checking is performed in Release builds.
inline const _Tp& operator[] (size_t i) const { CV_DbgCheckLT(i, sz, "out of range"); return ptr[i]; }
#endif
public:
inline iterator begin() { return data(); }
inline const_iterator begin() const { return data(); }
inline const_iterator cbegin() const { return begin(); }
inline iterator end() { return data()+size(); }
inline const_iterator end() const { return data()+size(); }
inline const_iterator cend() const { return end(); }
public:
inline bool empty() const { return !size(); }
inline void clear() {resize(0);}
inline const_reference front() const { return (*this)[0] ;}
inline reference front() { return (*this)[0] ;}
inline const_reference back() const { CV_DbgCheckGT(sz, (size_t)0, "out of range"); return (*this)[size()-1] ;}
inline reference back() { CV_DbgCheckGT(sz, (size_t)0, "out of range"); return (*this)[size()-1] ;}
public:
inline void push_back( const _Tp& value ) {resize(size()+1); back() = value;}
inline void push_back( _Tp&& value ) {resize(size()+1); back() = std::move(value);}
inline void emplace_back( _Tp&& value ) {push_back(value);}
inline void pop_back() {CV_DbgCheckGT(sz, (size_t)0, "out of range"); resize(size()-1);}
protected:
//! pointer to the real buffer, can point to buf if the buffer is small enough
_Tp* ptr;
@@ -1054,6 +1079,13 @@ AutoBuffer<_Tp, fixed_size>::AutoBuffer(size_t _size)
allocate(_size);
}
template<typename _Tp, size_t fixed_size> inline
AutoBuffer<_Tp, fixed_size>::AutoBuffer(size_t _size, const _Tp& value)
:AutoBuffer<_Tp, fixed_size>(_size)
{
std::fill(begin(), end(), value);
}
template<typename _Tp, size_t fixed_size> inline
AutoBuffer<_Tp, fixed_size>::AutoBuffer(const AutoBuffer<_Tp, fixed_size>& abuf )
{