example_tc_bitmap.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /**
  2. * Tencent is pleased to support the open source community by making Tars available.
  3. *
  4. * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  7. * in compliance with the License. You may obtain a copy of the License at
  8. *
  9. * https://opensource.org/licenses/BSD-3-Clause
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed
  12. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations under the License.
  15. */
  16. #include "util/tc_shm.h"
  17. #include "util/tc_bitmap.h"
  18. #include "util/tc_option.h"
  19. #include "util/tc_common.h"
  20. #include <iterator>
  21. #include <iostream>
  22. using namespace tars;
  23. TC_Shm g_shm;
  24. TC_BitMap g_bmap;
  25. void initMap()
  26. {
  27. unsigned iBit = 3;
  28. size_t iMemSize = TC_BitMap::calcMemSize(100, iBit);
  29. g_shm.init(iMemSize, 9999);
  30. if (g_shm.iscreate())
  31. {
  32. g_bmap.create(g_shm.getPointer(), g_shm.size(), iBit);
  33. }
  34. else
  35. {
  36. g_bmap.connect(g_shm.getPointer(), g_shm.size(), iBit);
  37. }
  38. cout << "init mem:" << iMemSize << endl;
  39. }
  40. void usage(char *argv[])
  41. {
  42. cout << argv[0] << " --get=uin --bit" << endl;
  43. cout << argv[0] << " --set=uin --bit" << endl;
  44. cout << argv[0] << " --clear=uin --bit" << endl;
  45. cout << argv[0] << " --clear4all --bit" << endl;
  46. cout << argv[0] << " --dump=file" << endl;
  47. cout << argv[0] << " --load=file" << endl;
  48. cout << argv[0] << " --help" << endl;
  49. }
  50. int main(int argc, char *argv[])
  51. {
  52. try
  53. {
  54. TC_Option op;
  55. op.decode(argc, argv);
  56. initMap();
  57. if(op.hasParam("get"))
  58. {
  59. size_t uin = TC_Common::strto<size_t>(op.getValue("get"));
  60. unsigned bit = TC_Common::strto<unsigned>(op.getValue("bit"));
  61. cout << "get:" << uin << "=" << g_bmap.get(uin, bit) << endl;
  62. }
  63. else if(op.hasParam("set"))
  64. {
  65. size_t uin = TC_Common::strto<size_t>(op.getValue("set"));
  66. unsigned bit = TC_Common::strto<unsigned>(op.getValue("bit"));
  67. cout << "set:" << uin << "=" << g_bmap.set(uin, bit) << endl;
  68. }
  69. else if(op.hasParam("clear"))
  70. {
  71. size_t uin = TC_Common::strto<size_t>(op.getValue("clear"));
  72. unsigned bit = TC_Common::strto<unsigned>(op.getValue("bit"));
  73. cout << "clear:" << uin << "=" << g_bmap.clear(uin, bit) << endl;
  74. }
  75. else if(op.hasParam("clear4all"))
  76. {
  77. int bit = TC_Common::strto<int>(op.getValue("bit"));
  78. cout << "clear4all:" << bit << "=" << g_bmap.clear4all((unsigned)bit) << endl;
  79. }
  80. else if(op.hasParam("dump"))
  81. {
  82. string file = op.getValue("dump");
  83. int ret = g_bmap.dump2file(file);
  84. if(ret == 0)
  85. cout << "dump2file:" << file << " ok" << endl;
  86. else
  87. cout << "dump2file:" << file << " err:" << ret << endl;
  88. }
  89. else if(op.hasParam("load"))
  90. {
  91. string file = op.getValue("load");
  92. int ret = g_bmap.load5file(file);
  93. if(ret == 0)
  94. cout << "load5file:" << file << " ok" << endl;
  95. else
  96. cout << "load5file:" << file << " err:" << ret << endl;
  97. }
  98. else
  99. {
  100. usage(argv);
  101. }
  102. }
  103. catch(exception &ex)
  104. {
  105. cout << ex.what() << endl;
  106. }
  107. return 0;
  108. }