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