YSTest: ymsg.h 源文件

YSTest PreAlpha_b400_20130424 The YSLib Test Project

- 首页
- 相关页面
- 模块
- 命名空间
- 类
- 文件

- 文件列表
- 文件成员

全部 类 命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义 组 页
ymsg.h
浏览该文件的文档.
1 ﻿/*
2 Copyright (C) by Franksoft 2009 - 2012.
3
4 This file is part of the YSLib project, and may only be used,
5 modified, and distributed under the terms of the YSLib project
6 license, LICENSE.TXT. By continuing to use, modify, or distribute
7 this file you indicate that you have read the license and
8 understand and accept it fully.
9 */
10
28 #ifndef YSL_INC_CORE_YSMSG_H_
29 #define YSL_INC_CORE_YSMSG_H_ 1
30
31 #include "yobject.h"
32 #include "../Adaptor/ycont.h"
33 #include <ctime>
34
35 YSL_BEGIN
36
37 YSL_BEGIN_NAMESPACE(Messaging)
38
39
43 typedef u32 ID;
48 typedef u8 Priority;
49
54 const std::time_t DefTimeout(0);
55
56
62 class YF_API Message
63 {
64 friend class MessageQueue;
65
66 private:
67 ID id;
68 ValueObject content; //消息内容。
69
70 public:
75 yconstfn
76 Message(ID msg_id = 0)
77 : id(msg_id)
78 {}
84
85 Message(ID, const ValueObject&);
86 Message(ID, ValueObject&&);
88
92 DefDeCopyCtor(Message)
97 DefDeMoveCtor(Message)
98
99 Message&
100 operator=(const ValueObject& c)
101 {
102 content = c;
103 return *this;
104 }
109 Message&
110 operator=(ValueObject&& c) ynothrow
111 {
112 content = std::move(c);
113 return *this;
114 }
115 /*
116 \brief 统一赋值：使用值参数和交换函数进行复制或转移赋值。
117 \since build 331
118 */
119 Message&
120 operator=(Message msg) ynothrow
121 {
122 Swap(msg);
123 return *this;
124 }
125
130 PDefHOp(bool, !, ) const ynothrow
131 ImplRet(!bool(*this))
132
136 friend bool
137 operator==(const Message&, const Message&);
142 friend bool
143 operator<(const Message&, const Message&);
144
149 explicit DefCvt(const ynothrow, bool, id)
150
151 DefGetter(const ynothrow, ID, MessageID, id)
152 DefGetter(const ynothrow, const ValueObject&, Content, content) \
154
155 /*
156 \brief 交换。
157 */
158 void
159 Swap(Message&) ynothrow;
160 };
161
162
169 class YF_API MessageQueue : private noncopyable,
170 private multimap<Priority, Message, std::greater<Priority>>
171 {
172 public:
173 typedef size_type SizeType;
178 typedef const_iterator Iterator;
179
183 inline DefDeCtor(MessageQueue)
184 DefEmptyDtor(MessageQueue)
185
186 DefPred(const ynothrow, Empty, empty())
187
188 DefGetter(const ynothrow, SizeType, Size, size())
189
193 DefGetter(const ynothrow, Iterator, Begin, begin())
198 DefGetter(const ynothrow, Iterator, End, end())
204 DefGetter(const ynothrow, Priority, MaxPriority,
205 empty() ? 0 : begin()->first)
206
210 PDefH(void, Clear, )
211 ImplRet(clear())
212
218 PDefH(bool, Erase, Iterator i)
219 ImplRet(erase(i) != end())
220
224 void
225 Merge(MessageQueue&);
226
232 void
233 Peek(Message& msg) const
234 {
235 if(YB_LIKELY(!empty()))
236 msg = begin()->second;
237 }
238
243 void
244 Pop()
245 {
246 if(YB_LIKELY(!empty()))
247 erase(begin());
248 }
249
254 void
255 Push(const Message& msg, Priority prior)
256 {
257 if(msg)
258 insert(make_pair(prior, msg));
259 }
264 void
265 Push(const Message&& msg, Priority prior)
266 {
267 if(msg)
268 insert(make_pair(prior, std::move(msg)));
269 }
270
275 void
276 Remove(Priority);
277 };
278
279 YSL_END_NAMESPACE(Messaging)
280
281 using Messaging::Message;
282
283 YSL_END
284
285 #endif
286