winget-cli

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

ArgumentValidation.cpp (8529B)


      1 // Copyright (c) Microsoft Corporation.
      2 // Licensed under the MIT License.
      3 #include <pch.h>
      4 #include "ArgumentValidation.h"
      5 
      6 namespace winrt::Microsoft::Management::Configuration::implementation
      7 {
      8     void EnsureSupportedType(Windows::Foundation::PropertyType type)
      9     {
     10         switch (type)
     11         {
     12         case Windows::Foundation::PropertyType::UInt8:
     13         case Windows::Foundation::PropertyType::Int16:
     14         case Windows::Foundation::PropertyType::UInt16:
     15         case Windows::Foundation::PropertyType::Int32:
     16         case Windows::Foundation::PropertyType::UInt32:
     17         case Windows::Foundation::PropertyType::Int64:
     18         case Windows::Foundation::PropertyType::UInt64:
     19         case Windows::Foundation::PropertyType::Single:
     20         case Windows::Foundation::PropertyType::Double:
     21         case Windows::Foundation::PropertyType::Char16:
     22         case Windows::Foundation::PropertyType::Boolean:
     23         case Windows::Foundation::PropertyType::String:
     24         case Windows::Foundation::PropertyType::Inspectable:
     25         case Windows::Foundation::PropertyType::DateTime:
     26         case Windows::Foundation::PropertyType::TimeSpan:
     27         case Windows::Foundation::PropertyType::Guid:
     28         case Windows::Foundation::PropertyType::UInt8Array:
     29         case Windows::Foundation::PropertyType::Int16Array:
     30         case Windows::Foundation::PropertyType::UInt16Array:
     31         case Windows::Foundation::PropertyType::Int32Array:
     32         case Windows::Foundation::PropertyType::UInt32Array:
     33         case Windows::Foundation::PropertyType::Int64Array:
     34         case Windows::Foundation::PropertyType::UInt64Array:
     35         case Windows::Foundation::PropertyType::SingleArray:
     36         case Windows::Foundation::PropertyType::DoubleArray:
     37         case Windows::Foundation::PropertyType::Char16Array:
     38         case Windows::Foundation::PropertyType::BooleanArray:
     39         case Windows::Foundation::PropertyType::StringArray:
     40         case Windows::Foundation::PropertyType::InspectableArray:
     41         case Windows::Foundation::PropertyType::DateTimeArray:
     42         case Windows::Foundation::PropertyType::TimeSpanArray:
     43         case Windows::Foundation::PropertyType::GuidArray:
     44             return;
     45         }
     46 
     47         THROW_HR(E_INVALIDARG);
     48     }
     49 
     50     bool IsValidObjectType(Windows::Foundation::IInspectable const& value, Windows::Foundation::PropertyType type)
     51     {
     52         auto propertyValue = value.try_as<Windows::Foundation::IPropertyValue>();
     53 
     54         // If the type is an object, it is acceptable for the value to be a ValueSet directly
     55         if (type == Windows::Foundation::PropertyType::Inspectable &&
     56             (propertyValue || value.try_as<Windows::Foundation::Collections::ValueSet>()))
     57         {
     58             return true;
     59         }
     60 
     61         // If it wasn't an object type and a ValueSet, it must be an IPropertyValue
     62         if (!propertyValue)
     63         {
     64             return false;
     65         }
     66 
     67         // If it is an IPropertyValue, it must have the required type
     68         return (propertyValue.Type() == type);
     69     }
     70 
     71     void EnsureObjectType(Windows::Foundation::IInspectable const& value, Windows::Foundation::PropertyType type)
     72     {
     73         THROW_HR_IF(E_INVALIDARG, !IsValidObjectType(value, type));
     74     }
     75 
     76     bool IsComparableType(Windows::Foundation::PropertyType type)
     77     {
     78         switch (type)
     79         {
     80         case Windows::Foundation::PropertyType::UInt8:
     81         case Windows::Foundation::PropertyType::Int16:
     82         case Windows::Foundation::PropertyType::UInt16:
     83         case Windows::Foundation::PropertyType::Int32:
     84         case Windows::Foundation::PropertyType::UInt32:
     85         case Windows::Foundation::PropertyType::Int64:
     86         case Windows::Foundation::PropertyType::UInt64:
     87         case Windows::Foundation::PropertyType::Single:
     88         case Windows::Foundation::PropertyType::Double:
     89         case Windows::Foundation::PropertyType::Char16:
     90         case Windows::Foundation::PropertyType::DateTime:
     91         case Windows::Foundation::PropertyType::TimeSpan:
     92             return true;
     93         }
     94 
     95         return false;
     96     }
     97 
     98     void EnsureComparableType(Windows::Foundation::PropertyType type)
     99     {
    100         THROW_HR_IF(E_INVALIDARG, !IsComparableType(type));
    101     }
    102 
    103     bool IsLengthType(Windows::Foundation::PropertyType type)
    104     {
    105         switch (type)
    106         {
    107         case Windows::Foundation::PropertyType::String:
    108         case Windows::Foundation::PropertyType::UInt8Array:
    109         case Windows::Foundation::PropertyType::Int16Array:
    110         case Windows::Foundation::PropertyType::UInt16Array:
    111         case Windows::Foundation::PropertyType::Int32Array:
    112         case Windows::Foundation::PropertyType::UInt32Array:
    113         case Windows::Foundation::PropertyType::Int64Array:
    114         case Windows::Foundation::PropertyType::UInt64Array:
    115         case Windows::Foundation::PropertyType::SingleArray:
    116         case Windows::Foundation::PropertyType::DoubleArray:
    117         case Windows::Foundation::PropertyType::Char16Array:
    118         case Windows::Foundation::PropertyType::BooleanArray:
    119         case Windows::Foundation::PropertyType::StringArray:
    120         case Windows::Foundation::PropertyType::InspectableArray:
    121         case Windows::Foundation::PropertyType::DateTimeArray:
    122         case Windows::Foundation::PropertyType::TimeSpanArray:
    123         case Windows::Foundation::PropertyType::GuidArray:
    124         case Windows::Foundation::PropertyType::PointArray:
    125         case Windows::Foundation::PropertyType::SizeArray:
    126         case Windows::Foundation::PropertyType::RectArray:
    127         case Windows::Foundation::PropertyType::OtherTypeArray:
    128             return true;
    129         }
    130 
    131         return false;
    132     }
    133 
    134     void EnsureLengthType(Windows::Foundation::PropertyType type)
    135     {
    136         THROW_HR_IF(E_INVALIDARG, !IsLengthType(type));
    137     }
    138 
    139     bool IsStringableType(Windows::Foundation::PropertyType type)
    140     {
    141         switch (type)
    142         {
    143         case Windows::Foundation::PropertyType::UInt8:
    144         case Windows::Foundation::PropertyType::Int16:
    145         case Windows::Foundation::PropertyType::UInt16:
    146         case Windows::Foundation::PropertyType::Int32:
    147         case Windows::Foundation::PropertyType::UInt32:
    148         case Windows::Foundation::PropertyType::Int64:
    149         case Windows::Foundation::PropertyType::UInt64:
    150         case Windows::Foundation::PropertyType::Single:
    151         case Windows::Foundation::PropertyType::Double:
    152         case Windows::Foundation::PropertyType::Char16:
    153         case Windows::Foundation::PropertyType::Boolean:
    154         case Windows::Foundation::PropertyType::String:
    155             return true;
    156         }
    157 
    158         return false;
    159     }
    160 
    161     hstring ToString(Windows::Foundation::IPropertyValue value)
    162     {
    163         Windows::Foundation::PropertyType type = value.Type();
    164         if (type == Windows::Foundation::PropertyType::String)
    165         {
    166             return value.GetString();
    167         }
    168 
    169         std::wostringstream stream;
    170 
    171         switch (value.Type())
    172         {
    173         case Windows::Foundation::PropertyType::UInt8:
    174             stream << value.GetUInt8();
    175             break;
    176         case Windows::Foundation::PropertyType::Int16:
    177             stream << value.GetInt16();
    178             break;
    179         case Windows::Foundation::PropertyType::UInt16:
    180             stream << value.GetUInt16();
    181             break;
    182         case Windows::Foundation::PropertyType::Int32:
    183             stream << value.GetInt32();
    184             break;
    185         case Windows::Foundation::PropertyType::UInt32:
    186             stream << value.GetUInt32();
    187             break;
    188         case Windows::Foundation::PropertyType::Int64:
    189             stream << value.GetInt64();
    190             break;
    191         case Windows::Foundation::PropertyType::UInt64:
    192             stream << value.GetUInt64();
    193             break;
    194         case Windows::Foundation::PropertyType::Single:
    195             stream << value.GetSingle();
    196             break;
    197         case Windows::Foundation::PropertyType::Double:
    198             stream << value.GetDouble();
    199             break;
    200         case Windows::Foundation::PropertyType::Char16:
    201             stream << value.GetChar16();
    202             break;
    203         case Windows::Foundation::PropertyType::Boolean:
    204             stream << value.GetBoolean() ? L"true" : L"false";
    205             break;
    206         }
    207 
    208         return hstring{ stream.str() };
    209     }
    210 }