Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

590 lines
13 KiB

  1. /*++
  2. 1998 Seagate Software, Inc. All rights reserved
  3. Module Name:
  4. wsbushrt.cpp
  5. Abstract:
  6. This component is an object representations of the USHORT standard type. It
  7. is both a persistable and collectable.
  8. Author:
  9. Chuck Bardeen [cbardeen] 29-Oct-1996
  10. Revision History:
  11. --*/
  12. #include "stdafx.h"
  13. #include "wsbushrt.h"
  14. HRESULT
  15. CWsbUshort::CompareToUshort(
  16. IN USHORT value,
  17. OUT SHORT* pResult
  18. )
  19. /*++
  20. Implements:
  21. IWsbUshort::CompareToUshort
  22. --*/
  23. {
  24. HRESULT hr = E_FAIL;
  25. SHORT result;
  26. WsbTraceIn(OLESTR("CWsbUshort::CompareToUshort"), OLESTR("value = <%ld>"), value);
  27. // Compare the values.
  28. if (m_value == value) {
  29. result = 0;
  30. }
  31. else if (m_value > value) {
  32. result = 1;
  33. }
  34. else {
  35. result = -1;
  36. }
  37. // If the aren't equal, then return false.
  38. if (result != 0) {
  39. hr = S_FALSE;
  40. }
  41. else {
  42. hr = S_OK;
  43. }
  44. // If they asked for the relative value back, then return it to them.
  45. if (pResult != NULL) {
  46. *pResult = result;
  47. }
  48. WsbTraceOut(OLESTR("CWsbUshort::CompareToUshort"), OLESTR("hr = <%ls>, result = <%d>"), WsbHrAsString(hr), result);
  49. return(hr);
  50. }
  51. HRESULT
  52. CWsbUshort::CompareToIUshort(
  53. IN IWsbUshort* pUshort,
  54. OUT SHORT* pResult
  55. )
  56. /*++
  57. Implements:
  58. IWsbUshort::CompareToIShort
  59. --*/
  60. {
  61. HRESULT hr = E_FAIL;
  62. USHORT value;
  63. WsbTraceIn(OLESTR("CWsbUshort::CompareToIUshort"), OLESTR(""));
  64. try {
  65. // Did they give us a valid item to compare to?
  66. WsbAssert(0 != pUshort, E_POINTER);
  67. // Get it's value and compare them.
  68. WsbAffirmHr(pUshort->GetUshort(&value));
  69. hr = CompareToUshort(value, pResult);
  70. } WsbCatch(hr);
  71. WsbTraceOut(OLESTR("CWsbUshort::CompareToIUshort"), OLESTR("hr = <%ls>, result = <%ls>"), WsbHrAsString(hr), WsbPtrToShortAsString(pResult));
  72. return(hr);
  73. }
  74. HRESULT
  75. CWsbUshort::CompareTo(
  76. IN IUnknown* pCollectable,
  77. OUT SHORT* pResult
  78. )
  79. /*++
  80. Implements:
  81. IWsbCollectable::CompareTo
  82. --*/
  83. {
  84. HRESULT hr = E_FAIL;
  85. IWsbUshort* pUshort;
  86. WsbTraceIn(OLESTR("CWsbUshort::CompareTo"), OLESTR(""));
  87. try {
  88. // Did they give us a valid item to compare to?
  89. WsbAssert(0 != pCollectable, E_POINTER);
  90. // We need the IWsbUshort interface to get the value of the object.
  91. WsbAffirmHr(pCollectable->QueryInterface(IID_IWsbUshort, (void**) &pUshort));
  92. hr = CompareToIUshort(pUshort, pResult);
  93. } WsbCatch(hr);
  94. WsbTraceOut(OLESTR("CWsbUshort::CompareTo"), OLESTR("hr = <%ls>, result = <%ls>"), WsbHrAsString(hr), WsbPtrToShortAsString(pResult));
  95. return(hr);
  96. }
  97. HRESULT
  98. CWsbUshort::FinalConstruct(
  99. void
  100. )
  101. /*++
  102. Implements:
  103. CComObjectRoot::FinalConstruct
  104. --*/
  105. {
  106. HRESULT hr = S_OK;
  107. try {
  108. WsbAffirmHr(CWsbObject::FinalConstruct());
  109. m_value = 0;
  110. } WsbCatch(hr);
  111. return(hr);
  112. }
  113. HRESULT
  114. CWsbUshort::GetUshort(
  115. OUT USHORT* pValue
  116. )
  117. /*++
  118. Implements:
  119. IWsbUshort::GetUshort
  120. --*/
  121. {
  122. HRESULT hr = S_OK;
  123. WsbTraceIn(OLESTR("CWsbUshort::GetUshort"), OLESTR(""));
  124. try {
  125. WsbAssert(0 != pValue, E_POINTER);
  126. *pValue = m_value;
  127. } WsbCatch(hr);
  128. WsbTraceOut(OLESTR("CWsbUshort::GetUshort"), OLESTR("hr = <%ls>, value = <%ld>"), WsbHrAsString(hr), m_value);
  129. return(hr);
  130. }
  131. HRESULT
  132. CWsbUshort::GetClassID(
  133. OUT CLSID* pClsid
  134. )
  135. /*++
  136. Implements:
  137. IPersist::GetClassID
  138. --*/
  139. {
  140. HRESULT hr = S_OK;
  141. WsbTraceIn(OLESTR("CWsbUshort::GetClassID"), OLESTR(""));
  142. try {
  143. WsbAssert(0 != pClsid, E_POINTER);
  144. *pClsid = CLSID_CWsbUshort;
  145. } WsbCatch(hr);
  146. WsbTraceOut(OLESTR("CWsbUshort::GetClassID"), OLESTR("hr = <%ls>, CLSID = <%ls>"), WsbHrAsString(hr), WsbGuidAsString(*pClsid));
  147. return(hr);
  148. }
  149. HRESULT
  150. CWsbUshort::GetSizeMax(
  151. ULARGE_INTEGER* pcbSize
  152. )
  153. /*++
  154. Implements:
  155. IPersistStream::GetSizeMax
  156. --*/
  157. {
  158. HRESULT hr = S_OK;
  159. WsbTraceIn(OLESTR("CWsbUshort::GetSizeMax"), OLESTR(""));
  160. try {
  161. WsbAssert(0 != pcbSize, E_POINTER);
  162. pcbSize->QuadPart = WsbPersistSizeOf(USHORT);
  163. } WsbCatch(hr);
  164. WsbTraceOut(OLESTR("CWsbUshort::GetSizeMax"), OLESTR("hr = <%ls>, Size = <%ls>"), WsbHrAsString(hr), WsbPtrToUliAsString(pcbSize));
  165. return(hr);
  166. }
  167. HRESULT
  168. CWsbUshort::Load(
  169. IStream* pStream
  170. )
  171. /*++
  172. Implements:
  173. IPersistStream::Load
  174. --*/
  175. {
  176. HRESULT hr = S_OK;
  177. WsbTraceIn(OLESTR("CWsbUshort::Load"), OLESTR(""));
  178. try {
  179. WsbAffirmHr(WsbLoadFromStream(pStream, &m_value));
  180. } WsbCatch(hr);
  181. WsbTraceOut(OLESTR("CWsbUshort::Load"), OLESTR("hr = <%ls>, value = <%ld>"), WsbHrAsString(hr), m_value);
  182. return(hr);
  183. }
  184. HRESULT
  185. CWsbUshort::Save(
  186. IStream* pStream,
  187. BOOL clearDirty
  188. )
  189. /*++
  190. Implements:
  191. IPersistStream::Save
  192. --*/
  193. {
  194. HRESULT hr = S_OK;
  195. WsbTraceIn(OLESTR("CWsbUshort::Save"), OLESTR("clearDirty = <%ls>"), WsbBoolAsString(clearDirty));
  196. try {
  197. WsbAffirmHr(WsbSaveToStream(pStream, m_value));
  198. // If we got it saved and we were asked to clear the dirty bit, then
  199. // do so now.
  200. if (clearDirty) {
  201. m_isDirty = FALSE;
  202. }
  203. } WsbCatch(hr);
  204. WsbTraceOut(OLESTR("CWsbUshort::Save"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  205. return(hr);
  206. }
  207. HRESULT
  208. CWsbUshort::SetUshort(
  209. USHORT value
  210. )
  211. /*++
  212. Implements:
  213. IWsbUshort::SetUshort
  214. --*/
  215. {
  216. WsbTraceIn(OLESTR("CWsbUshort::SetUshort"), OLESTR("value = <%ld>"), value);
  217. m_isDirty = TRUE;
  218. m_value = value;
  219. WsbTraceOut(OLESTR("CWsbUshort::SetUshort"), OLESTR(""));
  220. return(S_OK);
  221. }
  222. HRESULT
  223. CWsbUshort::Test(
  224. OUT USHORT* passed,
  225. OUT USHORT* failed
  226. )
  227. /*++
  228. Implements:
  229. IWsbTestable::Test
  230. --*/
  231. {
  232. *passed = 0;
  233. *failed = 0;
  234. HRESULT hr = S_OK;
  235. #if !defined(WSB_NO_TEST)
  236. CComPtr<IWsbUshort> pUshort1;
  237. CComPtr<IWsbUshort> pUshort2;
  238. // CComPtr<IPersistFile> pFile1;
  239. // CComPtr<IPersistFile> pFile2;
  240. USHORT value;
  241. SHORT result;
  242. WsbTraceIn(OLESTR("CWsbUshort::Test"), OLESTR(""));
  243. try {
  244. // Get the pUshort interface.
  245. hr = S_OK;
  246. try {
  247. WsbAffirmHr(((IUnknown*) (IWsbUshort*) this)->QueryInterface(IID_IWsbUshort, (void**) &pUshort1));
  248. // Set the bool to a value, and see if it is returned.
  249. hr = S_OK;
  250. try {
  251. WsbAffirmHr(pUshort1->SetUshort(65000));
  252. WsbAffirmHr(pUshort1->GetUshort(&value));
  253. WsbAssert(value == 65000, E_FAIL);
  254. } WsbCatch(hr);
  255. if (hr == S_OK) {
  256. (*passed)++;
  257. } else {
  258. (*failed)++;
  259. }
  260. // Create another instance and test the comparisson methods:
  261. try {
  262. WsbAffirmHr(CoCreateInstance(CLSID_CWsbUshort, NULL, CLSCTX_ALL, IID_IWsbUshort, (void**) &pUshort2));
  263. // Check the default values.
  264. hr = S_OK;
  265. try {
  266. WsbAffirmHr(pUshort2->GetUshort(&value));
  267. WsbAssert(value == 0, E_FAIL);
  268. } WsbCatch(hr);
  269. if (hr == S_OK) {
  270. (*passed)++;
  271. } else {
  272. (*failed)++;
  273. }
  274. // IsEqual()
  275. hr = S_OK;
  276. try {
  277. WsbAffirmHr(pUshort1->SetUshort(767));
  278. WsbAffirmHr(pUshort2->SetUshort(767));
  279. WsbAssert(pUshort1->IsEqual(pUshort2) == S_OK, E_FAIL);
  280. } WsbCatch(hr);
  281. if (hr == S_OK) {
  282. (*passed)++;
  283. } else {
  284. (*failed)++;
  285. }
  286. hr = S_OK;
  287. try {
  288. WsbAffirmHr(pUshort1->SetUshort(767));
  289. WsbAffirmHr(pUshort2->SetUshort(167));
  290. WsbAssert(pUshort1->IsEqual(pUshort2) == S_FALSE, E_FAIL);
  291. } WsbCatch(hr);
  292. if (hr == S_OK) {
  293. (*passed)++;
  294. } else {
  295. (*failed)++;
  296. }
  297. // CompareTo()
  298. hr = S_OK;
  299. try {
  300. WsbAffirmHr(pUshort1->SetUshort(900));
  301. WsbAffirmHr(pUshort2->SetUshort(900));
  302. WsbAssert((pUshort1->CompareTo(pUshort2, &result) == S_OK) && (0 == result), E_FAIL);
  303. } WsbCatch(hr);
  304. if (hr == S_OK) {
  305. (*passed)++;
  306. } else {
  307. (*failed)++;
  308. }
  309. hr = S_OK;
  310. try {
  311. WsbAffirmHr(pUshort1->SetUshort(900));
  312. WsbAffirmHr(pUshort2->SetUshort(1400));
  313. WsbAssert((pUshort1->CompareTo(pUshort2, &result) == S_FALSE) && (-1 == result), E_FAIL);
  314. } WsbCatch(hr);
  315. if (hr == S_OK) {
  316. (*passed)++;
  317. } else {
  318. (*failed)++;
  319. }
  320. hr = S_OK;
  321. try {
  322. WsbAffirmHr(pUshort1->SetUshort(15000));
  323. WsbAffirmHr(pUshort2->SetUshort(10000));
  324. WsbAssert((pUshort1->CompareTo(pUshort2, &result) == S_FALSE) && (1 == result), E_FAIL);
  325. } WsbCatch(hr);
  326. if (hr == S_OK) {
  327. (*passed)++;
  328. } else {
  329. (*failed)++;
  330. }
  331. #ifdef BOOL_PERSIST_FILE
  332. // TODO? Open the file and convert it to a stream?
  333. // Try out the persistence stuff.
  334. hr = S_OK;
  335. try {
  336. WsbAffirmHr(pUshort1->QueryInterface(IID_IPersistFile, (void**) &pFile1));
  337. WsbAffirmHr(pUshort2->QueryInterface(IID_IPersistFile, (void**) &pFile2));
  338. // The item should be dirty.
  339. hr = S_OK;
  340. try {
  341. WsbAffirmHr(pUshort2->SetUshort(777));
  342. WsbAssert(pFile2->IsDirty() == S_OK, E_FAIL);
  343. } WsbCatch(hr);
  344. if (hr == S_OK) {
  345. (*passed)++;
  346. } else {
  347. (*failed)++;
  348. }
  349. // Save the item, and remember.
  350. hr = S_OK;
  351. try {
  352. WsbAffirmHr(pFile2->Save(OLESTR("c:\\WsbTests\\WsbUshort.tst"), TRUE));
  353. } WsbCatch(hr);
  354. if (hr == S_OK) {
  355. (*passed)++;
  356. } else {
  357. (*failed)++;
  358. }
  359. // It shouldn't be dirty.
  360. hr = S_OK;
  361. try {
  362. WsbAssert(pFile2->IsDirty() == S_FALSE, E_FAIL);
  363. } WsbCatch(hr);
  364. if (hr == S_OK) {
  365. (*passed)++;
  366. } else {
  367. (*failed)++;
  368. }
  369. // Try reading it in to another object.
  370. hr = S_OK;
  371. try {
  372. WsbAffirmHr(pUshort1->SetUshort(888));
  373. WsbAffirmHr(pFile1->Load(OLESTR("c:\\WsbTests\\WsbUshort.tst"), 0));
  374. WsbAssert(pUshort1->CompareToUshort(777, NULL) == S_OK, E_FAIL);
  375. } WsbCatch(hr);
  376. if (hr == S_OK) {
  377. (*passed)++;
  378. } else {
  379. (*failed)++;
  380. }
  381. } WsbCatch(hr);
  382. if (hr == S_OK) {
  383. (*passed)++;
  384. } else {
  385. (*failed)++;
  386. }
  387. #endif
  388. } WsbCatch(hr);
  389. if (hr == S_OK) {
  390. (*passed)++;
  391. } else {
  392. (*failed)++;
  393. }
  394. } WsbCatch(hr);
  395. if (hr == S_OK) {
  396. (*passed)++;
  397. } else {
  398. (*failed)++;
  399. }
  400. // Tally up the results
  401. if (*failed) {
  402. hr = S_FALSE;
  403. } else {
  404. hr = S_OK;
  405. }
  406. } WsbCatch(hr);
  407. WsbTraceOut(OLESTR("CWsbUshort::Test"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  408. #endif
  409. return(hr);
  410. }