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.

585 lines
13 KiB

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