fork download
  1. #include <iostream>
  2. #include <map>
  3. #include <typeinfo>
  4.  
  5. struct Access {
  6.  
  7. static std::map<int, int> bits;
  8.  
  9. enum class Group1 : short {
  10. Right1 = 1 << 0,
  11. Right2 = 1 << 1,
  12. Key = 1
  13. };
  14.  
  15.  
  16. enum class Group2: short {
  17. Right1 = 1 << 0,
  18. Right2 = 1 << 1,
  19. Key = 2
  20. };
  21.  
  22. template<typename T>
  23. bool operator()(T bit)
  24. {
  25. std::cout << "T: " << typeid(T).name() << std::endl;
  26. return ((Access::bits.find(static_cast<int>(T::Key)) != Access::bits.end()) && (Access::bits[static_cast<int>(T::Key)] & bit) == bit);
  27. }
  28.  
  29. //bool operator()(Access::Group1 bit);
  30. //bool operator()(Access::Group2 bit);
  31.  
  32. Access operator=(std::map<int, int> rights)
  33. {
  34. Access::bits = rights;
  35. return *this;
  36. }
  37. } Access;
  38.  
  39. std::map<int, int> Access::bits;
  40.  
  41. // Bitvergleich Template
  42. template<typename T>
  43. T operator &(int lhs, T rhs)
  44. {
  45. return static_cast<T>(lhs & static_cast<int>(rhs));
  46. }
  47.  
  48. /*
  49. bool Access::operator()(Access::Group1 bit)
  50. {
  51.   return ((Access::bits.find(static_cast<int>(Access::Group1::Key)) != Access::bits.end()) && (Access::bits[static_cast<int>(Access::Group1::Key)] & bit) == bit);
  52. }
  53.  
  54. bool Access::operator()(Access::Group2 bit)
  55. {
  56.   return ((Access::bits.find(static_cast<int>(Access::Group2::Key)) != Access::bits.end()) && (Access::bits[static_cast<int>(Access::Group2::Key)] & bit) == bit);
  57. }
  58. */
  59.  
  60. int main(int argc, char *argv[]) {
  61.  
  62. // Diese Daten kommen aus der JSON Datei/Anfrage
  63. std::map<int, int> json = {
  64. {1, 7}
  65. };
  66.  
  67. Access = json;
  68.  
  69. if (Access(Access::Group1::Right1))
  70. {
  71. std::cout << "Access Group1::Right1 granted!" << std::endl;
  72. }
  73.  
  74. if (Access(Access::Group1::Right2))
  75. {
  76. std::cout << "Access Group1::Right2 granted!" << std::endl;
  77. }
  78.  
  79.  
  80. if (Access(Access::Group2::Right1))
  81. {
  82. std::cout << "Access Group2::Right1 granted!" << std::endl;
  83. }
  84.  
  85. return 0;
  86. }
Success #stdin #stdout 0s 15248KB
stdin
Standard input is empty
stdout
T: N6Access6Group1E
Access Group1::Right1 granted!
T: N6Access6Group1E
Access Group1::Right2 granted!
T: N6Access6Group2E