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.

201 lines
5.4 KiB

  1. /*
  2. Copyright (c) 2016, Antonio SJ Musumeci <trapexit@spawn.link>
  3. Permission to use, copy, modify, and/or distribute this software for any
  4. purpose with or without fee is hereby granted, provided that the above
  5. copyright notice and this permission notice appear in all copies.
  6. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  7. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  8. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  9. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  10. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  11. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  12. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  13. */
  14. #pragma once
  15. #include "branch.hpp"
  16. #include "category.hpp"
  17. #include "fs.hpp"
  18. #include <map>
  19. #include <string>
  20. #include <vector>
  21. class Policy
  22. {
  23. public:
  24. struct Enum
  25. {
  26. enum Type
  27. {
  28. invalid = -1,
  29. BEGIN = 0,
  30. all = BEGIN,
  31. epall,
  32. epff,
  33. eplfs,
  34. eplus,
  35. epmfs,
  36. eprand,
  37. erofs,
  38. ff,
  39. lfs,
  40. lus,
  41. mfs,
  42. newest,
  43. rand,
  44. END
  45. };
  46. static size_t begin() { return BEGIN; }
  47. static size_t end() { return END; }
  48. };
  49. struct Func
  50. {
  51. typedef std::string string;
  52. typedef std::vector<string> strvec;
  53. typedef std::vector<const string*> cstrptrvec;
  54. typedef const string cstring;
  55. typedef const uint64_t cuint64_t;
  56. typedef const strvec cstrvec;
  57. typedef const Category::Enum::Type CType;
  58. typedef int (*Ptr)(CType,const Branches &,const char *,cuint64_t,cstrptrvec &);
  59. template <CType T>
  60. class Base
  61. {
  62. public:
  63. Base(const Policy *p)
  64. : func(p->_func)
  65. {}
  66. int
  67. operator()(const Branches &b,const char *c,cuint64_t d,cstrptrvec &e)
  68. {
  69. return func(T,b,c,d,e);
  70. }
  71. int
  72. operator()(const Branches &b,const string &c,cuint64_t d,cstrptrvec &e)
  73. {
  74. return func(T,b,c.c_str(),d,e);
  75. }
  76. int
  77. operator()(const Branches &b,const char *c,cuint64_t d,string *e)
  78. {
  79. int rv;
  80. cstrptrvec v;
  81. rv = func(T,b,c,d,v);
  82. if(!v.empty())
  83. *e = *v[0];
  84. return rv;
  85. }
  86. private:
  87. const Ptr func;
  88. };
  89. typedef Base<Category::Enum::action> Action;
  90. typedef Base<Category::Enum::create> Create;
  91. typedef Base<Category::Enum::search> Search;
  92. static int invalid(CType,const Branches&,const char *,cuint64_t,cstrptrvec&);
  93. static int all(CType,const Branches&,const char*,cuint64_t,cstrptrvec&);
  94. static int epall(CType,const Branches&,const char*,cuint64_t,cstrptrvec&);
  95. static int epff(CType,const Branches&,const char *,cuint64_t,cstrptrvec&);
  96. static int eplfs(CType,const Branches&,const char *,cuint64_t,cstrptrvec&);
  97. static int eplus(CType,const Branches&,const char *,cuint64_t,cstrptrvec&);
  98. static int epmfs(CType,const Branches&,const char *,cuint64_t,cstrptrvec&);
  99. static int eprand(CType,const Branches&,const char *,cuint64_t,cstrptrvec&);
  100. static int erofs(CType,const Branches&,const char *,cuint64_t,cstrptrvec&);
  101. static int ff(CType,const Branches&,const char *,cuint64_t,cstrptrvec&);
  102. static int lfs(CType,const Branches&,const char *,cuint64_t,cstrptrvec&);
  103. static int lus(CType,const Branches&,const char *,cuint64_t,cstrptrvec&);
  104. static int mfs(CType,const Branches&,const char *,cuint64_t,cstrptrvec&);
  105. static int newest(CType,const Branches&,const char *,cuint64_t,cstrptrvec&);
  106. static int rand(CType,const Branches&,const char *,cuint64_t,cstrptrvec&);
  107. };
  108. private:
  109. Enum::Type _enum;
  110. std::string _str;
  111. Func::Ptr _func;
  112. bool _path_preserving;
  113. public:
  114. Policy()
  115. : _enum(invalid),
  116. _str(invalid),
  117. _func(invalid),
  118. _path_preserving(false)
  119. {
  120. }
  121. Policy(const Enum::Type enum_,
  122. const std::string &str_,
  123. const Func::Ptr func_,
  124. const bool path_preserving_)
  125. : _enum(enum_),
  126. _str(str_),
  127. _func(func_),
  128. _path_preserving(path_preserving_)
  129. {
  130. }
  131. bool
  132. path_preserving() const
  133. {
  134. return _path_preserving;
  135. }
  136. public:
  137. operator const Enum::Type() const { return _enum; }
  138. operator const std::string&() const { return _str; }
  139. operator const Func::Ptr() const { return _func; }
  140. operator const Policy*() const { return this; }
  141. bool operator==(const Enum::Type enum_) const
  142. { return _enum == enum_; }
  143. bool operator==(const std::string &str_) const
  144. { return _str == str_; }
  145. bool operator==(const Func::Ptr func_) const
  146. { return _func == func_; }
  147. bool operator!=(const Policy &r) const
  148. { return _enum != r._enum; }
  149. bool operator<(const Policy &r) const
  150. { return _enum < r._enum; }
  151. public:
  152. static const Policy &find(const std::string&);
  153. static const Policy &find(const Enum::Type);
  154. public:
  155. static const std::vector<Policy> _policies_;
  156. static const Policy * const policies;
  157. static const Policy &invalid;
  158. static const Policy &all;
  159. static const Policy &epall;
  160. static const Policy &epff;
  161. static const Policy &eplfs;
  162. static const Policy &eplus;
  163. static const Policy &epmfs;
  164. static const Policy &eprand;
  165. static const Policy &erofs;
  166. static const Policy &ff;
  167. static const Policy &lfs;
  168. static const Policy &lus;
  169. static const Policy &mfs;
  170. static const Policy &newest;
  171. static const Policy &rand;
  172. };