1 #include "orbsvcs/Trader/Interpreter.h"
2 #include "orbsvcs/Trader/Constraint_Visitors.h"
3 #include "ace/OS_NS_string.h"
5 template<class T
> class Literal_Interpreter
;
8 class Literal_Visitor
: public TAO_Constraint_Visitor
11 Literal_Visitor (Literal_Interpreter
<T
>* interp
);
12 virtual ~Literal_Visitor ();
14 virtual int visit_constraint (TAO_Unary_Constraint
*);
16 virtual int visit_with (TAO_Unary_Constraint
*) { return 1; }
17 virtual int visit_min (TAO_Unary_Constraint
*) { return 1; }
18 virtual int visit_max (TAO_Unary_Constraint
*) { return 1; }
19 virtual int visit_first (TAO_Noop_Constraint
*) { return 1; }
20 virtual int visit_random (TAO_Noop_Constraint
*) { return 1; }
22 virtual int visit_and (TAO_Binary_Constraint
*) { return 1; }
23 virtual int visit_or (TAO_Binary_Constraint
*) { return 1; }
24 virtual int visit_not (TAO_Unary_Constraint
*) { return 1; }
26 virtual int visit_exist (TAO_Unary_Constraint
*) { return 1; }
27 virtual int visit_unary_minus (TAO_Unary_Constraint
*) { return 1; }
29 virtual int visit_add (TAO_Binary_Constraint
*) { return 1; }
30 virtual int visit_sub (TAO_Binary_Constraint
*) { return 1; }
31 virtual int visit_mult (TAO_Binary_Constraint
*) { return 1; }
32 virtual int visit_div (TAO_Binary_Constraint
*) { return 1; }
34 virtual int visit_twiddle (TAO_Binary_Constraint
*) { return 1; }
35 virtual int visit_in (TAO_Binary_Constraint
*) { return 1; }
37 virtual int visit_less_than (TAO_Binary_Constraint
*) { return 1; }
38 virtual int visit_less_than_equal (TAO_Binary_Constraint
*) { return 1; }
39 virtual int visit_greater_than (TAO_Binary_Constraint
*) { return 1; }
40 virtual int visit_greater_than_equal (TAO_Binary_Constraint
*) { return 1; }
41 virtual int visit_equal (TAO_Binary_Constraint
*) { return 1; }
42 virtual int visit_not_equal (TAO_Binary_Constraint
*) { return 1; }
44 virtual int visit_literal (TAO_Literal_Constraint
*);
45 virtual int visit_property (TAO_Property_Constraint
*) { return 1; }
48 Literal_Interpreter
<T
>* interp_
;
52 Literal_Visitor
<T
>::Literal_Visitor (Literal_Interpreter
<T
>* interp
)
58 Literal_Visitor
<T
>::~Literal_Visitor ()
63 Literal_Visitor
<T
>::visit_constraint (TAO_Unary_Constraint
* c
)
65 return c
->operand ()->accept (this);
69 Literal_Visitor
<T
>::visit_literal (TAO_Literal_Constraint
* literal
)
71 this->interp_
->constraint(*literal
);
76 class Literal_Interpreter
: public TAO_Interpreter
79 Literal_Interpreter ();
80 virtual ~Literal_Interpreter ();
82 bool test (const char* str
,
84 void constraint (TAO_Literal_Constraint
& constraint
);
85 const TAO_Literal_Constraint
& constraint () const;
88 bool compare (T expected
) const;
90 TAO_Literal_Constraint constraint_
;
94 Literal_Interpreter
<T
>::Literal_Interpreter ()
99 Literal_Interpreter
<T
>::~Literal_Interpreter ()
103 template<class T
> bool
104 Literal_Interpreter
<T
>::test (const char* str
,
107 this->build_tree (str
);
108 if (this->root_
== 0)
110 ACE_ERROR ((LM_ERROR
, "ERROR: Invalid interpreter string.\n"));
114 Literal_Visitor
<T
> visitor (this);
115 this->root_
->accept (&visitor
);
117 return this->compare(expected
);
121 Literal_Interpreter
<const char*>::compare (const char* expected
) const
123 return (ACE_OS::strcmp(static_cast<const char*> (this->constraint_
),
127 template<class T
> bool
128 Literal_Interpreter
<T
>::compare (T expected
) const
130 return ACE::is_equal (static_cast<T
> (this->constraint_
), expected
);
133 template<class T
> void
134 Literal_Interpreter
<T
>::constraint (TAO_Literal_Constraint
& constraint
)
136 this->constraint_
= constraint
;
139 template<class T
> const TAO_Literal_Constraint
&
140 Literal_Interpreter
<T
>::constraint () const
142 return this->constraint_
;
146 ACE_TMAIN (int, ACE_TCHAR
**)
150 Literal_Interpreter
<CORBA::ULongLong
> u_interp
;
151 if (!u_interp
.test ("993834343433882",
152 ACE_UINT64_LITERAL (993834343433882)))
155 ACE_ERROR ((LM_ERROR
, "ERROR: CORBA::ULongLong test failed.\n"));
158 Literal_Interpreter
<CORBA::LongLong
> interp
;
159 if (!interp
.test ("-1879048193", -1879048193))
162 ACE_ERROR ((LM_ERROR
, "ERROR: CORBA::LongLong test failed.\n"));
165 // Since we're comparing signed and unsigned, the signed should get
166 // promoted to unsigned. However, it gets logically promoted, not
167 // binarily. So, a negative value is converted into zero as an
169 if (interp
.constraint () > u_interp
.constraint ())
172 ACE_ERROR ((LM_ERROR
, "ERROR: Mixed greater than test 1 failed.\n"));
176 Literal_Interpreter
<CORBA::Double
> u_interp
;
177 if (!u_interp
.test ("993834343433882.88837719", 993834343433882.88837719))
180 ACE_ERROR ((LM_ERROR
, "ERROR: CORBA::Double test 1 failed.\n"));
183 Literal_Interpreter
<CORBA::Double
> interp
;
184 if (!interp
.test ("-993834343433882.88837719",
185 -993834343433882.88837719))
188 ACE_ERROR ((LM_ERROR
, "ERROR: CORBA::Double test 2 failed.\n"));
191 if (interp
.constraint () > u_interp
.constraint ())
194 ACE_ERROR ((LM_ERROR
, "ERROR: Mixed greater than test 2 failed.\n"));
198 Literal_Interpreter
<CORBA::Boolean
> interp
;
199 if (!interp
.test ("TRUE", true))
202 ACE_ERROR ((LM_ERROR
, "ERROR: CORBA::Boolean test 1 failed.\n"));
206 Literal_Interpreter
<CORBA::Boolean
> interp
;
207 if (!interp
.test ("FALSE", false))
210 ACE_ERROR ((LM_ERROR
, "ERROR: CORBA::Boolean test 2 failed.\n"));
214 Literal_Interpreter
<const char*> interp
;
215 if (!interp
.test ("'This is a test'", "This is a test"))
218 ACE_ERROR ((LM_ERROR
, "ERROR: const char* test failed.\n"));