PandaRoot
PndTrkVectors.h
Go to the documentation of this file.
1 //****************************************************************************
2 //* This file is part of PandaRoot. *
3 //* *
4 //* PandaRoot is distributed under the terms of the *
5 //* GNU General Public License (GPL) version 3, *
6 //* copied verbatim in the file "LICENSE". *
7 //* *
8 //* Copyright (C) 2006 - 2024 FAIR GmbH and copyright holders of PandaRoot *
9 //* The copyright holders are listed in the file "COPYRIGHTHOLDERS". *
10 //* The authors are listed in the file "AUTHORS". *
11 //****************************************************************************
12 
13 #ifndef PndTrkVectors_H
14 #define PndTrkVectors_H 1
15 // Root includes
16 #include "TROOT.h"
17 
18 #include <stdlib.h>
19 #include <iostream>
20 
21 using namespace std;
22 
23 // this class is useful to mimic the [] operator
24 // of an array or the at() operator of the std::vector
25 // but it contains also the boundary check. If the boundary
26 // are violated an error message is printed and a call
27 // to the exit function with value -1 is performed.
28 // The argument for the constructor are :
29 // p --> pointer to an EXISTING array(of ANY type) to be mimiced;
30 // dim --> dimension of the array to be mimiced;
31 // nam --> string containing the name of the Vec class
32 // that mimics the array.
33 
34 // The typical statements for producing the Vec mimicing
35 // an existing array of type MYTYPE is :
36 
37 /*
38  MYTYPE array[100]; the array must physically exist in memory;
39  Vec <MYTYPE> myvec(array, 100, "myvec");
40 */
41 
42 template <class T>
43 class Vec {
44 
45  public:
46  int dimension;
47  TString name;
48  T *array;
49 
50  // constructor;
51  Vec(T *p, int dim, TString nam)
52  {
53  if (dim < 0 || dim > 1000000) {
54  cout << "PndTrkVectors::Vec the dimension of the array " << nam << " is " << dim << " and not acceptable; exit(-2) the program.\n";
55  exit(-2);
56  }
57  dimension = dim;
58  array = p;
59  name = nam;
60  };
61 
62  ~Vec() { ; };
63 
64  // function at();
65  T &at(int index)
66  {
67 
68  if (index >= 0 && index < dimension) {
69  T &alias = array[index];
70  return alias;
71  }
72  cout << "PndTrkVectors:: array " << name << ": index = " << index << " and it is out of bounds [from 0 to " << dimension - 1 << " included]; exiting the process.\n";
73  exit(-1);
74  };
75 
76  // overloaded operator [] ; the functionality is identical to the at() function;
77  T &operator[](int index)
78  {
79 
80  T &alias = at(index);
81  return alias;
82  };
83 };
84 
85 #endif
T & operator[](int index)
Definition: PndTrkVectors.h:77
STL namespace.
TString name
Definition: PndTrkVectors.h:47
Vec(T *p, int dim, TString nam)
Definition: PndTrkVectors.h:51
~Vec()
Definition: PndTrkVectors.h:62
T * array
Definition: PndTrkVectors.h:48
int dimension
Definition: PndTrkVectors.h:46
T & at(int index)
Definition: PndTrkVectors.h:65