Source code of Windows XP (NT5)
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.

589 lines
13 KiB

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