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. wsbulong.cpp
  5. Abstract:
  6. This component is an object representations of the ULONG 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 "wsbulong.h"
  14. HRESULT
  15. CWsbUlong::CompareToUlong(
  16. IN ULONG value,
  17. OUT SHORT* pResult
  18. )
  19. /*++
  20. Implements:
  21. IWsbUlong::CompareToUlong
  22. --*/
  23. {
  24. HRESULT hr = E_FAIL;
  25. SHORT result;
  26. WsbTraceIn(OLESTR("CWsbUlong::CompareToUlong"), 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("CWsbUlong::CompareToUlong"), OLESTR("hr = <%ls>, result = <%d>"), WsbHrAsString(hr), result);
  49. return(hr);
  50. }
  51. HRESULT
  52. CWsbUlong::CompareToIUlong(
  53. IN IWsbUlong* pUlong,
  54. OUT SHORT* pResult
  55. )
  56. /*++
  57. Implements:
  58. IWsbUlong::CompareToIUlong
  59. --*/
  60. {
  61. HRESULT hr = E_FAIL;
  62. ULONG value;
  63. WsbTraceIn(OLESTR("CWsbUlong::CompareToIUlong"), OLESTR(""));
  64. try {
  65. // Did they give us a valid item to compare to?
  66. WsbAssert(0 != pUlong, E_POINTER);
  67. // Get it's value and compare them.
  68. WsbAffirmHr(pUlong->GetUlong(&value));
  69. hr = CompareToUlong(value, pResult);
  70. } WsbCatch(hr);
  71. WsbTraceOut(OLESTR("CWsbUlong::CompareToIUlong"), OLESTR("hr = <%ls>, result = <%ls>"), WsbHrAsString(hr), WsbPtrToShortAsString(pResult));
  72. return(hr);
  73. }
  74. HRESULT
  75. CWsbUlong::CompareTo(
  76. IN IUnknown* pCollectable,
  77. OUT SHORT* pResult
  78. )
  79. /*++
  80. Implements:
  81. IWsbCollectable::CompareTo
  82. --*/
  83. {
  84. HRESULT hr = E_FAIL;
  85. IWsbUlong* pUlong;
  86. WsbTraceIn(OLESTR("CWsbUlong::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 IWsbUlong interface to get the value of the object.
  91. WsbAffirmHr(pCollectable->QueryInterface(IID_IWsbUlong, (void**) &pUlong));
  92. hr = CompareToIUlong(pUlong, pResult);
  93. } WsbCatch(hr);
  94. WsbTraceOut(OLESTR("CWsbUlong::CompareTo"), OLESTR("hr = <%ls>, result = <%ls>"), WsbHrAsString(hr), WsbPtrToShortAsString(pResult));
  95. return(hr);
  96. }
  97. HRESULT
  98. CWsbUlong::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. CWsbUlong::GetUlong(
  115. OUT ULONG* pValue
  116. )
  117. /*++
  118. Implements:
  119. IWsbUlong::GetUlong
  120. --*/
  121. {
  122. HRESULT hr = S_OK;
  123. WsbTraceIn(OLESTR("CWsbUlong::GetUlong"), OLESTR(""));
  124. try {
  125. WsbAssert(0 != pValue, E_POINTER);
  126. *pValue = m_value;
  127. } WsbCatch(hr);
  128. WsbTraceOut(OLESTR("CWsbUlong::GetUlong"), OLESTR("hr = <%ls>, value = <%ld>"), WsbHrAsString(hr), m_value);
  129. return(hr);
  130. }
  131. HRESULT
  132. CWsbUlong::GetClassID(
  133. OUT CLSID* pClsid
  134. )
  135. /*++
  136. Implements:
  137. IPersist::GetClassID
  138. --*/
  139. {
  140. HRESULT hr = S_OK;
  141. WsbTraceIn(OLESTR("CWsbUlong::GetClassID"), OLESTR(""));
  142. try {
  143. WsbAssert(0 != pClsid, E_POINTER);
  144. *pClsid = CLSID_CWsbUlong;
  145. } WsbCatch(hr);
  146. WsbTraceOut(OLESTR("CWsbUlong::GetClassID"), OLESTR("hr = <%ls>, CLSID = <%ls>"), WsbHrAsString(hr), WsbGuidAsString(*pClsid));
  147. return(hr);
  148. }
  149. HRESULT
  150. CWsbUlong::GetSizeMax(
  151. ULARGE_INTEGER* pcbSize
  152. )
  153. /*++
  154. Implements:
  155. IPersistStream::GetSizeMax
  156. --*/
  157. {
  158. HRESULT hr = S_OK;
  159. WsbTraceIn(OLESTR("CWsbUlong::GetSizeMax"), OLESTR(""));
  160. try {
  161. WsbAssert(0 != pcbSize, E_POINTER);
  162. pcbSize->QuadPart = WsbPersistSizeOf(ULONG);
  163. } WsbCatch(hr);
  164. WsbTraceOut(OLESTR("CWsbUlong::GetSizeMax"), OLESTR("hr = <%ls>, Size = <%ls>"), WsbHrAsString(hr), WsbPtrToUliAsString(pcbSize));
  165. return(hr);
  166. }
  167. HRESULT
  168. CWsbUlong::Load(
  169. IStream* pStream
  170. )
  171. /*++
  172. Implements:
  173. IPersistStream::Load
  174. --*/
  175. {
  176. HRESULT hr = S_OK;
  177. WsbTraceIn(OLESTR("CWsbUlong::Load"), OLESTR(""));
  178. try {
  179. WsbAffirmHr(WsbLoadFromStream(pStream, &m_value));
  180. } WsbCatch(hr);
  181. WsbTraceOut(OLESTR("CWsbUlong::Load"), OLESTR("hr = <%ls>, value = <%ld>"), WsbHrAsString(hr), m_value);
  182. return(hr);
  183. }
  184. HRESULT
  185. CWsbUlong::Save(
  186. IStream* pStream,
  187. BOOL clearDirty
  188. )
  189. /*++
  190. Implements:
  191. IPersistStream::Save
  192. --*/
  193. {
  194. HRESULT hr = S_OK;
  195. WsbTraceIn(OLESTR("CWsbUlong::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("CWsbUlong::Save"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  205. return(hr);
  206. }
  207. HRESULT
  208. CWsbUlong::SetUlong(
  209. ULONG value
  210. )
  211. /*++
  212. Implements:
  213. IWsbUlong::SetUlong
  214. --*/
  215. {
  216. WsbTraceIn(OLESTR("CWsbUlong::SetUlong"), OLESTR("value = <%ld>"), value);
  217. m_isDirty = TRUE;
  218. m_value = value;
  219. WsbTraceOut(OLESTR("CWsbUlong::SetUlong"), OLESTR(""));
  220. return(S_OK);
  221. }
  222. HRESULT
  223. CWsbUlong::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<IWsbUlong> pUlong1;
  237. CComPtr<IWsbUlong> pUlong2;
  238. // CComPtr<IPersistFile> pFile1;
  239. // CComPtr<IPersistFile> pFile2;
  240. ULONG value;
  241. SHORT result;
  242. WsbTraceIn(OLESTR("CWsbUlong::Test"), OLESTR(""));
  243. try {
  244. // Get the pUlong interface.
  245. hr = S_OK;
  246. try {
  247. WsbAffirmHr(((IUnknown*) (IWsbUlong*) this)->QueryInterface(IID_IWsbUlong, (void**) &pUlong1));
  248. // Set the bool to a value, and see if it is returned.
  249. hr = S_OK;
  250. try {
  251. WsbAffirmHr(pUlong1->SetUlong(0xffffffff));
  252. WsbAffirmHr(pUlong1->GetUlong(&value));
  253. WsbAssert(value == 0xffffffff, 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_CWsbUlong, NULL, CLSCTX_ALL, IID_IWsbUlong, (void**) &pUlong2));
  263. // Check the default values.
  264. hr = S_OK;
  265. try {
  266. WsbAffirmHr(pUlong2->GetUlong(&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(pUlong1->SetUlong(767));
  278. WsbAffirmHr(pUlong2->SetUlong(767));
  279. WsbAssert(pUlong1->IsEqual(pUlong2) == 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(pUlong1->SetUlong(767));
  289. WsbAffirmHr(pUlong2->SetUlong(65000));
  290. WsbAssert(pUlong1->IsEqual(pUlong2) == 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(pUlong1->SetUlong(900));
  301. WsbAffirmHr(pUlong2->SetUlong(900));
  302. WsbAssert((pUlong1->CompareTo(pUlong2, &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(pUlong1->SetUlong(500));
  312. WsbAffirmHr(pUlong2->SetUlong(1000));
  313. WsbAssert((pUlong1->CompareTo(pUlong2, &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(pUlong1->SetUlong(75000));
  323. WsbAffirmHr(pUlong2->SetUlong(20000));
  324. WsbAssert((pUlong1->CompareTo(pUlong2, &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(pUlong1->QueryInterface(IID_IPersistFile, (void**) &pFile1));
  337. WsbAffirmHr(pUlong2->QueryInterface(IID_IPersistFile, (void**) &pFile2));
  338. // The item should be dirty.
  339. hr = S_OK;
  340. try {
  341. WsbAffirmHr(pUlong2->SetUlong(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\\WsbUlong.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(pUlong1->SetUlong(888));
  373. WsbAffirmHr(pFile1->Load(OLESTR("c:\\WsbTests\\WsbUlong.tst"), 0));
  374. WsbAssert(pUlong1->CompareToUlong(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("CWsbUlong::Test"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  408. #endif
  409. return(hr);
  410. }