Skip to content

Commit

Permalink
This might be promising...
Browse files Browse the repository at this point in the history
  • Loading branch information
maleadt committed Apr 23, 2011
1 parent ad79e63 commit c459ad6
Show file tree
Hide file tree
Showing 16 changed files with 203 additions and 112 deletions.
3 changes: 2 additions & 1 deletion api/container/connectionlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ namespace iRail
class ConnectionList : public QAbstractListModel
{
Q_OBJECT
public:
private:
// Construction and destruction
ConnectionList(const Journey::Id& iJourney, QObject* iParent = 0);
~ConnectionList();
friend class ContainerCache;

// Basic I/O
public:
const Journey::Id& journeyId() const;

// Model interface
Expand Down
64 changes: 64 additions & 0 deletions api/data.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//
// Configuration
//

// Include guard
#ifndef DATA_H
#define DATA_H

// Includes
#include <QObject>
#include <QHash>

namespace iRail
{
class Data : public QObject
{
Q_OBJECT
Q_PROPERTY(Id id READ id CONSTANT)
public:
// Auxiliary types
struct Id
{
// Operator implementation
virtual bool equals(const Id& id) const = 0;
virtual unsigned int hash() const = 0;

// Operators
friend unsigned int qHash(const Id& id);
friend bool operator==(const Id& lhs, const Id& rhs);
};

// Basic I/O
virtual Id& id() const = 0;

// Operator implementation
virtual bool equals(const Data& data) const = 0;
virtual Data& assign(const Data& data) = 0;

// Operators
virtual Data& operator=(const Data& data)
{
return assign(data);
}

friend bool operator==(const Data& lhs, const Data& rhs);
};

unsigned int qHash(const Data::Id& id)
{
return id.hash();
}

bool operator==(const Data::Id& lhs, const Data::Id& rhs)
{
return lhs.equals(rhs);
}

bool operator==(const Data& lhs, const Data& rhs)
{
return lhs.equals(rhs);
}
}

#endif // DATA_H
27 changes: 15 additions & 12 deletions api/data/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,21 @@ void Connection::setDelay(unsigned int iDelay)


//
// Operators
// Operator implementation
//

bool iRail::operator==(const Connection& lhs, const Connection& rhs)
bool Connection::equals(const Data& data) const
{
return (lhs.departure() == rhs.departure() &&
lhs.arrival() == rhs.arrival() &&
lhs.terminus() == rhs.terminus() &&
lhs.vehicle() == rhs.vehicle());
const Connection& other = dynamic_cast<const Connection&>(data);
return (departure() == other.departure() &&
arrival() == other.arrival() &&
terminus() == other.terminus() &&
vehicle() == other.vehicle());
}

Connection& Connection::operator=(const Connection& other)
Data& Connection::assign(const Data& data)
{
const Connection& other = dynamic_cast<const Connection&>(data);
if (this != &other)
{
Q_ASSERT(this->id() == other.id());
Expand All @@ -89,13 +91,14 @@ Connection& Connection::operator=(const Connection& other)
return *this;
}

inline unsigned int qHash(const Connection::Id& iConnection)
unsigned int Connection::Id::hash()
{
return (3*qHash(iConnection.origin)) ^ (5*qHash(iConnection.destination));
return (3*qHash(origin)) ^ (5*qHash(destination));
}

bool iRail::operator==(const Connection::Id& lhs, const Connection::Id& rhs)
bool Connection::Id::equals(const Data::Id& data) const
{
return (lhs.origin->id() == rhs.origin->id() &&
lhs.destination->id() == rhs.destination->id());
const Connection::Id& other = dynamic_cast<const Connection::Id&>(data);
return (origin->id() == other.origin->id() &&
destination->id() == other.destination->id());
}
16 changes: 7 additions & 9 deletions api/data/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ namespace iRail
{
Stop const* origin;
Stop const* destination;
friend inline unsigned int qHash(const Connection::Id& iConnection);
friend bool operator==(const Connection::Id& lhs, const Connection::Id& rhs);

// Operator implementation
bool equals(const Data::Id& id) const;
unsigned int hash() const;
};
enum Roles
{
Expand All @@ -54,20 +56,16 @@ namespace iRail
unsigned int delay() const;
void setDelay(unsigned int iDelay);

// Operators
friend bool operator==(const Connection& lhs, const Connection& rhs);
Connection& operator=(const Connection& other);
// Operator implementation
bool equals(const Data& data) const;
Data& assign(const Data& data);

private:
Id mId;
Station const* mTerminus;
Vehicle const* mVehicle;
unsigned int mDelay;
};

bool operator==(const Connection& lhs, const Connection& rhs);
inline unsigned int qHash(const Connection::Id& iConnection);
bool operator==(const Connection::Id& lhs, const Connection::Id& rhs);
}

Q_DECLARE_METATYPE(iRail::Connection::Id)
Expand Down
23 changes: 13 additions & 10 deletions api/data/departure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,19 @@ void Departure::setDelay(unsigned int iDelay)


//
// Operators
// Operator implementation
//

bool iRail::operator==(const Departure& lhs, const Departure& rhs)
bool Departure::equals(const Data& data)
{
return (lhs.id().vehicle == rhs.id().vehicle &&
lhs.id().origin == rhs.id().origin);
const Departure& other = dynamic_cast<const Departure&>(data);
return (id().vehicle == other.id().vehicle &&
id().origin == other.id().origin);
}

Departure& Departure::operator=(const Departure& other)
Data& Departure::assign(const Data& data)
{
const Departure& other = dynamic_cast<const Departure&>(data);
if (this != &other)
{
Q_ASSERT(this->id() == other.id());
Expand All @@ -63,13 +65,14 @@ Departure& Departure::operator=(const Departure& other)
return *this;
}

inline unsigned int qHash(const Departure::Id& iDepartureId)
unsigned int Departure::Id::hash()
{
return qHash(iDepartureId.origin) ^ qHash(iDepartureId.vehicle);
return qHash(origin) ^ qHash(vehicle);
}

bool iRail::operator==(const Departure::Id& lhs, const Departure::Id& rhs)
bool Departure::Id::equals(const Data::Id& data) const
{
return (lhs.vehicle->id() == rhs.vehicle->id() &&
lhs.origin->id() == rhs.origin->id());
const Departure::Id& other = dynamic_cast<const Departure::Id&>(data);
return (vehicle->id() == other.vehicle->id() &&
origin->id() == other.origin->id());
}
16 changes: 7 additions & 9 deletions api/data/departure.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ namespace iRail
{
Stop const* origin;
Vehicle const* vehicle;
friend inline unsigned int qHash(const Departure::Id& iDepartureId);
friend bool operator==(const Departure::Id& lhs, const Departure::Id& rhs);

// Operator implementation
bool equals(const Data::Id& id) const;
unsigned int hash() const;
};
enum Roles
{
Expand All @@ -46,18 +48,14 @@ namespace iRail
unsigned int delay() const;
void setDelay(unsigned int iDelay);

// Operators
friend bool operator==(const Departure& lhs, const Departure& rhs);
Departure& operator=(const Departure& other);
// Operator implementation
bool equals(const Data& data) const;
Data& assign(const Data& data);

private:
Id mId;
unsigned int mDelay;
};

bool operator==(const Departure& lhs, const Departure& rhs);
inline unsigned int qHash(const Departure::Id& iDepartureId);
bool operator==(const Departure::Id& lhs, const Departure::Id& rhs);
}

Q_DECLARE_METATYPE(iRail::Departure::Id)
Expand Down
23 changes: 13 additions & 10 deletions api/data/journey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,19 @@ void Journey::setDelay(unsigned int iDelay)


//
// Operators
// Operator implementation
//

bool iRail::operator==(const Journey& lhs, const Journey& rhs)
bool Journey::equals(const Data& data)
{
return (lhs.id().origin || rhs.id().origin &&
lhs.id().destination || rhs.id().destination);
const Journey& other = dynamic_cast<const Journey&>(data);
return (id().origin || other.id().origin &&
id().destination || other.id().destination);
}

Journey& Journey::operator=(const Journey& other)
Data& Journey::assign(const Data& data)
{
const Journey& other = dynamic_cast<const Journey&>(data);
if (this != &other)
{
Q_ASSERT(this->id() == other.id());
Expand All @@ -63,13 +65,14 @@ Journey& Journey::operator=(const Journey& other)
return *this;
}

unsigned int qHash(const Journey::Id& iJourneyId)
unsigned int Journey::Id::hash()
{
return (3*qHash(iJourneyId.origin)) ^ (5*qHash(iJourneyId.destination));
return (3*qHash(origin)) ^ (5*qHash(destination));
}

bool iRail::operator==(const Journey::Id& lhs, const Journey::Id& rhs)
bool Journey::Id::equals(const Data::Id& data) const
{
return (lhs.origin->id() == rhs.origin->id() &&
lhs.destination->id() == rhs.destination->id());
const Journey::Id& other = dynamic_cast<const Journey::Id&>(data);
return (origin->id() == other.origin->id() &&
destination->id() == other.destination->id());
}
16 changes: 7 additions & 9 deletions api/data/journey.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ namespace iRail
{
Stop const* origin;
Stop const* destination;
friend inline unsigned int qHash(const Journey::Id& iJourneyId);
friend bool operator==(const Journey::Id& lhs, const Journey::Id& rhs);

// Operator implementation
bool equals(const Data::Id& id) const;
unsigned int hash() const;
};

enum Roles
Expand All @@ -47,18 +49,14 @@ namespace iRail
unsigned int delay() const;
void setDelay(unsigned int iDelay);

// Operators
friend bool operator==(const Journey& lhs, const Journey& rhs);
Journey& operator=(const Journey& other);
// Operator implementation
bool equals(const Data& data) const;
Data& assign(const Data& data);

private:
Id mId;
unsigned int mDelay;
};

bool operator==(const Journey& lhs, const Journey& rhs);
inline unsigned int qHash(const Journey::Id& iJourneyId);
bool operator==(const Journey::Id& lhs, const Journey::Id& rhs);
}

Q_DECLARE_METATYPE(iRail::Journey::Id)
Expand Down
29 changes: 19 additions & 10 deletions api/data/station.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,20 @@ void Station::setLocation(const Location& iLocation)


//
// Operators
// Operator implementation
//

bool iRail::operator==(const Station& lhs, const Station& rhs)
bool Station::equals(const Data& data) const
{
return (lhs.id() == rhs.id() &&
lhs.name() == rhs.name() &&
*(lhs.location()) == *(rhs.location()));
const Station& other = dynamic_cast<const Station&>(data);
return (id() == other.id() &&
name() == other.name() &&
*(location()) == *(other.location()));
}

Station& Station::operator=(const Station& other)
Data& Station::assign(const Data& data)
{
const Station& other = dynamic_cast<const Station&>(data);
if (this != &other)
{
Q_ASSERT(this->id() == other.id());
Expand All @@ -80,6 +82,17 @@ Station& Station::operator=(const Station& other)
return *this;
}

unsigned int Station::Id::hash()
{
return qHash(guid);
}

bool Station::Id::equals(const Data::Id& data) const
{
const Station::Id& other = dynamic_cast<const Station::Id&>(data);
return (guid == other.guid);
}

QDataStream& iRail::operator<<(QDataStream& iStream, const Station& iStation)
{
iStream << iStation.mId;
Expand All @@ -98,7 +111,3 @@ QDataStream& iRail::operator>>(QDataStream& iStream, Station& iStation)
return iStream;
}

inline unsigned int iRail::qHash(const Station &iStation)
{
return qHash(iStation.id());
}
Loading

0 comments on commit c459ad6

Please sign in to comment.