source: cpp/frams/mhandlers/mhandlers.h @ 372

Last change on this file since 372 was 372, checked in by sz, 9 years ago

Renamed some classes and functions to make their purpose more obvious:

All MessageHandlers? must now be given the explicit "Enable" argument if you want them to automatically become active. This makes side effects clearly visible.

  • Property svn:eol-style set to native
File size: 2.9 KB
Line 
1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
2// Copyright (C) 1999-2015  Maciej Komosinski and Szymon Ulatowski.
3// See LICENSE.txt for details.
4
5#ifndef _MHANDLERS_H_
6#define _MHANDLERS_H_
7
8#include <frams/util/list.h>
9#include <frams/util/sstring.h>
10#include <common/hmessage.h>
11#include <common/threads.h>
12
13class MessageHandlerBase;
14
15class MessageHandlerManager
16{
17        friend class MessageHandlerBase;
18        SListTempl<MessageHandlerBase*> handlers;
19        void send(int level, const char *o, const char *m, const char *bl, int w);
20public:
21        int find(MessageHandlerBase *r) { return handlers.find(r); }
22        int add(MessageHandlerBase *r);
23        void remove(int i);
24        void remove(MessageHandlerBase *r);
25        void removeAll();
26        void send(const char *o, const char *m, const char *bl, int w)
27        {
28                send(handlers.size() - 1, o, m, bl, w);
29        }
30        ~MessageHandlerManager() { removeAll(); }
31};
32
33extern THREAD_LOCAL_DECL(MessageHandlerManager, message_handler_manager_instance);
34
35////////////////////////////////////////
36
37class MessageHandlerBase
38{
39        friend class MessageHandlerManager;
40protected:
41        MessageHandlerManager* mgr;
42        int options;
43
44public:
45
46        enum HandlerOptions
47        {
48                DontBlock = 1, CannotBeBlocked = 2, Enable = 4, Paused = 8
49        };
50
51        void Hprintf(const char *o, const char *m, int w, const char *bl, ...);
52        void send(const char *o, const char *m, const char *bl, int w);
53
54        bool isEnabled() { return mgr ? 1 : 0; }
55        void enable();
56        void disable();
57        bool isPaused() { return (options & Paused) != 0; }
58        void pause();
59        void resume();
60
61        MessageHandlerBase(int opts = 0) :mgr(0), options(opts)
62        {
63                if (options&Enable) enable();
64        }
65        virtual ~MessageHandlerBase()
66        {
67                disable();
68        }
69
70        virtual void handle(const char *o, const char *m, const char *bl, int w) {}
71};
72
73///////////////////////////////////////////
74
75class MessageHandlerToMemory : public MessageHandlerBase
76{
77protected:
78        int maxlevel, errcount, warncount, storlevel, storcount, infocount;
79        SString msgs;
80
81public:
82
83        void reset() { maxlevel = HMLV_INFO - 1; errcount = warncount = storcount = infocount = 0; msgs = 0; }
84
85        enum Options2
86        {
87                StoreFirstMessage = 16, StoreAllMessages = 32
88        };
89
90        int getErrorCount()       { return errcount; }
91        int getWarningCount()     { return warncount; }
92        int getInfoCount()        { return infocount; }
93        int getStoredCount()      { return storcount; }
94        int getErrorLevel()       { return maxlevel; }
95        const SString& getMessages() { return msgs; }
96
97        MessageHandlerToMemory(int opts = 0, int minimal_level_to_store = HMLV_ERROR) :MessageHandlerBase(opts), storlevel(minimal_level_to_store)
98        {
99                reset();
100        }
101
102        void handle(const char *o, const char *m, const char *bl, int w);
103};
104
105class RedirectingMessageHandler : public MessageHandlerBase
106{
107        MessageHandlerManager *other_mgr;
108public:
109        RedirectingMessageHandler(MessageHandlerManager *om,int opts=0)
110                :MessageHandlerBase(opts), other_mgr(om) {}
111
112        void handle(const char *o, const char *m, const char *bl, int w)
113        {
114                other_mgr->send(o, m, bl, w);
115        }
116};
117
118#endif
Note: See TracBrowser for help on using the repository browser.