Website Summary Files Commits Branches @commit @diff @stats
path: root / src / Models / Ticket.cpp
blob: bc8ef84d98b472286d5b9348dc650f7120513f1c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "Ticket.h"

Ticket::Ticket() : Ticket(0, 0) {}

Ticket::Ticket(unsigned row, unsigned seat) {
	this->row = row;
	this->seat = seat;
}

unsigned Ticket::get_row() const {
	return row;
}

unsigned Ticket::get_seat() const {
	return seat;
}

/*! \param istr An input stream
 *
 * Directly reads bytes from stream (calls read() function).
 * 
 * \remark Doesn't alter the stream in any other way.
 * \note Best used with binary ifstream
 */
void Ticket::read(std::istream& istr) {
	istr.read((char*)&row, sizeof(row));
	istr.read((char*)&seat, sizeof(seat));
}

/*! \param ostr An output stream
 *
 * Directly writes bytes to stream (calls write() function).
 * 
 * \remark Doesn't alter the stream in any other way.
 * \note Best used with binary ofstream
 */
void Ticket::write(std::ostream& ostr) const {
	ostr.write((const char*)&row, sizeof(row));
	ostr.write((const char*)&seat, sizeof(seat));
}

/*! \return If rows are equal, -1 if seat < other.seat, 1 if seat > other.seat, 0 if seat == other.seat, else the same, but for rows
 */
int Ticket::compare(const Ticket& other) const {
	if (row == other.row)
		return (seat > other.seat) - (seat < other.seat);
	return (row > other.row) - (row < other.row);
}

/* Outside of class */

/*!
 * Uses the stream's >> operator to read and parse row and seat.
 *
 * \note Best used with std::cin or text std::ifstream
 */
std::istream& operator>>(std::istream& istr, Ticket& ticket) {
	unsigned row, seat;
	istr >> row >> seat;
	ticket = Ticket(row, seat);
	return istr;
}

/*!
 * Uses the stream's << operator to write row and seat, separated by a space.
 *   
 * \note Best used with std::cout or text std::ofstream
 */
std::ostream& operator<<(std::ostream& ostr, const Ticket& ticket) {
	return ostr << ticket.get_row() << " " << ticket.get_seat();
}