|
10 | 10 |
|
11 | 11 | #include "gTrueInfoData.h" |
12 | 12 | #include "gDigitizedData.h" |
| 13 | + |
| 14 | +// c++ |
| 15 | +#include <memory> |
13 | 16 | #include <vector> |
14 | 17 |
|
| 18 | + |
15 | 19 | class GDataCollection { |
16 | 20 | public: |
17 | 21 | /** |
18 | 22 | * \brief Constructs a GDataCollection. |
19 | 23 | * \param logger Pointer to a GLogger instance. |
20 | 24 | */ |
21 | | - explicit GDataCollection(std::shared_ptr<GLogger> logger) : log(logger) { |
22 | | - log->debug(CONSTRUCTOR, "GDataCollection"); |
23 | | - trueInfosData = new std::vector<GTrueInfoData*>; |
24 | | - digitizedData = new std::vector<GDigitizedData*>; |
25 | | - } |
| 25 | + explicit GDataCollection(std::shared_ptr<GLogger> logger) |
| 26 | + : log(logger) { log->debug(CONSTRUCTOR, "GDataCollection"); } |
26 | 27 |
|
27 | 28 | /** |
28 | 29 | * \brief Destructor for GDataCollection. |
29 | 30 | * |
30 | | - * Deletes all stored hit data and the associated containers. |
| 31 | + * Smart pointers clean up automatically. |
31 | 32 | */ |
32 | | - ~GDataCollection() { |
33 | | - for (auto* hit : (*trueInfosData)) { delete hit; } |
34 | | - for (auto* hit : (*digitizedData)) { delete hit; } |
35 | | - log->debug(DESTRUCTOR, "GDataCollection"); |
36 | | - delete trueInfosData; |
37 | | - delete digitizedData; |
38 | | - } |
| 33 | + ~GDataCollection() { if (log) log->debug(DESTRUCTOR, "GDataCollection"); } |
39 | 34 |
|
40 | 35 | /** |
41 | 36 | * \brief Adds true hit information data. |
42 | | - * \param data Pointer to GTrueInfoData. |
| 37 | + * \param data Unique pointer to GTrueInfoData. |
43 | 38 | */ |
44 | | - void addTrueInfoData(GTrueInfoData* data) const { |
| 39 | + void addTrueInfoData(std::unique_ptr<GTrueInfoData> data) { |
45 | 40 | log->debug(NORMAL, " adding hit to trueInfosData with identity: ", data->getIdentityString()); |
46 | | - trueInfosData->push_back(data); |
| 41 | + trueInfosData.push_back(std::move(data)); // taking ownership of the unique_ptr |
47 | 42 | } |
48 | 43 |
|
49 | 44 | /** |
50 | 45 | * \brief Adds digitized hit data. |
51 | | - * \param data Pointer to GDigitizedData. |
| 46 | + * \param data Unique pointer to GDigitizedData. |
52 | 47 | */ |
53 | | - void addDigitizedData(GDigitizedData* data) const { |
| 48 | + void addDigitizedData(std::unique_ptr<GDigitizedData> data) { |
54 | 49 | log->debug(NORMAL, " adding hit to digitizedData with identity: ", data->getIdentityString()); |
55 | | - digitizedData->push_back(data); |
| 50 | + digitizedData.push_back(std::move(data)); // taking ownership of the unique_ptr |
56 | 51 | } |
57 | 52 |
|
58 | 53 | /** |
59 | | - * \brief Returns the vector of true hit information data. |
60 | | - * \return Pointer to the vector of GTrueInfoData pointers. |
| 54 | + * \brief Provides read-only access to the stored true hit data. |
| 55 | + * |
| 56 | + * Returns a constant reference to the internal vector of unique pointers |
| 57 | + * to GTrueInfoData objects. Ownership of the data remains with this class. |
| 58 | + * Callers may inspect the data via the pointers but must not modify or |
| 59 | + * take ownership of them. |
| 60 | + * |
| 61 | + * \return Const reference to the vector of unique_ptr<GTrueInfoData>. |
61 | 62 | */ |
62 | | - [[nodiscard]] inline const std::vector<GTrueInfoData*>* getTrueInfoData() const { return trueInfosData; } |
| 63 | + [[nodiscard]] inline const std::vector<std::unique_ptr<GTrueInfoData>>& getTrueInfoData() const { return trueInfosData; } |
63 | 64 |
|
64 | 65 | /** |
65 | | - * \brief Returns the vector of digitized hit data. |
66 | | - * \return Pointer to the vector of GDigitizedData pointers. |
| 66 | + * \brief Provides read-only access to the stored digitized hit data. |
| 67 | + * |
| 68 | + * Returns a constant reference to the internal vector of unique pointers |
| 69 | + * to GDigitizedData objects. Ownership of the data remains with this class. |
| 70 | + * Callers may read the data but must not modify or transfer ownership. |
| 71 | + * |
| 72 | + * \return Const reference to the vector of unique_ptr<GDigitizedData>. |
67 | 73 | */ |
68 | | - [[nodiscard]] inline const std::vector<GDigitizedData*>* getDigitizedData() const { return digitizedData; } |
| 74 | + [[nodiscard]] inline const std::vector<std::unique_ptr<GDigitizedData>>& getDigitizedData() const { return digitizedData; } |
69 | 75 |
|
70 | 76 | private: |
71 | | - std::vector<GTrueInfoData*>* trueInfosData = nullptr; ///< Vector of true hit data. |
72 | | - std::vector<GDigitizedData*>* digitizedData = nullptr; ///< Vector of digitized hit data. |
73 | | - std::shared_ptr<GLogger> log; ///< Logger instance |
74 | | -}; |
| 77 | + std::vector<std::unique_ptr<GTrueInfoData>> trueInfosData; ///< Vector of true hit data. |
| 78 | + std::vector<std::unique_ptr<GDigitizedData>> digitizedData; ///< Vector of digitized hit data. |
| 79 | + std::shared_ptr<GLogger> log; ///< Logger instance |
75 | 80 |
|
| 81 | +}; |
0 commit comments