Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / TAO / tao / RTCORBA / Linear_Priority_Mapping.cpp
blobf88834ace7060dcaf61aff8f4a920ac3944874c8
1 #include "tao/orbconf.h"
3 #if defined (TAO_HAS_CORBA_MESSAGING) && TAO_HAS_CORBA_MESSAGING != 0
5 #include "tao/RTCORBA/Linear_Priority_Mapping.h"
6 #include "tao/debug.h"
7 #include "ace/Sched_Params.h"
8 #include "ace/Log_Msg.h"
10 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
12 TAO_Linear_Priority_Mapping::TAO_Linear_Priority_Mapping (long policy)
13 : policy_ (policy)
14 , min_ (ACE_Sched_Params::priority_min (this->policy_))
15 , max_ (ACE_Sched_Params::priority_max (this->policy_))
19 TAO_Linear_Priority_Mapping::~TAO_Linear_Priority_Mapping ()
23 CORBA::Boolean
24 TAO_Linear_Priority_Mapping::to_native (
25 RTCORBA::Priority corba_priority,
26 RTCORBA::NativePriority &native_priority)
28 if (corba_priority < RTCORBA::minPriority
29 // The line below will always be false unless the value of
30 // RTCORBA::maxPriority, which is now assigned the value of
31 // 32767, is changed in RTCORBA.pidl.
32 // || corba_priority > RTCORBA::maxPriority
35 return 0;
38 #if defined (ACE_WIN32)
39 // Count up the number of distinct native priorities on current
40 // platform.
41 int n;
42 int current_priority = this->min_;
43 for (n = 1; current_priority != this->max_; ++n)
45 current_priority =
46 ACE_Sched_Params::next_priority (this->policy_,
47 current_priority);
49 int native_priority_index =
51 + ((n - 1)
52 * (corba_priority - RTCORBA::minPriority)
53 / (RTCORBA::maxPriority - RTCORBA::minPriority));
55 // Now, find the value corresponding to this index.
56 native_priority = static_cast<RTCORBA::NativePriority> (this->min_);
57 for (int i = 2; i <= native_priority_index; ++i)
59 native_priority = static_cast<RTCORBA::NativePriority>
60 (ACE_Sched_Params::next_priority (this->policy_, native_priority));
62 return 1;
64 #else
66 native_priority =
67 this->min_
68 + ((this->max_ - this->min_)
69 * (corba_priority - RTCORBA::minPriority)
70 / (RTCORBA::maxPriority - RTCORBA::minPriority));
72 return 1;
74 #endif /* ACE_WIN32 */
77 CORBA::Boolean
78 TAO_Linear_Priority_Mapping::to_CORBA (RTCORBA::NativePriority native_priority,
79 RTCORBA::Priority &corba_priority)
81 #if defined (ACE_WIN32)
83 // Iterate over native priorities in order to 1) make sure
84 // <native_priority> argument contains a valid value, 2) count its
85 // index among all valid native pr. values, 3) get the total number
86 // of native priority values for the current platform.
88 int total;
89 int native_priority_index = 0;
90 int current_priority = this->min_;
91 for (total = 1; ; ++total)
93 if (native_priority == current_priority)
94 native_priority_index = total;
96 if (current_priority == this->max_)
97 break;
99 current_priority =
100 ACE_Sched_Params::next_priority (this->policy_,
101 current_priority);
104 if (native_priority_index == 0)
105 return 0;
107 int delta = total - 1;
108 if (delta != 0)
110 int numerator = (RTCORBA::maxPriority - RTCORBA::minPriority)
111 * (native_priority_index - 1);
113 div_t corba_offset = div (numerator, delta);
115 int rounding = 0;
117 if (corba_offset.rem)
119 rounding = ((numerator < 0 && delta < 0) ||
120 (numerator >= 0 && delta >= 0) ? 1 : -1);
123 corba_priority = static_cast<RTCORBA::Priority>
124 (RTCORBA::minPriority + corba_offset.quot + rounding);
126 else
128 // There is only one native priority.
129 corba_priority = RTCORBA::minPriority;
132 return 1;
134 #else
136 if ((this->min_ < this->max_
137 && (native_priority < this->min_
138 || native_priority > this->max_))
139 || (this->min_ > this->max_
140 && (native_priority < this->max_
141 || native_priority > this->min_)))
143 TAOLIB_DEBUG ((LM_DEBUG,
144 "TAO (%P|%t) - Linear_Priority_Mapping::to_CORBA: "
145 " priority %d out of range [%d,%d]\n",
146 native_priority, this->min_, this->max_));
147 return 0;
150 int delta = this->max_ - this->min_;
151 if (delta != 0)
153 int numerator = (RTCORBA::maxPriority - RTCORBA::minPriority)
154 * (native_priority - this->min_);
156 div_t corba_offset = div (numerator, delta);
158 int rounding = 0;
160 if (corba_offset.rem)
162 rounding = ((numerator < 0 && delta < 0) ||
163 (numerator >= 0 && delta >= 0) ? 1 : -1);
166 corba_priority =
167 RTCORBA::minPriority + corba_offset.quot + rounding;
169 else
171 // There is only one native priority.
172 if (native_priority != this->min_)
173 return 0;
174 corba_priority = RTCORBA::minPriority;
177 return 1;
179 #endif /* ACE_WIN32 */
182 TAO_END_VERSIONED_NAMESPACE_DECL
184 #endif /* TAO_HAS_CORBA_MESSAGING && TAO_HAS_CORBA_MESSAGING != 0 */