Tiberian Technologies Scripts Reference Revision: 9000
Loading...
Searching...
No Matches
Observee.h
1/* Renegade Scripts.dll
2 Copyright 2013 Tiberian Technologies
3
4 This file is part of the Renegade scripts.dll
5 The Renegade scripts.dll is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License as published by the Free
7 Software Foundation; either version 2, or (at your option) any later
8 version. See the file COPYING for more details.
9 In addition, an exemption is given to allow Run Time Dynamic Linking of this code with any closed source module that does not contain code covered by this licence.
10 Only the source code to the module(s) containing the licenced code has to be released.
11*/
12#pragma once
13
14#include "Iterator.h"
15
16template<typename T> inline T clone(const T& o) { return o; }
17
18#define TT_SCOPE_DEFINE(d) for (bool c = true; c;) for (d; c; c = false) // Hack, but it should be compiled away anyway.
19#define TT_EACH_OBSERVER(observee) TT_CONST_FOREACH(observer, (observee).getObservers()) (**observer)
20#define TT_EACH_OBSERVER_SAFE(observee) TT_SCOPE_DEFINE(auto observeeClone = clone((observee).getObservers())) TT_CONST_FOREACH(observer, observeeClone) (**observer)
21
22
23template<class Observer> class Observee
24{
25 SimpleDynVecClass<Observer*> observers;
26
27public:
28
29 void addObserver(Observer& observer) { observers.Add(&observer); }
30 void removeObserver(Observer& observer) { observers.Delete(&observer); }
31 SimpleDynVecClass<Observer*>& getObservers() { return observers; }
32 bool hasObservers() const { return !observers.isEmpty(); }
33};