|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache LICENSE, Version 2.0 (the |
| 7 | + * "LICENSE"); you may not use this file except in compliance |
| 8 | + * with the LICENSE. You may obtain a copy of the LICENSE at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the LICENSE is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the LICENSE for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the LICENSE. |
| 18 | + */ |
| 19 | + |
| 20 | +#ifndef G42CLOUD_CPP_SDK_CORE_BSON_BUILDER_H |
| 21 | +#define G42CLOUD_CPP_SDK_CORE_BSON_BUILDER_H |
| 22 | + |
| 23 | +#include <string> |
| 24 | + |
| 25 | +#include <g42cloud/core/bson/Types.h> |
| 26 | +#include <g42cloud/core/bson/impl/LibBsonEncoder.h> |
| 27 | +#include <g42cloud/core/exception/SdkException.h> |
| 28 | + |
| 29 | +using namespace G42Cloud::Sdk::Core::Exception; |
| 30 | + |
| 31 | +namespace G42Cloud { |
| 32 | +namespace Sdk { |
| 33 | +namespace Core { |
| 34 | +namespace Bson { |
| 35 | + |
| 36 | +/**************************** Stream manipulator ***************************/ |
| 37 | +// Begin append child document |
| 38 | +struct OpenDocumentType { |
| 39 | + constexpr OpenDocumentType() = default; |
| 40 | +}; |
| 41 | + |
| 42 | +// End append child document |
| 43 | +struct CloseDocumentType { |
| 44 | + constexpr CloseDocumentType() = default; |
| 45 | +}; |
| 46 | + |
| 47 | +// Begin append child array. |
| 48 | +struct OpenArrayType { |
| 49 | + constexpr OpenArrayType() = default; |
| 50 | +}; |
| 51 | + |
| 52 | +// End append child array. |
| 53 | +struct CloseArrayType { |
| 54 | + constexpr CloseArrayType() = default; |
| 55 | +}; |
| 56 | + |
| 57 | +// Builder should generate document bson object. |
| 58 | +struct BuildDocumentType { |
| 59 | + constexpr BuildDocumentType() = default; |
| 60 | +}; |
| 61 | + |
| 62 | +// Builder should generate array bson object. |
| 63 | +struct BuildArrayType { |
| 64 | + constexpr BuildArrayType() = default; |
| 65 | +}; |
| 66 | +/**************************** Stream manipulator end ***************************/ |
| 67 | + |
| 68 | +/** |
| 69 | + * Builder bson object with stream style, which is based capacity of LibBsonEncoder. |
| 70 | + */ |
| 71 | +class Builder { |
| 72 | +public: |
| 73 | + Builder(Builder && other) noexcept : key_(std::move(other.key_)), encoder_(other.encoder_) { |
| 74 | + other.encoder_ = nullptr; |
| 75 | + } |
| 76 | + |
| 77 | + virtual ~Builder() { |
| 78 | + delete encoder_; |
| 79 | + } |
| 80 | + |
| 81 | + static Builder array() { |
| 82 | + return Builder(true); |
| 83 | + } |
| 84 | + |
| 85 | + static Builder document() { |
| 86 | + return Builder(false); |
| 87 | + } |
| 88 | + |
| 89 | + Builder &operator<<(const OpenDocumentType &value); |
| 90 | + |
| 91 | + Builder &operator<<(const CloseDocumentType &value); |
| 92 | + |
| 93 | + Builder &operator<<(const OpenArrayType &value); |
| 94 | + |
| 95 | + Builder &operator<<(const CloseArrayType &value); |
| 96 | + |
| 97 | + virtual Builder &operator<<(const char *value); |
| 98 | + |
| 99 | + virtual Builder &operator<<(const std::string &value); |
| 100 | + |
| 101 | + Document operator<<(const BuildDocumentType &value); |
| 102 | + |
| 103 | + Array operator<<(const BuildArrayType &value); |
| 104 | + |
| 105 | + template<class T> |
| 106 | + Builder &operator<<(const T &value); |
| 107 | + |
| 108 | + static constexpr OpenDocumentType SubDocumentBegin{}; |
| 109 | + static constexpr CloseDocumentType SubDocumentEnd{}; |
| 110 | + static constexpr OpenArrayType SubArrayBegin{}; |
| 111 | + static constexpr CloseArrayType SubArrayEnd{}; |
| 112 | + static constexpr BuildDocumentType DocumentEnd{}; |
| 113 | + static constexpr BuildArrayType ArrayEnd{}; |
| 114 | + |
| 115 | +private: |
| 116 | + explicit Builder(bool isArray) : encoder_(new LibBsonEncoder(isArray)) {} |
| 117 | + |
| 118 | + Builder(const Builder &) = default; |
| 119 | + |
| 120 | + Builder &operator=(const Builder &) = default; |
| 121 | + |
| 122 | + BsonEncoder *encoder_{nullptr}; |
| 123 | + std::string key_; |
| 124 | +}; |
| 125 | + |
| 126 | +#define ENSURE_KEY_SET_IN_ARRAY() \ |
| 127 | + if (encoder_->isArray()) { \ |
| 128 | + key_ = encoder_->nextKey(); \ |
| 129 | + } |
| 130 | + |
| 131 | +#define THROW_EXCEPTION_IF_KEY_ABSENT() \ |
| 132 | + if (key_.empty()) { \ |
| 133 | + throw SdkException("Document key is empty."); \ |
| 134 | + } |
| 135 | + |
| 136 | +#define CLEAR_AFTER_APPEND() \ |
| 137 | + key_.clear(); |
| 138 | + |
| 139 | +template<class T> |
| 140 | +Builder &Builder::operator<<(const T &value) { |
| 141 | + ENSURE_KEY_SET_IN_ARRAY() |
| 142 | + THROW_EXCEPTION_IF_KEY_ABSENT() |
| 143 | + |
| 144 | + encoder_->append(key_, value); |
| 145 | + |
| 146 | + CLEAR_AFTER_APPEND() |
| 147 | + |
| 148 | + return *this; |
| 149 | +} |
| 150 | + |
| 151 | +} // namespace Bson |
| 152 | +} // namespace Core |
| 153 | +} // namespace Sdk |
| 154 | +} // namespace G42Cloud |
| 155 | + |
| 156 | +#endif //G42CLOUD_CPP_SDK_CORE_BSON_BUILDER_H |
0 commit comments