-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDBRepository.cpp
83 lines (68 loc) · 2.27 KB
/
DBRepository.cpp
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
72
73
74
75
76
77
78
79
80
81
82
83
#include "DBRepository.h"
#include "setDebugNew.h"
#define new DEBUG_NEW
using namespace std;
void DBRepository::add(const Tower& tower)
{
db.add(tower);
this->elements.push_back(tower);
}
void DBRepository::remove(const std::string& location)
{
db.remove(location);
this->elements.erase(std::remove_if(this->elements.begin(), this->elements.end(), [location](const Tower& tower) { return tower.get_location() == location; }), this->elements.end());
}
void DBRepository::update(const Tower& tower)
{
this->db.update(tower);
auto it = std::find_if(this->elements.begin(), this->elements.end(), [tower](const Tower& mytower) {return mytower.get_location() == tower.get_location(); });
*it = tower;
}
Tower DBRepository::search(const std::string& location) const
{
auto it = std::find_if(this->elements.begin(), this->elements.end(), [location](const Tower& mytower) {return mytower.get_location() == location; });
if (it != this->elements.end())
return *it;
return Tower();
}
std::unique_ptr<RepoInterface::IteratorInterface> DBRepository::begin() const
{
return make_unique<DBIterator>("first", this);
// TODO: insert return statement here
}
std::unique_ptr<RepoInterface::IteratorInterface> DBRepository::end() const
{
return make_unique<DBIterator>("last", this);
// TODO: insert return statement here
}
DBRepository::DBIterator::DBIterator(std::string pos, const DBRepository* container) : IteratorInterface{ container }
{
this->current = MyDatabase::iterator(&container->db, pos);
}
DBRepository::DBIterator::DBIterator(const DBIterator& other): IteratorInterface{container}
{
this->current = other.current;
}
void DBRepository::DBIterator::first()
{
this->current = MyDatabase::iterator(&((DBRepository*)this->container)->db, "first");
}
const Tower& DBRepository::DBIterator::getTower() const
{
return *this->current;
}
void DBRepository::DBIterator::next()
{
if (!this->valid())
throw std::exception("Invalid next call");
this->current++;
}
bool DBRepository::DBIterator::Equals(const std::unique_ptr<RepoInterface::IteratorInterface> it) const
{
auto iter = dynamic_cast<DBRepository::DBIterator*>(const_cast<RepoInterface::IteratorInterface*>(it.get()));
return this->current != iter->current;
}
bool DBRepository::DBIterator::valid() const
{
return this->current.valid();
}