Jpp  master_rocky
the software that should make you happy
JLocation.hh
Go to the documentation of this file.
1 #ifndef __JDETECTOR__JLOCATION__
2 #define __JDETECTOR__JLOCATION__
3 
4 #include <istream>
5 #include <ostream>
6 #include <iomanip>
7 #include <string>
8 
9 #include "JLang/JComparable.hh"
10 #include "JLang/JString.hh"
11 #include "JLang/JManip.hh"
12 #include "JIO/JSerialisable.hh"
13 #include "Jeep/JPrint.hh"
14 
15 
16 /**
17  * \file
18  *
19  * Logical location of module.
20  * \author mdejong
21  */
22 namespace JDETECTOR {}
23 namespace JPP { using namespace JDETECTOR; }
24 
25 namespace JDETECTOR {
26 
27  using JLANG::JComparable;
28  using JIO::JReader;
29  using JIO::JWriter;
30 
31 
32  /**
33  * Logical location of module.
34  *
35  * The logical location of a module consists of a string and floor number.\n
36  * This class implements the JLANG::JComparable interface.
37  */
38  class JLocation :
39  public JComparable<JLocation>
40  {
41  public:
42  /**
43  * Default constructor.
44  */
46  string(-1),
47  floor (-1)
48  {}
49 
50 
51  /**
52  * Constructor.
53  *
54  * \param string string
55  * \param floor floor
56  */
57  JLocation(const int string,
58  const int floor)
59  {
60  this->string = string;
61  this->floor = floor;
62  }
63 
64 
65  /**
66  * Get location.
67  *
68  * \return location
69  */
70  const JLocation& getLocation() const
71  {
72  return static_cast<const JLocation&>(*this);
73  }
74 
75 
76  /**
77  * Get location.
78  *
79  * \return location
80  */
82  {
83  return static_cast<JLocation&>(*this);
84  }
85 
86 
87  /**
88  * Set location.
89  *
90  * \param location location
91  */
92  void setLocation(const JLocation& location)
93  {
94  static_cast<JLocation&>(*this) = location;
95  }
96 
97 
98  /**
99  * Convert module location to string.
100  *
101  * \return string
102  */
103  std::string toString() const
104  {
105  return toString("% %");
106  }
107 
108 
109  /**
110  * Convert module loation to string.
111  *
112  * The targets <tt>target</tt> in the format string <tt>fmt</tt> are
113  * consecutively replaced by <tt>floor</tt> and <tt>string</tt>.
114  *
115  * \param fmt format
116  * \param target target
117  * \return string
118  */
119  std::string toString(const std::string& fmt, const std::string target = "%") const
120  {
121  JLANG::JString buffer(fmt);
122 
123  buffer.replace(target, string, 1);
124  buffer.replace(target, floor, 1);
125 
126  return buffer;
127  }
128 
129 
130  /**
131  * Get string number.
132  *
133  * \return string number
134  */
135  int getString() const
136  {
137  return string;
138  }
139 
140 
141  /**
142  * Get floor number.
143  *
144  * \return floor number
145  */
146  int getFloor() const
147  {
148  return floor;
149  }
150 
151 
152  /**
153  * Less than method.
154  *
155  * \param location module location
156  * \result true if first location before second location; else false
157  */
158  bool less(const JLocation& location) const
159  {
160  if (this->getString() == location.getString())
161  return this->getFloor() < location.getFloor();
162  else
163  return this->getString() < location.getString();
164  }
165 
166 
167  /**
168  * Read module location from input.
169  *
170  * \param in input stream
171  * \param location module location
172  * \return input stream
173  */
174  friend inline std::istream& operator>>(std::istream& in, JLocation& location)
175  {
176  in >> location.string;
177  in >> location.floor;
178 
179  return in;
180  }
181 
182 
183  /**
184  * Write module location to output.
185  *
186  * \param out output stream
187  * \param location module location
188  * \return output stream
189  */
190  friend inline std::ostream& operator<<(std::ostream& out, const JLocation& location)
191  {
192  using namespace std;
193 
194  out << setw(4) << location.string;
195  out << ' ';
196  out << setw(2) << location.floor;
197 
198  return out;
199  }
200 
201 
202  /**
203  * Read module location from input.
204  *
205  * \param in reader
206  * \param location module location
207  * \return reader
208  */
209  friend inline JReader& operator>>(JReader& in, JLocation& location)
210  {
211  in >> location.string;
212  in >> location.floor;
213 
214  return in;
215  }
216 
217 
218  /**
219  * Write module location to output.
220  *
221  * \param out writer
222  * \param location module location
223  * \return writer
224  */
225  friend inline JWriter& operator<<(JWriter& out, const JLocation& location)
226  {
227  out << location.string;
228  out << location.floor;
229 
230  return out;
231  }
232 
233 
234  protected:
235  int string;
236  int floor;
237  };
238 
239 
240  /**
241  * Get module label for monitoring and other applications.\n
242  * The format is "(XXXX,YY)", where XXXX is the string number and YY the floor.
243  *
244  * \param location module location
245  * \return label
246  */
247  inline std::string getLabel(const JLocation& location)
248  {
249  using namespace std;
250  using namespace JPP;
251 
252  return MAKE_STRING("(" << FILL(4,'0') << location.getString() << "." << FILL(2,'0') << location.getFloor() << ")");
253  }
254 }
255 
256 #endif
I/O manipulators.
I/O formatting auxiliaries.
#define MAKE_STRING(A)
Make string.
Definition: JPrint.hh:63
Logical location of module.
Definition: JLocation.hh:40
JLocation()
Default constructor.
Definition: JLocation.hh:45
friend JWriter & operator<<(JWriter &out, const JLocation &location)
Write module location to output.
Definition: JLocation.hh:225
std::string toString(const std::string &fmt, const std::string target="%") const
Convert module loation to string.
Definition: JLocation.hh:119
friend std::istream & operator>>(std::istream &in, JLocation &location)
Read module location from input.
Definition: JLocation.hh:174
JLocation & getLocation()
Get location.
Definition: JLocation.hh:81
int getFloor() const
Get floor number.
Definition: JLocation.hh:146
JLocation(const int string, const int floor)
Constructor.
Definition: JLocation.hh:57
std::string toString() const
Convert module location to string.
Definition: JLocation.hh:103
int getString() const
Get string number.
Definition: JLocation.hh:135
friend std::ostream & operator<<(std::ostream &out, const JLocation &location)
Write module location to output.
Definition: JLocation.hh:190
bool less(const JLocation &location) const
Less than method.
Definition: JLocation.hh:158
const JLocation & getLocation() const
Get location.
Definition: JLocation.hh:70
void setLocation(const JLocation &location)
Set location.
Definition: JLocation.hh:92
friend JReader & operator>>(JReader &in, JLocation &location)
Read module location from input.
Definition: JLocation.hh:209
Interface for binary input.
Interface for binary output.
Wrapper class around STL string class.
Definition: JString.hh:29
JString & replace(const char target, const char replacement, const std::size_t max=std::numeric_limits< std::size_t >::max())
Replace characters.
Definition: JString.hh:170
file Auxiliary data structures and methods for detector calibration.
Definition: JAnchor.hh:12
std::string getLabel(const JLocation &location)
Get module label for monitoring and other applications.
Definition: JLocation.hh:247
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JSTDTypes.hh:14
Auxiliary data structure for sequence of same character.
Definition: JManip.hh:330
Template definition of auxiliary base class for comparison of data structures.
Definition: JComparable.hh:139