weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2018, Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of NDN repo-ng (Next generation of NDN repository). |
| 6 | * See AUTHORS.md for complete list of repo-ng authors and contributors. |
| 7 | * |
| 8 | * repo-ng is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * repo-ng is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * repo-ng, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | #ifndef REPO_REPO_COMMAND_HPP |
| 20 | #define REPO_REPO_COMMAND_HPP |
| 21 | |
| 22 | #include "common.hpp" |
| 23 | #include "repo-command-parameter.hpp" |
| 24 | |
| 25 | #include <stdexcept> |
| 26 | |
| 27 | namespace repo { |
| 28 | |
| 29 | class RepoCommand : boost::noncopyable |
| 30 | { |
| 31 | public: |
| 32 | /** \brief represents an error in RepoCommandParameters |
| 33 | */ |
| 34 | class ArgumentError : public std::invalid_argument |
| 35 | { |
| 36 | public: |
| 37 | explicit |
| 38 | ArgumentError(const std::string& what) |
| 39 | : std::invalid_argument(what) |
| 40 | { |
| 41 | } |
| 42 | }; |
| 43 | |
| 44 | virtual |
| 45 | ~RepoCommand() = default; |
| 46 | |
| 47 | class FieldValidator |
| 48 | { |
| 49 | public: |
| 50 | FieldValidator(); |
| 51 | |
| 52 | /** \brief declare a required field |
| 53 | */ |
| 54 | FieldValidator& |
| 55 | required(RepoParameterField field) |
| 56 | { |
| 57 | m_required[field] = true; |
| 58 | return *this; |
| 59 | } |
| 60 | |
| 61 | /** \brief declare an optional field |
| 62 | */ |
| 63 | FieldValidator& |
| 64 | optional(RepoParameterField field) |
| 65 | { |
| 66 | m_optional[field] = true; |
| 67 | return *this; |
| 68 | } |
| 69 | |
| 70 | /** \brief verify that all required fields are present, |
| 71 | * and all present fields are either required or optional |
| 72 | * \throw ArgumentError |
| 73 | */ |
| 74 | void |
| 75 | validate(const RepoCommandParameter& parameters) const; |
| 76 | |
| 77 | private: |
| 78 | std::vector<bool> m_required; |
| 79 | std::vector<bool> m_optional; |
| 80 | }; |
| 81 | |
| 82 | void |
| 83 | validateRequest(const RepoCommandParameter& parameters); |
| 84 | |
| 85 | private: |
| 86 | virtual void |
| 87 | check(const RepoCommandParameter& parameters) const |
| 88 | { |
| 89 | } |
| 90 | |
| 91 | public: |
| 92 | FieldValidator m_requestValidator; |
| 93 | }; |
| 94 | |
| 95 | class InsertCommand : public RepoCommand |
| 96 | { |
| 97 | public: |
| 98 | InsertCommand(); |
| 99 | |
| 100 | private: |
| 101 | void |
| 102 | check(const RepoCommandParameter& parameters) const override; |
| 103 | }; |
| 104 | |
| 105 | class InsertCheckCommand : public RepoCommand |
| 106 | { |
| 107 | public: |
| 108 | InsertCheckCommand(); |
| 109 | }; |
| 110 | |
| 111 | class WatchStartCommand : public RepoCommand |
| 112 | { |
| 113 | public: |
| 114 | WatchStartCommand(); |
| 115 | }; |
| 116 | |
| 117 | class WatchCheckCommand : public RepoCommand |
| 118 | { |
| 119 | public: |
| 120 | WatchCheckCommand(); |
| 121 | }; |
| 122 | |
| 123 | class WatchStopCommand : public RepoCommand |
| 124 | { |
| 125 | public: |
| 126 | WatchStopCommand(); |
| 127 | }; |
| 128 | |
| 129 | class DeleteCommand : public RepoCommand |
| 130 | { |
| 131 | public: |
| 132 | DeleteCommand(); |
| 133 | |
| 134 | private: |
| 135 | void |
| 136 | check(const RepoCommandParameter& parameters) const override; |
| 137 | }; |
| 138 | |
| 139 | } // namespace repo |
| 140 | |
| 141 | #endif // REPO_REPO_COMMAND_HPP |