summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/orbsvcs/Trader/Property_Evaluator.cpp
blob: bd3017ddfe49966110c583bfb9f75fca98eea48b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// File: Property_Evaluator.cpp
// $Id$

#include "Trader_Base.h"
#include "Property_Evaluator.h"

// Constructor

TAO_Property_Evaluator::
TAO_Property_Evaluator(CosTrading::PropertySeq& props,
		       CORBA::Boolean supports_dp)
  : props_ (props),
    supports_dp_ (supports_dp)
{
}


TAO_Property_Evaluator::
TAO_Property_Evaluator(CosTrading::Offer& offer,
		       CORBA::Boolean supports_dp)
  : props_ (offer.properties),
    supports_dp_ (supports_dp)
{
}

int
TAO_Property_Evaluator::is_dynamic_property (int index)
{
  CORBA::Environment env;
  int return_value = 0,
    num_properties = this->props_.length();

  // Ensure index is in bounds. 
  if (index >= 0 && index < num_properties)
    {
      // Obtain the value of the property at index <index>.
      CORBA::Any& value = this->props_[index].value;
      CORBA::TypeCode* type = value.type();
      
      if (type->equal(CosTradingDynamic::_tc_DynamicProp, env))
	return_value = 1;
    }
  
  return return_value;
}

CORBA::Any*
TAO_Property_Evaluator::property_value(int index,
				       CORBA::Environment& _env)
  TAO_THROW_SPEC ((CosTradingDynamic::DPEvalFailure))
{
  CORBA::Any* prop_val = 0;
  
  if (! this->is_dynamic_property(index))
    prop_val = &(this->props_[index].value);
  else if (this->supports_dp_)
    {
      // Property is defined at this point.
      DP_Eval* dp_eval;
      DP_Struct dp_struct;
      CORBA::String_var name = this->props_[index].name;
      CORBA::Any& value = this->props_[index].value;

      // Extract the DP_Struct.
      //value >>= dp_struct;
      dp_eval = dp_struct.eval_if;
	  
      if (CORBA::is_nil (dp_eval))
	TAO_THROW_RETURN (CosTradingDynamic::DPEvalFailure (), prop_val);
      else
	{
	  CORBA::TypeCode* type = dp_struct.returned_type;
	  CORBA::Any& info = dp_struct.extra_info;
	  
	  TAO_TRY
	    {
	      // Retrieve the value of the dynamic property.
	      prop_val = dp_eval->evalDP(name, type, info, TAO_TRY_ENV);
	    }
	  TAO_CATCH (CORBA::SystemException, excp)
	    {
	      TAO_THROW_RETURN
		(CosTradingDynamic::DPEvalFailure (name, type, info),
		 prop_val);
	    }
	  TAO_ENDTRY;
	}
    }

  return prop_val;
}

CORBA::TypeCode*
TAO_Property_Evaluator::property_type(int index)
{
  CORBA::TypeCode* prop_type = CORBA::TypeCode::_nil();
  
  // Determine if property is both defined and dynamic.
  if (this->is_dynamic_property (index))
    {
      // Extract type information from the DP_Struct.
      CORBA::Any& value = this->props_[index].value;
      DP_Struct dp_struct;
      //value >>= dp_struct;
      
      // Grab a pointer to the returned_type description
      prop_type = CORBA::TypeCode::_duplicate(dp_struct.returned_type);
    }
  else
    // TypeCode is self-evident at this point.
    prop_type = this->props_[index].value.type();
  
  return prop_type;
}

TAO_Property_Evaluator_By_Name::
TAO_Property_Evaluator_By_Name (CosTrading::PropertySeq& properties,
				CORBA::Environment& _env,
				CORBA::Boolean supports_dp)
  TAO_THROW_SPEC ((CosTrading::DuplicatePropertyName,
		  CosTrading::IllegalPropertyName))
  : TAO_Property_Evaluator (properties, supports_dp)
{
  string prop_name;
  int length = this->props_.length();

  for (int i = 0; i < length; i++)
    {
      CosTrading::Property& prop = this->props_[i];

#ifdef CLIENT_LIB
      if (! ::is_valid_identifier_name (prop.name))
	TAO_THROW (CosTrading::IllegalPropertyName (prop.name));
#else
      if (! TAO_Trader_Base::is_valid_identifier_name (prop.name))
	TAO_THROW (CosTrading::IllegalPropertyName (prop.name));
#endif /* CLIENT_LIB */
      
      prop_name = prop.name;
      if (! (this->table_.insert (make_pair (prop_name, i))).second)
	TAO_THROW (CosTrading::DuplicatePropertyName (prop.name));
    }
}

TAO_Property_Evaluator_By_Name::
TAO_Property_Evaluator_By_Name(CosTrading::Offer& offer,
			       CORBA::Boolean supports_dp)
  : TAO_Property_Evaluator(offer, supports_dp)
{
  int length = this->props_.length();

  for (int i = 0; i < length; i++)
    {
      string prop_name = (char*)this->props_[i].name;
      this->table_[prop_name] = i;
    }
}

int
TAO_Property_Evaluator_By_Name::
is_dynamic_property(const char* property_name)
{
  int predicate = 0;
  string prop_name (property_name);
  Lookup_Table_Iter lookup_iter = this->table_.find (prop_name);

  // If the property name is in the map, delegate evaluation to our
  // superclass. Otherwise, throw an exception.
  if (lookup_iter != this->table_.end())
    {
      int index = (*lookup_iter).second;
      predicate = TAO_Property_Evaluator::is_dynamic_property(index);
    }

  return predicate;
}
  
CORBA::Any*
TAO_Property_Evaluator_By_Name::property_value(const char* property_name,
					       CORBA::Environment& _env)
  TAO_THROW_SPEC ((CosTradingDynamic::DPEvalFailure))
{
  CORBA::Any* prop_value = 0;
  string prop_name(property_name);
  Lookup_Table_Iter lookup_iter = this->table_.find(prop_name);

  // If the property name is in the map, delegate evaluation to our
  // superclass. Otherwise, throw an exception.
  if (lookup_iter != this->table_.end())
    {
      int index = (*lookup_iter).second;
      prop_value = TAO_Property_Evaluator::property_value(index, _env);
    }

  return prop_value;
}
  
CORBA::TypeCode*
TAO_Property_Evaluator_By_Name::property_type(const char* property_name)
{
  string prop_name(property_name);
  CORBA::TypeCode* prop_type = CORBA::TypeCode::_nil();
  Lookup_Table_Iter lookup_iter = this->table_.find(prop_name);
  
  // If the property name is in the map, delegate evaluation to our
  // superclass. Otherwise, throw an exception.
  if (lookup_iter != this->table_.end())
    {
      int index = (*lookup_iter).second;
      prop_type = TAO_Property_Evaluator::property_type(index);
    }

  return prop_type;
}

const CosTrading::Property*
TAO_Property_Evaluator_By_Name::get_property (const char* property_name)
{
  CosTrading::Property* property = 0;
  string prop_name (property_name);
  Lookup_Table_Iter lookup_iter = this->table_.find(prop_name);

  if (lookup_iter != this->table_.end())
    property = &this->props_[(*lookup_iter).second];

  return property;
}