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.

209 lines
4.1 KiB

  1. /*
  2. Copyright (c) 2020, 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 "branches.hpp"
  16. #include "strvec.hpp"
  17. #include <string>
  18. namespace Policy
  19. {
  20. class ActionImpl
  21. {
  22. public:
  23. ActionImpl(const std::string &name_)
  24. : name(name_)
  25. {
  26. }
  27. public:
  28. std::string name;
  29. virtual int operator()(const Branches::CPtr&,const char*,StrVec*) const = 0;
  30. };
  31. class Action
  32. {
  33. public:
  34. Action(ActionImpl *impl_)
  35. : impl(impl_)
  36. {}
  37. Action&
  38. operator=(ActionImpl *impl_)
  39. {
  40. impl = impl_;
  41. return *this;
  42. }
  43. const
  44. std::string&
  45. name(void) const
  46. {
  47. return impl->name;
  48. }
  49. int
  50. operator()(const Branches::CPtr &branches_,
  51. const char *fusepath_,
  52. StrVec *paths_) const
  53. {
  54. return (*impl)(branches_,fusepath_,paths_);
  55. }
  56. int
  57. operator()(const Branches::CPtr &branches_,
  58. const std::string &fusepath_,
  59. StrVec *paths_) const
  60. {
  61. return (*impl)(branches_,fusepath_.c_str(),paths_);
  62. }
  63. operator bool() const
  64. {
  65. return (bool)impl;
  66. }
  67. private:
  68. ActionImpl *impl;
  69. };
  70. class CreateImpl
  71. {
  72. public:
  73. CreateImpl(const std::string &name_)
  74. : name(name_)
  75. {
  76. }
  77. public:
  78. std::string name;
  79. virtual int operator()(const Branches::CPtr&,const char*,StrVec*) const = 0;
  80. virtual bool path_preserving(void) const = 0;
  81. };
  82. class Create
  83. {
  84. public:
  85. Create(CreateImpl *impl_)
  86. : impl(impl_)
  87. {}
  88. Create&
  89. operator=(CreateImpl *impl_)
  90. {
  91. impl = impl_;
  92. return *this;
  93. }
  94. const
  95. std::string&
  96. name(void) const
  97. {
  98. return impl->name;
  99. }
  100. bool
  101. path_preserving(void) const
  102. {
  103. return impl->path_preserving();
  104. }
  105. int
  106. operator()(const Branches::CPtr &branches_,
  107. const char *fusepath_,
  108. StrVec *paths_) const
  109. {
  110. return (*impl)(branches_,fusepath_,paths_);
  111. }
  112. int
  113. operator()(const Branches::CPtr &branches_,
  114. const std::string &fusepath_,
  115. StrVec *paths_) const
  116. {
  117. return (*impl)(branches_,fusepath_.c_str(),paths_);
  118. }
  119. operator bool() const
  120. {
  121. return (bool)impl;
  122. }
  123. private:
  124. CreateImpl *impl;
  125. };
  126. class SearchImpl
  127. {
  128. public:
  129. SearchImpl(const std::string &name_)
  130. : name(name_)
  131. {
  132. }
  133. public:
  134. std::string name;
  135. virtual int operator()(const Branches::CPtr&,const char*,StrVec*) const = 0;
  136. };
  137. class Search
  138. {
  139. public:
  140. Search(SearchImpl *impl_)
  141. : impl(impl_)
  142. {}
  143. Search&
  144. operator=(SearchImpl *impl_)
  145. {
  146. impl = impl_;
  147. return *this;
  148. }
  149. const
  150. std::string&
  151. name(void) const
  152. {
  153. return impl->name;
  154. }
  155. int
  156. operator()(const Branches::CPtr &branches_,
  157. const char *fusepath_,
  158. StrVec *paths_) const
  159. {
  160. return (*impl)(branches_,fusepath_,paths_);
  161. }
  162. int
  163. operator()(const Branches::CPtr &branches_,
  164. const std::string &fusepath_,
  165. StrVec *paths_) const
  166. {
  167. return (*impl)(branches_,fusepath_.c_str(),paths_);
  168. }
  169. operator bool() const
  170. {
  171. return (bool)impl;
  172. }
  173. private:
  174. SearchImpl *impl;
  175. };
  176. }