Skip to content

Commit

Permalink
Избягване на експлицитното преобразуване на Cloneable* към Task*
Browse files Browse the repository at this point in the history
чрез шаблонизиране на Cloneable, шаблонният параметър указва базовия
клас, указател към който ще връща clone()
  • Loading branch information
triffon committed Jun 5, 2019
1 parent b5f3e01 commit 3368b78
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 8 deletions.
3 changes: 2 additions & 1 deletion common/cloneable.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#ifndef __CLONEABLE_H
#define __CLONEABLE_H

template <typename T>
class Cloneable {
public:
virtual Cloneable* clone() const = 0;
virtual T* clone() const = 0;
};

#endif
2 changes: 1 addition & 1 deletion tasks/quick_task.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class QuickTask : public Task {

void print(std::ostream& os = std::cout) const;

Cloneable* clone() const { return new QuickTask(*this); }
Task* clone() const { return new QuickTask(*this); }
};

#endif
6 changes: 3 additions & 3 deletions tasks/repeated_task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

RepeatedTask::RepeatedTask(char const* n, Task const& t, unsigned r) :
SimpleTask(n, r), current(nullptr) {
prototype = (Task*)t.clone();
prototype = t.clone();
reset();
}

Expand All @@ -24,8 +24,8 @@ RepeatedTask::~RepeatedTask() {
}

void RepeatedTask::copy(RepeatedTask const& rt) {
prototype = (Task*)rt.prototype->clone();
current = (Task*)rt.current->clone();
prototype = rt.prototype->clone();
current = rt.current->clone();
}

void RepeatedTask::destroy() {
Expand Down
2 changes: 1 addition & 1 deletion tasks/repeated_task.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class RepeatedTask : public SimpleTask {

void print(std::ostream& os = std::cout) const;

Cloneable* clone() const { return new RepeatedTask(*this); }
Task* clone() const { return new RepeatedTask(*this); }

unsigned getRepetitions() const { return SimpleTask::getExecutionTime(); }

Expand Down
2 changes: 1 addition & 1 deletion tasks/simple_task.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class SimpleTask : public Task {

void print(std::ostream& os = std::cout) const;

Cloneable* clone() const { return new SimpleTask(*this); }
Task* clone() const { return new SimpleTask(*this); }
};

#endif
2 changes: 1 addition & 1 deletion tasks/task.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "../common/named.h"
#include "../common/cloneable.h"

class Task : public Printable, public Named, public Cloneable {
class Task : public Printable, public Named, public Cloneable<Task> {

public:

Expand Down

0 comments on commit 3368b78

Please sign in to comment.