Apache Mesos
module.hpp
Go to the documentation of this file.
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 #ifndef __TESTS_MODULE_HPP__
18 #define __TESTS_MODULE_HPP__
19 
20 #include <string>
21 
22 #include <mesos/module/module.hpp>
23 
24 #include <stout/try.hpp>
25 
26 #include "logging/flags.hpp"
27 #include "messages/messages.hpp"
28 #include "module/manager.hpp"
29 
30 #include "tests/mesos.hpp"
31 
32 namespace mesos {
33 namespace internal {
34 namespace tests {
35 
36 // The ModuleID is used by typed tests to specify the specific module
37 // name for the test instance. Ideally, we would have passed the
38 // module name itself, but templates do not allow string literals as
39 // template parameters.
41 {
56 };
57 
58 
60 
62 
63 
64 template <typename T, ModuleID N>
65 class Module
66 {
67 public:
68  // Create is used by the type_param'ed tests. T here denotes the
69  // module type, whereas N denotes the module name.
70  static Try<T*> create()
71  {
72  Try<std::string> moduleName = getModuleName(N);
73  if (moduleName.isError()) {
74  return Error(moduleName.error());
75  }
76  return mesos::modules::ModuleManager::create<T>(moduleName.get());
77  }
78 
79  // Create is used by the type_param'ed tests. T here denotes the
80  // module type, whereas N denotes the module name.
81  static Try<T*> create(const Parameters& parameters)
82  {
83  Try<std::string> moduleName = getModuleName(N);
84  if (moduleName.isError()) {
85  return Error(moduleName.error());
86  }
87  return mesos::modules::ModuleManager::create<T>(
88  moduleName.get(),
89  parameters);
90  }
91 
93  {
94  return create();
95  }
96 };
97 
98 } // namespace tests {
99 } // namespace internal {
100 } // namespace mesos {
101 
102 namespace std {
103 
104 template <>
106 {
107  typedef size_t result_type;
108 
110 
111  result_type operator()(const argument_type& moduleId) const
112  {
113  // Use the underlying type of the enum as hash value.
114  return static_cast<size_t>(moduleId);
115  }
116 };
117 
118 } // namespace std {
119 
120 #endif // __TESTS_MODULE_HPP__
static Try< T * > create(const Parameters &parameters)
Definition: module.hpp:81
result_type operator()(const argument_type &moduleId) const
Definition: module.hpp:111
Definition: errorbase.hpp:36
Try< Nothing > initModules(const Option< Modules > &modules)
T & get()&
Definition: try.hpp:80
Definition: flags.hpp:29
Definition: check.hpp:33
Definition: module.hpp:46
Definition: type_utils.hpp:619
size_t result_type
Definition: module.hpp:107
mesos::internal::tests::ModuleID argument_type
Definition: module.hpp:109
ModuleID
Definition: module.hpp:40
Definition: module.hpp:65
Definition: agent.hpp:25
static Try< T * > create()
Definition: module.hpp:70
static Try error(const E &e)
Definition: try.hpp:43
Definition: attributes.hpp:24
bool isError() const
Definition: try.hpp:78
Try< std::string > getModuleName(ModuleID id)
Definition: module.hpp:47
static Try< T * > create(const logging::Flags &flags)
Definition: module.hpp:92
Definition: parse.hpp:33