| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_VALIDATE_PARAMS_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_VALIDATE_PARAMS_H_ |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_VALIDATE_PARAMS_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_VALIDATE_PARAMS_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 | 11 |
| 12 namespace mojo { | 12 namespace mojo { |
| 13 namespace internal { | 13 namespace internal { |
| 14 | 14 |
| 15 using ValidateEnumFunc = bool (*)(int32_t); | 15 using ValidateEnumFunc = bool (*)(int32_t); |
| 16 | 16 |
| 17 // TODO(tibell): Rename to ContainerValidateParams. |
| 17 class ArrayValidateParams { | 18 class ArrayValidateParams { |
| 18 public: | 19 public: |
| 20 // Validates a map. A map is validated as a pair of arrays, one for the keys |
| 21 // and one for the values. Both arguments must be non-null. |
| 22 // |
| 23 // ArrayValidateParams takes ownership of |in_key_validate params| and |
| 24 // |in_element_validate params|. |
| 25 ArrayValidateParams(ArrayValidateParams* in_key_validate_params, |
| 26 ArrayValidateParams* in_element_validate_params) |
| 27 : key_validate_params(in_key_validate_params), |
| 28 element_validate_params(in_element_validate_params) { |
| 29 DCHECK(in_key_validate_params) |
| 30 << "Map validate params require key validate params"; |
| 31 DCHECK(in_element_validate_params) |
| 32 << "Map validate params require element validate params"; |
| 33 } |
| 34 |
| 35 // Validates an array. |
| 36 // |
| 19 // ArrayValidateParams takes ownership of |in_element_validate params|. | 37 // ArrayValidateParams takes ownership of |in_element_validate params|. |
| 20 ArrayValidateParams(uint32_t in_expected_num_elements, | 38 ArrayValidateParams(uint32_t in_expected_num_elements, |
| 21 bool in_element_is_nullable, | 39 bool in_element_is_nullable, |
| 22 ArrayValidateParams* in_element_validate_params) | 40 ArrayValidateParams* in_element_validate_params) |
| 23 : expected_num_elements(in_expected_num_elements), | 41 : expected_num_elements(in_expected_num_elements), |
| 24 element_is_nullable(in_element_is_nullable), | 42 element_is_nullable(in_element_is_nullable), |
| 25 element_validate_params(in_element_validate_params), | 43 element_validate_params(in_element_validate_params) {} |
| 26 validate_enum_func(nullptr) {} | |
| 27 | 44 |
| 28 // Validates an array of enums. | 45 // Validates an array of enums. |
| 29 ArrayValidateParams(uint32_t in_expected_num_elements, | 46 ArrayValidateParams(uint32_t in_expected_num_elements, |
| 30 ValidateEnumFunc in_validate_enum_func) | 47 ValidateEnumFunc in_validate_enum_func) |
| 31 : expected_num_elements(in_expected_num_elements), | 48 : expected_num_elements(in_expected_num_elements), |
| 32 element_is_nullable(false), | |
| 33 element_validate_params(nullptr), | |
| 34 validate_enum_func(in_validate_enum_func) {} | 49 validate_enum_func(in_validate_enum_func) {} |
| 35 | 50 |
| 36 ~ArrayValidateParams() { | 51 ~ArrayValidateParams() { |
| 37 if (element_validate_params) | 52 if (element_validate_params) |
| 38 delete element_validate_params; | 53 delete element_validate_params; |
| 54 if (key_validate_params) |
| 55 delete key_validate_params; |
| 39 } | 56 } |
| 40 | 57 |
| 41 // If |expected_num_elements| is not 0, the array is expected to have exactly | 58 // If |expected_num_elements| is not 0, the array is expected to have exactly |
| 42 // that number of elements. | 59 // that number of elements. |
| 43 uint32_t expected_num_elements; | 60 uint32_t expected_num_elements = 0; |
| 44 | 61 |
| 45 // Whether the elements are nullable. | 62 // Whether the elements are nullable. |
| 46 bool element_is_nullable; | 63 bool element_is_nullable = false; |
| 47 | 64 |
| 48 // Validation information for elements. It is either a pointer to another | 65 // Validation information for the map key array. May contain other |
| 49 // instance of ArrayValidateParams (if elements are arrays or maps), or | 66 // ArrayValidateParams e.g. if the keys are strings. |
| 50 // nullptr. In the case of maps, this is used to validate the value array. | 67 ArrayValidateParams* key_validate_params = nullptr; |
| 51 ArrayValidateParams* element_validate_params; | 68 |
| 69 // For arrays: validation information for elements. It is either a pointer to |
| 70 // another instance of ArrayValidateParams (if elements are arrays or maps), |
| 71 // or nullptr. |
| 72 // |
| 73 // For maps: validation information for the whole value array. May contain |
| 74 // other ArrayValidateParams e.g. if the values are arrays or maps. |
| 75 ArrayValidateParams* element_validate_params = nullptr; |
| 52 | 76 |
| 53 // Validation function for enum elements. | 77 // Validation function for enum elements. |
| 54 ValidateEnumFunc validate_enum_func; | 78 ValidateEnumFunc validate_enum_func = nullptr; |
| 55 | 79 |
| 56 private: | 80 private: |
| 57 DISALLOW_COPY_AND_ASSIGN(ArrayValidateParams); | 81 DISALLOW_COPY_AND_ASSIGN(ArrayValidateParams); |
| 58 }; | 82 }; |
| 59 | 83 |
| 60 } // namespace internal | 84 } // namespace internal |
| 61 } // namespace mojo | 85 } // namespace mojo |
| 62 | 86 |
| 63 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_VALIDATE_PARAMS_H_ | 87 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_VALIDATE_PARAMS_H_ |
| OLD | NEW |