Asylo
shared_name.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2017 Asylo authors
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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  */
18 
19 #ifndef ASYLO_PLATFORM_CORE_SHARED_NAME_H_
20 #define ASYLO_PLATFORM_CORE_SHARED_NAME_H_
21 
22 #include <cstdlib>
23 #include <functional>
24 #include <iostream>
25 #include <string>
26 
27 #include "asylo/platform/common/hash_combine.h"
28 #include "asylo/platform/core/shared_name_kind.h"
29 
30 namespace asylo {
31 
32 /// A name shared between trusted and untrusted code.
33 ///
34 /// A tagged string class representing a name shared between trusted and
35 /// untrusted code.
36 class SharedName {
37  public:
38  /// Constructs an invalid, null SharedName.
39  SharedName() = default;
40 
41  /// Constructs a SharedName.
42  ///
43  /// \param kind The kind of the name's resource domain
44  /// \param name The SharedName's name that should be unique within its kind.
45  SharedName(SharedNameKind kind, const std::string &name)
46  : kind_(kind), name_(name) {}
47 
48  /// The SharedName's kind of resource name domain.
49  ///
50  /// \returns The resource domain of the name.
51  SharedNameKind kind() const { return kind_; }
52 
53  /// The SharedName's name within its kind.
54  ///
55  /// \returns The string value of the name within its domain.
56  const std::string &name() const { return name_; }
57 
58  /// Constructs a SharedName with kind kAddressName.
59  static SharedName Address(const std::string &name) {
60  return SharedName(kAddressName, name);
61  }
62 
63  /// Constructs a SharedName with kind kMemBlockName.
64  static SharedName MemBlock(const std::string &name) {
65  return SharedName(kMemBlockName, name);
66  }
67 
68  /// Constructs a SharedName with kind kSocketName.
69  static SharedName Socket(const std::string &name) {
70  return SharedName(kSocketName, name);
71  }
72 
73  /// Constructs a SharedName with kind kTimerName.
74  static SharedName Timer(const std::string &name) {
75  return SharedName(kTimerName, name);
76  }
77 
78  struct Hash : std::unary_function<SharedName, size_t> {
79  size_t operator()(const SharedName &name) const {
80  return HashCombine<std::string>(std::hash<int>()(name.kind_), name.name_);
81  }
82  };
83 
84  struct Eq : std::binary_function<SharedName, SharedName, bool> {
85  bool operator()(const SharedName &lhs, const SharedName &rhs) const {
86  return (lhs.kind_ == rhs.kind_) && (lhs.name_ == rhs.name_);
87  }
88  };
89 
90  private:
91  SharedNameKind kind_;
92  std::string name_;
93 };
94 
95 inline std::ostream &operator<<(std::ostream &os, const SharedName &name) {
96  switch (name.kind()) {
97  case kUnspecifiedName:
98  os << "kUnspecifiedName";
99  break;
100  case kAddressName:
101  os << "kAddressName";
102  break;
103  case kSocketName:
104  os << "kSocketName";
105  break;
106  case kTimerName:
107  os << "kTimerName";
108  break;
109  case kMemBlockName:
110  os << "kMemBlockName";
111  break;
112  default:
113  abort();
114  }
115  return os << "::" << name.name();
116 }
117 
118 } // namespace asylo
119 
120 #endif // ASYLO_PLATFORM_CORE_SHARED_NAME_H_