• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2014 Google Inc. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef FRUIT_NORMALIZED_BINDINGS_H
18 #define FRUIT_NORMALIZED_BINDINGS_H
19 
20 #include <fruit/impl/component_storage/component_storage_entry.h>
21 #include <memory>
22 
23 namespace fruit {
24 namespace impl {
25 
26 /** A single normalized binding (not a multibinding). */
27 struct NormalizedBinding {
28   union {
29     // Valid iff this is a terminal node (in the SemistaticGraph that contains this NormalizedBinding object).
30     ComponentStorageEntry::BindingForConstructedObject::object_ptr_t object;
31 
32     // Valid iff this is not a terminal node  (in the SemistaticGraph that contains this NormalizedBinding object).
33     ComponentStorageEntry::BindingForObjectToConstruct::create_t create;
34   };
35 
36 #if FRUIT_EXTRA_DEBUG
37   bool is_nonconst;
38 #endif
39 
40   NormalizedBinding() = default;
41 
42   // Converts a ComponentStorageEntry to a NormalizedBinding.
43   // This is only supported for entries with these kinds:
44   // * BINDING_FOR_CONSTRUCTED_OBJECT,
45   // * BINDING_FOR_OBJECT_TO_CONSTRUCT_THAT_NEEDS_ALLOCATION,
46   // * BINDING_FOR_OBJECT_TO_CONSTRUCT_THAT_NEEDS_NO_ALLOCATION,
47   explicit NormalizedBinding(ComponentStorageEntry entry);
48 };
49 
50 /** A single normalized multibinding. */
51 struct NormalizedMultibinding {
52 
53   bool is_constructed;
54 
55   union {
56     // Valid iff is_constructed==true.
57     ComponentStorageEntry::MultibindingForConstructedObject::object_ptr_t object;
58 
59     // Valid iff is_constructed==false.
60     ComponentStorageEntry::MultibindingForObjectToConstruct::create_t create;
61   };
62 };
63 
64 /** This stores all multibindings for a given type_id. */
65 struct NormalizedMultibindingSet {
66 
67   // Can be empty, but only if v is present and non-empty.
68   std::vector<NormalizedMultibinding> elems;
69 
70   // TODO: Check this comment.
71   // Returns the std::vector<T*> of instances, or nullptr if none.
72   // Caches the result in the `v' member.
73   ComponentStorageEntry::MultibindingVectorCreator::get_multibindings_vector_t get_multibindings_vector;
74 
75   // A (casted) pointer to the std::vector<T*> of objects, or nullptr if the vector hasn't been constructed yet.
76   // Can't be empty.
77   std::shared_ptr<char> v;
78 };
79 
80 } // namespace impl
81 } // namespace fruit
82 
83 #include <fruit/impl/normalized_component_storage/normalized_bindings.defn.h>
84 
85 #endif // FRUIT_NORMALIZED_BINDINGS_H
86