XACC
xacc_service.hpp
1 /*******************************************************************************
2  * Copyright (c) 2019 UT-Battelle, LLC.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * and Eclipse Distribution License v1.0 which accompanies this
6  * distribution. The Eclipse Public License is available at
7  * http://www.eclipse.org/legal/epl-v10.html and the Eclipse Distribution
8  *License is available at https://eclipse.org/org/documents/edl-v10.php
9  *
10  * Contributors:
11  * Alexander J. McCaskey - initial API and implementation
12  *******************************************************************************/
13 #ifndef XACC_SERVICE_SERVICEAPI_HPP_
14 #define XACC_SERVICE_SERVICEAPI_HPP_
15 
16 #include "ServiceRegistry.hpp"
17 #include <memory>
18 
19 namespace xacc {
20 
21 extern std::shared_ptr<ServiceRegistry> serviceRegistry;
22 extern bool serviceAPIInitialized;
23 
24 void ServiceAPI_Initialize(int argc, char **argv);
25 void ServiceAPI_Finalize();
26 
27 void contributeService(const std::string name, ContributableService& service);
28 void contributeService(const std::string name, ContributableService&& service);
29 
30 template <class Service>
31 std::shared_ptr<Service> getService(const std::string &serviceName, bool failIfNotFound = true) {
32  if (!xacc::serviceAPIInitialized) {
33  XACCLogger::instance()->error(
34  "XACC not initialized before use. Please execute "
35  "xacc::Initialize() before using API.");
36  }
37  auto service = serviceRegistry->getService<Service>(serviceName);
38  if (!service && failIfNotFound) {
39  XACCLogger::instance()->error("Invalid XACC Service. Could not find " +
40  serviceName + " in Service Registry.");
41  }
42  return service;
43 }
44 
45 template <typename Service> bool hasService(const std::string &serviceName) {
46  if (!xacc::serviceAPIInitialized) {
47  XACCLogger::instance()->error(
48  "XACC not initialized before use. Please execute "
49  "xacc::Initialize() before using API.");
50  }
51  return serviceRegistry->hasService<Service>(serviceName);
52 }
53 
54 
55 template <class Service>
56 std::shared_ptr<Service> getContributedService(const std::string &serviceName, bool failIfNotFound = true) {
57  if (!xacc::serviceAPIInitialized) {
58  XACCLogger::instance()->error(
59  "XACC not initialized before use. Please execute "
60  "xacc::Initialize() before using API.");
61  }
62  auto service = serviceRegistry->getContributedService<Service>(serviceName);
63  if (!service && failIfNotFound) {
64  XACCLogger::instance()->error("Invalid XACC Service. Could not find " +
65  serviceName + " in Service Registry.");
66  }
67  return service;
68 }
69 
70 template<typename Service>
71 bool hasContributedService(const std::string &serviceName) {
72  if (!xacc::serviceAPIInitialized) {
73  XACCLogger::instance()->error(
74  "XACC not initialized before use. Please execute "
75  "xacc::Initialize() before using API.");
76  }
77  return serviceRegistry->hasContributedService<Service>(serviceName);
78 }
79 
80 template <typename ServiceInterface>
81 std::vector<std::string> getRegisteredIds() {
82  return serviceRegistry->getRegisteredIds<ServiceInterface>();
83 }
84 
85 template <typename ServiceInterface>
86 std::vector<std::shared_ptr<ServiceInterface>> getServices() {
87  return serviceRegistry->getServices<ServiceInterface>();
88 }
89 
90 std::vector<OptionPairs> getRegisteredOptions();
91 bool handleOptions(const std::map<std::string, std::string> &map);
92 
93 std::string getRootPathString();
94 
95 } // namespace xacc
96 
97 #endif
Definition: Accelerator.hpp:25