BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSTwoVector.hh
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) Royal Holloway,
3University of London 2001 - 2023.
4
5This file is part of BDSIM.
6
7BDSIM is free software: you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published
9by the Free Software Foundation version 3 of the License.
10
11BDSIM is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with BDSIM. If not, see <http://www.gnu.org/licenses/>.
18*/
19#ifndef BDSTWOVECTOR_H
20#define BDSTWOVECTOR_H
21#include "BDSException.hh"
22
23#include "globals.hh" // geant4 types / globals
24
25#include <ostream>
26
36template <typename T>
38{
39public:
42 {values[0] = 0; values[1] = 0;}
43
46 {values[0] = x; values[1] = y;}
47
50 {
51 for (G4int i : {0, 1})
52 {values[i] = other.values[i];}
53 }
54
57 {
58 if (this == &rhs)
59 {return *this;}
60 values[0] = rhs.values[0];
61 values[1] = rhs.values[1];
62 return *this;
63 }
64
66 T& operator[](const G4int index)
67 {
68 if (index > 1)
69 {throw BDSException("BDSTwoVector::operator[]> index outside array");}
70 return values[index];
71 }
72
73 // Access a single element.
74 const T& operator[](const G4int index) const
75 {
76 if (index > 1)
77 {throw BDSException("BDSTwoVector::operator[]> index outside array");}
78 return values[index];
79 }
80
82 inline const T& x() const {return values[0];}
83 inline const T& y() const {return values[1];}
85
87 friend std::ostream& operator<< (std::ostream& out, BDSTwoVector const &v)
88 {out << "(" << v.values[0] << ", " << v.values[1] << ")"; return out;}
89
90private:
92 T values[2];
93};
94
95
96#endif
General exception with possible name of object and message.
Definition: BDSException.hh:35
Simple two vector that's templated so the right type can be used.
Definition: BDSTwoVector.hh:38
BDSTwoVector(const BDSTwoVector &other)
Copy constructor.
Definition: BDSTwoVector.hh:49
BDSTwoVector(T x, T y)
Alternate constructor.
Definition: BDSTwoVector.hh:45
friend std::ostream & operator<<(std::ostream &out, BDSTwoVector const &v)
Output stream.
Definition: BDSTwoVector.hh:87
T & operator[](const G4int index)
Access / set a single element.
Definition: BDSTwoVector.hh:66
BDSTwoVector()
Default constructor.
Definition: BDSTwoVector.hh:41
BDSTwoVector & operator=(const BDSTwoVector &rhs)
Assignment operator.
Definition: BDSTwoVector.hh:56
const T & y() const
Accessor by name.
Definition: BDSTwoVector.hh:83
const T & x() const
Accessor by name.
Definition: BDSTwoVector.hh:82
T values[2]
Member data - x,y.
Definition: BDSTwoVector.hh:92