impl.hpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright 2015 MongoDB Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #pragma once
  15. #include <bsoncxx/builder/basic/sub_array.hpp>
  16. #include <bsoncxx/builder/basic/sub_document.hpp>
  17. #include <bsoncxx/util/functor.hpp>
  18. #include <bsoncxx/config/prelude.hpp>
  19. namespace bsoncxx {
  20. BSONCXX_INLINE_NAMESPACE_BEGIN
  21. namespace builder {
  22. namespace basic {
  23. namespace impl {
  24. template <typename T>
  25. using takes_document = typename util::is_functor<T, void(sub_document)>;
  26. template <typename T>
  27. using takes_array = typename util::is_functor<T, void(sub_array)>;
  28. template <typename T>
  29. BSONCXX_INLINE typename std::enable_if<takes_document<T>::value, void>::type generic_append(
  30. core* core, T&& func) {
  31. core->open_document();
  32. func(sub_document(core));
  33. core->close_document();
  34. }
  35. template <typename T>
  36. BSONCXX_INLINE typename std::enable_if<takes_array<T>::value, void>::type generic_append(core* core,
  37. T&& func) {
  38. core->open_array();
  39. func(sub_array(core));
  40. core->close_array();
  41. }
  42. template <typename T>
  43. BSONCXX_INLINE
  44. typename std::enable_if<!takes_document<T>::value && !takes_array<T>::value, void>::type
  45. generic_append(core* core, T&& t) {
  46. core->append(std::forward<T>(t));
  47. }
  48. template <typename T>
  49. BSONCXX_INLINE void value_append(core* core, T&& t) {
  50. generic_append(core, std::forward<T>(t));
  51. }
  52. } // namespace impl
  53. } // namespace basic
  54. } // namespace builder
  55. BSONCXX_INLINE_NAMESPACE_END
  56. } // namespace bsoncxx
  57. #include <bsoncxx/config/postlude.hpp>