File indexing completed on 2025-01-05 04:37:28

0001 /*
0002     SPDX-FileCopyrightText: 2005 Joris Guisson <joris.guisson@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 #ifndef BTARRAY_H
0007 #define BTARRAY_H
0008 
0009 #include "constants.h"
0010 #include <ktorrent_export.h>
0011 
0012 namespace bt
0013 {
0014 /**
0015  * @author Joris Guisson
0016  *
0017  * Template array classes, makes creating dynamic buffers easier
0018  * and safer.
0019  */
0020 template<class T> class KTORRENT_EXPORT Array
0021 {
0022     Uint32 num;
0023     T *data;
0024 
0025 public:
0026     Array(Uint32 num = 0)
0027         : num(num)
0028         , data(nullptr)
0029     {
0030         if (num > 0)
0031             data = new T[num];
0032     }
0033 
0034     ~Array()
0035     {
0036         delete[] data;
0037     }
0038 
0039     T &operator[](Uint32 i)
0040     {
0041         return data[i];
0042     }
0043     const T &operator[](Uint32 i) const
0044     {
0045         return data[i];
0046     }
0047 
0048     operator const T *() const
0049     {
0050         return data;
0051     }
0052     operator T *()
0053     {
0054         return data;
0055     }
0056 
0057     /// Get the number of elements in the array
0058     Uint32 size() const
0059     {
0060         return num;
0061     }
0062 
0063     /**
0064      * Fill the array with a value
0065      * @param val The value
0066      */
0067     void fill(T val)
0068     {
0069         for (Uint32 i = 0; i < num; i++)
0070             data[i] = val;
0071     }
0072 };
0073 
0074 }
0075 
0076 #endif