2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 //%% (c) Copyright 1993, 1994 Hewlett-Packard Company
24 //%% (c) Copyright 1993, 1994 International Business Machines Corp.
25 //%% (c) Copyright 1993, 1994 Sun Microsystems, Inc.
26 //%% (c) Copyright 1993, 1994 Novell, Inc.
27 //%% $XConsortium: mp_arg.C /main/3 1995/10/23 10:18:54 rswiston $
30 * @(#)mp_arg.C 1.19 93/09/07
32 * Tool Talk Message Passer (MP) - mp_arg.C
34 * Copyright (c) 1990,1992 by Sun Microsystems, Inc.
36 * Implementation of the _Tt_arg & _Tt_arg_list classes representing
37 * arguments to a method or a procedure.
39 #include "mp/mp_global.h"
40 #include "mp/mp_arg.h"
43 #include "util/tt_enumname.h"
44 #include "util/tt_iostream.h"
45 #include "util/tt_xdr_utils.h"
46 #include "util/tt_global_env.h"
50 * Construct a _Tt_arg with everything zeroed out
59 * Copy constructor for _Tt_args.
62 _Tt_arg(const _Tt_arg &a)
67 _data_type = a._data_type;
68 _data_int = a._data_int;
69 _data_string = a._data_string;
73 * Copy the _Tt_arg pointed to by a _Tt_arg_ptr.
76 _Tt_arg(const _Tt_arg_ptr &p)
81 _data_type = p->_data_type;
82 _data_int = p->_data_int;
83 _data_string = p->_data_string;
87 _Tt_arg(const _Tt_string &t)
94 * Constructs a _Tt_arg where the mode and type are known.
95 * No valid data is yet known.
99 _Tt_arg(Tt_mode m, _Tt_string &t)
101 constructor_common();
108 _Tt_arg(const char *t)
111 constructor_common();
116 * Constructs a _Tt_arg where the mode and type are known.
117 * No valid data is yet known.
121 _Tt_arg(Tt_mode m, const char *t)
124 constructor_common();
131 * Destroys any memory associated with _Tt_arg
140 * Sets the data associated with this arg as an integer
152 * Sets the data associated with this arg as a byte string
155 set_data_string(const _Tt_string &s)
164 * Obtain the data for this arg as an int.
165 * Return TT_ERR_NUM if the value is a string.
168 data_int(int &i) const
170 if (_data_type != INT) return TT_ERR_NUM;
176 * Obtain the data for this arg as a string.
177 * Return TT_ERR_POINTER if the value is an int.
180 data_string(_Tt_string &s) const
182 if (_data_type != STRING) return TT_ERR_POINTER;
188 * Prints out a _Tt_arg. Mainly used for debugging purposes only.
191 print(const _Tt_ostream &os) const
193 os << _tt_enumname(_mode) << " " << _type << ": ";
195 if (_name.len() > 0) {
196 os << "<" << _name << ">\n";
197 } else if (_data_type==INT) {
198 os << _data_int << "\n";
200 _data_string.print(os, _Tt_string_user_width, 1);
207 * XDR encodes and decodes a _Tt_arg object. Returns 1 if successful and
213 if (! xdr_int(xdrs, (int *)&_mode)) {
216 if (! _type.xdr(xdrs)) {
219 if (_tt_global->xdr_version() < TT_PUSH_ROTATE_XDR_VERSION) {
221 // Earlier versions of the XDR protocol included an
222 // a never-used _Tt_string called _matched_type.
224 _Tt_string _matched_type;
225 if (! _matched_type.xdr(xdrs)) {
229 if (! _name.xdr(xdrs)) {
232 if (! xdr_int(xdrs, (int *)&_data_type)) {
235 if (_data_type==INT) {
236 if (! xdr_int(xdrs, &_data_int)) {
240 if (! _data_string.xdr(xdrs)) {
248 * update_value is used when merging a reply with the original message.
249 * The value cells of this arg are updated with the value cells in the
253 update_value(const _Tt_arg &x)
255 _data_type = x._data_type;
256 _data_int = x._data_int;
257 _data_string = x._data_string;
262 * Wrapper whose only purpose is to invoke the xdr method on the given
266 tt_xdr_arg(XDR *xdrs, _Tt_arg_ptr *ptr)
268 return((*ptr)->xdr(xdrs));
273 * XDR encodes and decodes a list of _Tt_arg objects. p is assumed to
274 * point to the list on which to append/get the objects.
277 tt_xdr_arg_list(XDR *xdrs, _Tt_arg_list_ptr *p)
279 return((*p).xdr(xdrs));
284 _tt_arg_print(const _Tt_ostream &os, const _Tt_object *obj)
286 ((_Tt_arg *)obj)->print(os);
290 * Common initialization code for _Tt_arg constructors.
295 _mode = TT_MODE_UNDEFINED;
300 _data_string = (char *)0;
304 * Returns non-zero score if \'this\', which must be a pattern arg, matches
305 * the given arg, which must be a message arg. Matching is
306 * less restrictive than equality, since the following exceptions
308 * 1\) If the pattern type is ALL, it matches any type in the message.
309 * 2\) If the message type is ALL, it matches any type in the pattern.
310 * \[Somehow, the pattern type should get into the message, but
311 * I don\'t know how yet.\]
312 * 3\) If the pattern value is not set, it matches any value in the
314 * Although this method is only used in the server, it is too hard
315 * to figure out how to get messages and patterns un-XDRed in
316 * the server with args as a _Tt_s_arg instead of _Tt_arg.
319 match_score(const _Tt_arg &msg_arg, int &used_wildcard) const
321 // Comparing args works because XDR is defined as always
322 // padding with zeroes.
324 int score = 1; // wildcard match scores only 1
328 if (_mode == msg_arg.mode()) {
330 } else if (_mode != TT_MODE_UNDEFINED) {
335 if (_type == msg_arg.type()) {
336 // check equality first, in case both are "ALL"
338 } else if (_type == "ALL") {
339 used_wildcard = 1; // pattern ALL matches everything
340 } else if (msg_arg.type() == "ALL") {
341 used_wildcard = 1; // message ALL matches everything
343 return 0; // types mismatched
347 switch (_data_type) {
351 if (msg_arg.data_type() != _data_type) {
354 switch (_data_type) {
356 if (msg_arg.data_int() != _data_int) {
361 if (msg_arg.data_string() != _data_string) {
371 if (msg_arg.data_type() != _data_type) {
377 // everything matches
382 // Returns 1 if the typed values are equal, else 0. Ignores mode and
383 // vtype. Used by _Tt_pat_context::deleteValue().
386 operator==(const _Tt_arg &arg) const
388 if (_data_type != arg.data_type()) {
391 switch (_data_type) {
393 if (_data_int != arg.data_int()) {
398 if (_data_string != arg.data_string()) {