Fawkes API  Fawkes Development Version
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
interface_info.cpp
1 
2 /***************************************************************************
3  * interface_info.h - BlackBoard Interface Info
4  *
5  * Created: Mon Mar 03 15:44:46 2008
6  * Copyright 2006-2008 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #include <interface/interface_info.h>
25 #include <interface/interface.h>
26 #include <utils/misc/strndup.h>
27 
28 #include <cstdlib>
29 #include <cstring>
30 
31 namespace fawkes {
32 
33 /** @class InterfaceInfo <interface/interface_info.h>
34  * Interface info.
35  * This class holds information about a specific interface.
36  * @author Tim Niemueller
37  */
38 
39 /** Constructor.
40  * @param type type of the interface
41  * @param id id of the interface
42  * @param hash version hash
43  * @param has_writer true if there is a writer, false otherwise
44  * @param num_readers number of readers
45  * @param serial instance serial
46  */
47 InterfaceInfo::InterfaceInfo(const char *type, const char *id, const unsigned char *hash,
48  unsigned int serial, bool has_writer, unsigned int num_readers)
49 {
50  __type = strndup(type, __INTERFACE_TYPE_SIZE);
51  __id = strndup(id, __INTERFACE_ID_SIZE);
52  __hash = (unsigned char *)malloc(__INTERFACE_HASH_SIZE);
53  memcpy(__hash, hash, __INTERFACE_HASH_SIZE);
54  __has_writer = has_writer;
55  __num_readers = num_readers;
56  __serial = serial;
57 }
58 
59 
60 /** Copy constructor.
61  * @param i info to copy
62  */
64 {
65  __type = strndup(i.__type, __INTERFACE_TYPE_SIZE);
66  __id = strndup(i.__id, __INTERFACE_ID_SIZE);
67  __hash = (unsigned char *)malloc(__INTERFACE_HASH_SIZE);
68  memcpy(__hash, i.__hash, __INTERFACE_HASH_SIZE);
69  __has_writer = i.__has_writer;
70  __num_readers = i.__num_readers;
71  __serial = i.__serial;
72 }
73 
74 
75 /** Destructor. */
77 {
78  free(__type);
79  free(__id);
80  free(__hash);
81 }
82 
83 
84 /** Get interface type.
85  * @return type string
86  */
87 const char *
89 {
90  return __type;
91 }
92 
93 
94 /** Get interface ID.
95  * @return ID string
96  */
97 const char *
99 {
100  return __id;
101 }
102 
103 
104 /** Get interface version hash.
105  * @return interface version hash
106  */
107 const unsigned char *
109 {
110  return __hash;
111 }
112 
113 
114 /** Check if there is a writer.
115  * @return true if there is a writer, false otherwise
116  */
117 bool
119 {
120  return __has_writer;
121 }
122 
123 
124 /** Get number of readers.
125  * @return number of readers
126  */
127 unsigned int
129 {
130  return __num_readers;
131 }
132 
133 
134 /** Get interface instance serial.
135  * @return type string
136  */
137 unsigned int
139 {
140  return __serial;
141 }
142 
143 
144 /** < operator
145  * This compares two interface infos with respect to the less than (<) relation
146  * considering the type and id of an interface.
147  * An interface info A is less than an interface info B (A < B) iff
148  * (A.type < B.type) or ((A.type == B.type) && A.id < B.id).
149  * @param ii interface info to compare this to
150  * @return true if this instance is considered less than @p ii, false otherwise
151  */
152 bool
154 {
155  int td = strncmp(__type, ii.__type, __INTERFACE_TYPE_SIZE);
156  if ( td < 0 ) {
157  return true;
158  } else if (td > 0) {
159  return false;
160  } else {
161  return (strncmp(__id, ii.__id, __INTERFACE_ID_SIZE) < 0);
162  }
163 }
164 
165 
166 /** @class InterfaceInfoList <interface/interface_info.h>
167  * Interface information list.
168  * List with InterfaceInfo instances.
169  * @author Tim Niemueller
170  */
171 
172 /** Append an interface info.
173  * @param type type of the interface
174  * @param id id of the interface
175  * @param hash version hash
176  * @param has_writer true if there is a writer, false otherwise
177  * @param num_readers number of readers
178  * @param serial instance serial
179  */
180 void
181 InterfaceInfoList::append(const char *type, const char *id, const unsigned char *hash,
182  unsigned int serial, bool has_writer, unsigned int num_readers)
183 {
184  push_back(InterfaceInfo(type, id, hash, serial, has_writer, num_readers));
185 }
186 
187 } // end namespace fawkes