1 /*****************************************************************************/
2 /* */
3 /* MSGCOLL.CC */
4 /* */
5 /* (C) 1993,94 Ullrich von Bassewitz */
6 /* Zwehrenbuehlstrasse 33 */
7 /* D-72070 Tuebingen */
8 /* EMail: uz@ibb.schwaben.com */
9 /* */
10 /*****************************************************************************/
11
12
13
14 // $Id$
15 //
16 // $Log$
17 //
18 //
19
20
21
22 #include "machine.h"
23 #include "coll.h"
24 #include "stream.h"
25 #include "msg.h"
26 #include "msgcoll.h"
27 #include "streamid.h"
28
29
30
31 // Register class MsgCollection if there are references in this module
32 LINK(MsgCollection, ID_MsgCollection);
33
34
35
36 /*****************************************************************************/
37 /* Explicit template instantiation */
38 /*****************************************************************************/
39
40
41
42 #ifdef EXPLICIT_TEMPLATES
43 template class Collection<Msg>;
44 template class SortedCollection<Msg, u16>;
45 #endif
46
47
48
49 /*****************************************************************************/
50 /* class MsgCollection */
51 /*****************************************************************************/
52
53
54
StreamableID() const55 u16 MsgCollection::StreamableID () const
56 {
57 return ID_MsgCollection;
58 }
59
60
61
Build()62 Streamable* MsgCollection::Build ()
63 {
64 return new MsgCollection (Empty);
65 }
66
67
68
GetItem(Stream & S)69 void* MsgCollection::GetItem (Stream& S)
70 {
71 // Create new Msg
72 Msg *Item = new Msg (Empty);
73
74 // Read data
75 S >> Item;
76
77 return (void*) Item;
78 }
79
80
81
PutItem(Stream & S,void * Item) const82 void MsgCollection::PutItem (Stream& S, void* Item) const
83 {
84 S << (Msg*) Item;
85 }
86
87
88
Compare(const u16 * Key1,const u16 * Key2)89 int MsgCollection::Compare (const u16* Key1, const u16* Key2)
90 {
91 if (*Key1 > *Key2) {
92 return 1;
93 } else if (*Key1 < *Key2) {
94 return -1;
95 } else {
96 return 0;
97 }
98 }
99
100
101
KeyOf(const Msg * Item)102 const u16* MsgCollection::KeyOf (const Msg* Item)
103 {
104 return &Item->MsgNum;
105 }
106
107
108
GetMsg(u16 ID)109 const Msg& MsgCollection::GetMsg (u16 ID)
110 {
111 int Index;
112
113 // Search for item with given ID
114 if (Search (&ID, Index) == 0) {
115 // Not found, set Index to -1
116 Index = -1;
117 }
118
119 // Check if found
120 if (Index == -1) {
121 String Msg = FormatStr ("GetMsg: Message #%u not found", ID);
122 FAIL (Msg.GetStr ());
123 }
124
125 // Return Msg
126 return *At (Index);
127 }
128
129
130