home *** CD-ROM | disk | FTP | other *** search
-
- // abstract.h.state
-
- // Dreamscape - C++ class library for RISC OS
- // Copyright (c) 1996 Mark Seaborn <mseaborn@argonet.co.uk>
- //
- // This library is free software; you can redistribute it and/or
- // modify it under the terms of the GNU Library General Public
- // License as published by the Free Software Foundation; either
- // version 2 of the License, or (at your option) any later version.
- // See the Dreamscape documentation for more information.
-
- #ifndef dreamscape_state_H
- #define dreamscape_state_H
-
- #include "list.h"
- #include "algorithm.h"
-
- template <class Type> class State;
- template <class Type> class Echo;
-
- template <class Type>
- class State {
- friend Echo<Type>;
- List<Echo<Type> *> echoes;
-
- public:
- State<Type> &operator=(const Type &state)
- { set_state(state); return *this; }
- operator Type() const { return get_state(); }
-
- inline virtual void set_state(const Type &state);
- inline virtual void get_state(Type &state) const {}
- Type get_state() const { Type temp; get_state(temp); return temp; }
- };
-
- template <class Type>
- class Echo {
- friend State<Type>;
- State<Type> *_state;
-
- protected:
- virtual void update() = 0;
- State<Type> *state() { return _state; }
-
- public:
- inline Echo(State<Type> *s);
- inline virtual ~Echo();
- };
-
- template <class Type>
- inline void State<Type>::set_state(const Type &state) {
- for(ListIterator<Echo<Type> *> l=echoes.begin(); l!=echoes.end(); ++l)
- (*l)->update();
- }
-
- template <class Type>
- inline Echo<Type>::Echo(State<Type> *s): _state(s) {
- _state->echoes.push_front((Echo<Type> *) this);
- }
-
- template <class Type>
- inline Echo<Type>::~Echo() {
- _state->echoes.erase(find(_state->echoes.begin(), _state->echoes.end(),
- (Echo<Type> *) this));
- }
-
-
- template <class Type>
- class IndirectState: public State<Type> {
- State<Type> *state;
- public:
- IndirectState(State<Type> *s): state(s) {}
-
- void set_state(const Type &state) { state->set_state(state); }
- void get_state(Type &state) const { state->get_state(state); }
-
- IndirectState<Type> operator=(State<Type> *s)
- { state->s; set_state(state->get_state()); return *this; }
- operator State<Type>() const { return get_attached(); }
-
- void attach(State<Type> *s)
- { state = s; set_state(state->get_state()); return *this; }
- State<Type> get_attached() const { return state; }
- };
-
- /*
-
- We could now have a font size state and echo:
-
- class FontSizeState: public State<int> {
- };
-
- class FontSizeEcho: public Echo<int> {
- };
-
-
- Create a font size echo with a number range gadget:
-
- class FontSizeNumberRange: public FontSizeEcho {
- NumberRange *gadget;
- void update()
- { gadget->set_value(state()->get_state()); }
- };
-
- */
-
- #endif
-