extend.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright (C) 2021 Duowan Inc. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing,
  11. * software distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef __X_PACK_EXTEND_H
  17. #define __X_PACK_EXTEND_H
  18. #include <cstddef>
  19. #include <map>
  20. #include <set>
  21. #include "config.h"
  22. #include "util.h"
  23. namespace xpack {
  24. // user flag
  25. #define X_PACK_FLAG_0 0
  26. #define X_PACK_FLAG_OE (1<<0) // omitempty, in encode
  27. #define X_PACK_FLAG_M (1<<1) // mandatory, in decode
  28. #define X_PACK_FLAG_ATTR (1<<15) // for xml encode, encode in attribute
  29. // control flag
  30. #define X_PACK_CTRL_FLAG_INHERIT (1<<0)
  31. // Alias name. [def ][type:name[,flag,key@value,flag]] def not support flag
  32. struct Alias {
  33. const char *raw; // raw name
  34. const char *alias; // alias define
  35. struct Type {
  36. std::string name;
  37. std::set<std::string> flags;
  38. std::map<std::string, std::string> kv_flags;
  39. };
  40. Alias(const char *_raw, const char *_alias):raw(_raw), alias(_alias) {
  41. std::vector<std::string> tps;
  42. Util::split(tps, alias, ' ');
  43. for (size_t i=0; i<tps.size(); ++i) {
  44. std::vector<std::string> typeFlag;
  45. Util::split(typeFlag, tps[i], ':', 1);
  46. if (typeFlag.size() == 1) { // no ':', default name
  47. def = tps[i];
  48. } else { // type:name[,flags]
  49. Type tp;
  50. std::vector<std::string> nameFlags;
  51. Util::split(nameFlags, typeFlag[1], ',');
  52. tp.name = nameFlags[0];
  53. if (nameFlags.size() > 1) {
  54. for (size_t j=1; j<nameFlags.size(); ++j) {
  55. std::vector<std::string> kvFlag;
  56. Util::split(kvFlag, nameFlags[j], '@', 1);
  57. if (kvFlag.size() == 1) {
  58. tp.flags.insert(nameFlags[j]);
  59. } else {
  60. tp.kv_flags[kvFlag[0]] = kvFlag[1];
  61. }
  62. }
  63. }
  64. types[typeFlag[0]] = tp;
  65. }
  66. }
  67. }
  68. const char *Name(const char *type) const {
  69. std::map<std::string, Type>::const_iterator iter = types.find(type);
  70. if (iter == types.end()) {
  71. if (def.empty()) {
  72. return raw;
  73. } else {
  74. return def.c_str();
  75. }
  76. } else {
  77. return iter->second.name.c_str();
  78. }
  79. }
  80. std::string Flag(const std::string&type, const std::string& flag) const {
  81. std::map<std::string, Type>::const_iterator it1 = types.find(type);
  82. if (it1 != types.end()) {
  83. if (it1->second.flags.find(flag) != it1->second.flags.end()) {
  84. return flag;
  85. }
  86. std::map<std::string, std::string>::const_iterator it2 = it1->second.kv_flags.find(flag);
  87. if (it2 != it1->second.kv_flags.end()) {
  88. return it2->second;
  89. }
  90. }
  91. return "";
  92. }
  93. private:
  94. std::string def; // default name
  95. std::map<std::string, Type> types;
  96. };
  97. struct Extend {
  98. int flag;
  99. int ctrl_flag;
  100. const Alias *alias;
  101. Extend(int _flag, const Alias *_alias):flag(_flag), ctrl_flag(0), alias(_alias) {
  102. }
  103. Extend(const Extend *ext) {
  104. if (NULL != ext) {
  105. flag = ext->flag;
  106. ctrl_flag = ext->ctrl_flag;
  107. alias = ext->alias;
  108. } else {
  109. flag = 0;
  110. ctrl_flag = 0;
  111. alias = NULL;
  112. }
  113. }
  114. static int Flag(const Extend *ext) {
  115. if (NULL == ext) {
  116. return 0;
  117. } else {
  118. return ext->flag;
  119. }
  120. }
  121. static int CtrlFlag(const Extend *ext) {
  122. if (NULL == ext) {
  123. return 0;
  124. } else {
  125. return ext->ctrl_flag;
  126. }
  127. }
  128. static bool OmitEmpty(const Extend *ext) {
  129. return NULL!=ext && (ext->flag&X_PACK_FLAG_OE);
  130. }
  131. static bool Mandatory(const Extend *ext) {
  132. return NULL!=ext && (ext->flag&X_PACK_FLAG_M);
  133. }
  134. static bool Attribute(const Extend *ext) {
  135. return NULL!=ext && (ext->flag&X_PACK_FLAG_ATTR);
  136. }
  137. };
  138. }
  139. #endif