• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef SANDBOXED_API_VAR_STRUCT_H_
16 #define SANDBOXED_API_VAR_STRUCT_H_
17 
18 #include <cstddef>
19 #include <string>
20 #include <utility>
21 
22 #include "absl/strings/str_cat.h"
23 #include "sandboxed_api/var_abstract.h"
24 #include "sandboxed_api/var_type.h"
25 
26 namespace sapi::v {
27 
28 // Class representing a structure.
29 template <class T>
30 class Struct : public Var {
31  public:
32   // Forwarding constructor to initialize the struct_ field.
33   template <typename... Args>
Struct(Args &&...args)34   explicit Struct(Args&&... args) : struct_(std::forward<Args>(args)...) {
35     SetLocal(&struct_);
36   }
37 
38   Struct(Struct&& other) = default;
39   Struct& operator=(Struct&& other) = default;
40 
GetSize()41   size_t GetSize() const final { return sizeof(T); }
GetType()42   Type GetType() const final { return Type::kStruct; }
GetTypeString()43   std::string GetTypeString() const final { return "Structure"; }
ToString()44   std::string ToString() const final {
45     return absl::StrCat("Structure of size: ", sizeof(struct_));
46   }
47 
data()48   const T& data() const { return struct_; }
mutable_data()49   T* mutable_data() { return &struct_; }
50 
51  protected:
52   friend class LenVal;
53 
54   T struct_;
55 };
56 
57 }  // namespace sapi::v
58 
59 #endif  // SANDBOXED_API_VAR_STRUCT_H_
60