Make G() safe.

GitOrigin-RevId: d417008f94ce6f8c587561af352d696ef55895b3
This commit is contained in:
levlam 2019-08-01 22:12:04 +03:00
parent 84dba6cd8e
commit 116f0028a2
4 changed files with 22 additions and 2 deletions

View File

@ -122,6 +122,9 @@ class MultiTd : public Actor {
public:
explicit TdActorContext(std::string tag) : tag_(std::move(tag)) {
}
int32 get_id() const override {
return 0x172ae58d;
}
std::string tag_;
};
auto context = std::make_shared<TdActorContext>(to_string(td_id));

View File

@ -68,6 +68,11 @@ class Global : public ActorContext {
Global(Global &&other) = delete;
Global &operator=(Global &&other) = delete;
static constexpr int32 ID = -572104940;
int32 get_id() const override {
return ID;
}
#define td_db() get_td_db_impl(__FILE__, __LINE__)
TdDb *get_td_db_impl(const char *file, int line) {
LOG_CHECK(td_db_) << close_flag() << " " << file << " " << line;
@ -410,8 +415,10 @@ class Global : public ActorContext {
};
inline Global *G() {
CHECK(Scheduler::context());
return static_cast<Global *>(Scheduler::context());
ActorContext *context = Scheduler::context();
CHECK(context);
CHECK(context->get_id() == Global::ID);
return static_cast<Global *>(context);
}
} // namespace td

View File

@ -32,6 +32,11 @@ class ActorContext {
ActorContext(ActorContext &&) = delete;
ActorContext &operator=(ActorContext &&) = delete;
virtual ~ActorContext() = default;
virtual int32 get_id() const {
return 0;
}
const char *tag_ = nullptr;
std::weak_ptr<ActorContext> this_ptr_;
};

View File

@ -468,6 +468,10 @@ TEST(Actors, do_after_stop) {
class XContext : public ActorContext {
public:
int32 get_id() const override {
return 123456789;
}
void validate() {
CHECK(x == 1234);
}
@ -476,6 +480,7 @@ class XContext : public ActorContext {
}
int x = 1234;
};
class WithContext : public Actor {
public:
void start_up() override {