xref: /aosp_15_r20/external/sandboxed-api/sandboxed_api/var_struct.h (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
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 <memory>
20 #include <string>
21 #include <utility>
22 
23 #include "absl/base/macros.h"
24 #include "absl/strings/str_cat.h"
25 #include "sandboxed_api/var_ptr.h"
26 
27 namespace sapi::v {
28 
29 // Class representing a structure.
30 template <class T>
31 class Struct : public Var {
32  public:
33   // Forwarding constructor to initalize the struct_ field.
34   template <typename... Args>
Struct(Args &&...args)35   explicit Struct(Args&&... args) : struct_(std::forward<Args>(args)...) {
36     SetLocal(&struct_);
37   }
38 
GetSize()39   size_t GetSize() const final { return sizeof(T); }
GetType()40   Type GetType() const final { return Type::kStruct; }
GetTypeString()41   std::string GetTypeString() const final { return "Structure"; }
ToString()42   std::string ToString() const final {
43     return absl::StrCat("Structure of size: ", sizeof(struct_));
44   }
45 
data()46   const T& data() const { return struct_; }
mutable_data()47   T* mutable_data() { return &struct_; }
48 
49  protected:
50   friend class LenVal;
51 
52   T struct_;
53 };
54 
55 }  // namespace sapi::v
56 
57 #endif  // SANDBOXED_API_VAR_STRUCT_H_
58