winget-cli

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

Search.h (3484B)


      1 // Copyright (c) Microsoft Corporation.
      2 // Licensed under the MIT License.
      3 #pragma once
      4 #include <memory>
      5 #include <type_traits>
      6 #include <winrt/base.h>
      7 
      8 // TODO: This code is expected to eventually be placed into its own DLL to support the CLI
      9 //       and OOP COM server for use by OS integration points.
     10 //       For now we will just maintain the ABI with helper C++ wrappers for client use.
     11 
     12 // "C" ABI
     13 
     14 using AICLIString = wchar_t*;
     15 using AICLICString = wchar_t const*;
     16 using RepositoryHandle = void*;
     17 using ApplicationHandle = void*;
     18 using ManifestHandle = void*;
     19 
     20 winrt::hresult aicliGetApplicationById(RepositoryHandle repo, AICLICString id, ApplicationHandle* app);
     21 
     22 winrt::hresult aicliGetManifestByVersion(ApplicationHandle app, AICLICString version, ManifestHandle* man);
     23 
     24 winrt::hresult aicliGetManifestContents(ManifestHandle man, AICLICString* contents);
     25 
     26 void aicliFreeRepository(RepositoryHandle repo);
     27 void aicliFreeApplication(ApplicationHandle app);
     28 void aicliFreeManifest(ManifestHandle manifest);
     29 
     30 // C++ wrapper
     31 
     32 namespace AppInstaller::CLI
     33 {
     34     namespace details
     35     {
     36         struct RepositoryDeleter
     37         {
     38             void operator()(RepositoryHandle repo) { aicliFreeRepository(repo); }
     39         };
     40 
     41         struct ApplicationDeleter
     42         {
     43             void operator()(ApplicationHandle app) { aicliFreeApplication(app); }
     44         };
     45 
     46         struct ManifestDeleter
     47         {
     48             void operator()(ManifestHandle manifest) { aicliFreeManifest(manifest); }
     49         };
     50 
     51         using unique_repository = std::unique_ptr<std::remove_pointer_t<RepositoryHandle>, RepositoryDeleter>;
     52         using unique_application = std::unique_ptr<std::remove_pointer_t<ApplicationHandle>, ApplicationDeleter>;
     53         using unique_manifest = std::unique_ptr<std::remove_pointer_t<ManifestHandle>, ManifestDeleter>;
     54 
     55         template <typename Result, typename Func, typename... Args>
     56         Result CallABIFunc(Func&& f, Args&&... args)
     57         {
     58             Result result{};
     59             winrt::check_hresult(f(std::forward(args)..., &result));
     60             return result;
     61         }
     62     }
     63 
     64     struct Manifest
     65     {
     66         Manifest() = default;
     67         Manifest(ManifestHandle mh) : m_man(mh) {}
     68         ~Manifest() = default;
     69 
     70         Manifest(const Manifest&) = delete;
     71         Manifest& operator=(const Manifest&) = delete;
     72 
     73         Manifest(Manifest&&) = default;
     74         Manifest& operator=(Manifest&&) = default;
     75 
     76         AICLICString GetManifestContents() const { return details::CallABIFunc<AICLICString>(aicliGetManifestContents, m_man.get()); }
     77 
     78     private:
     79         details::unique_manifest m_man;
     80     };
     81 
     82     struct Application
     83     {
     84         Application() = default;
     85         Application(ApplicationHandle ah) : m_app(ah) {}
     86         ~Application() = default;
     87 
     88         Application(const Application&) = delete;
     89         Application& operator=(const Application&) = delete;
     90 
     91         Application(Application&&) = default;
     92         Application& operator=(Application&&) = default;
     93 
     94         static Application GetById(AICLICString id) { return details::CallABIFunc<ApplicationHandle>(aicliGetApplicationById, nullptr, id); }
     95 
     96         Manifest GetManifestByVersion(AICLICString version) const { return details::CallABIFunc<ManifestHandle>(aicliGetManifestByVersion, m_app.get(), version); }
     97 
     98     private:
     99         details::unique_application m_app;
    100     };
    101 }