aboutsummaryrefslogtreecommitdiff
path: root/src/injector_storage.cpp
blob: 1e777024f157f2d7bf8bfd5a09c42c0c1e57bb31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
 * Copyright 2014 Google Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define IN_FRUIT_CPP_FILE 1

#include <algorithm>
#include <cstdlib>
#include <fruit/impl/util/type_info.h>
#include <iostream>
#include <memory>
#include <vector>

#include <fruit/impl/component_storage/component_storage.h>
#include <fruit/impl/data_structures/semistatic_graph.templates.h>
#include <fruit/impl/injector/injector_storage.h>
#include <fruit/impl/normalized_component_storage/binding_normalization.h>
#include <fruit/impl/normalized_component_storage/binding_normalization.templates.h>

using std::cout;
using std::endl;

using namespace fruit::impl;

namespace fruit {
namespace impl {

void InjectorStorage::fatal(const std::string& error) {
  std::cerr << "Fatal injection error: " << error << std::endl;
  exit(1);
}

// LCOV_EXCL_START
namespace {
template <typename Id, typename Value>
struct DummyNode {
  Id getId() {
    return Id();
  }
  bool isTerminal() {
    return false;
  }
  Id* getEdgesBegin() {
    return nullptr;
  }
  Id* getEdgesEnd() {
    return nullptr;
  }
  Value getValue() {
    return Value();
  }
};
}
// LCOV_EXCL_STOP

InjectorStorage::InjectorStorage(ComponentStorage&& component,
                                 const std::vector<TypeId, ArenaAllocator<TypeId>>& exposed_types,
                                 MemoryPool& memory_pool)
    : normalized_component_storage_ptr(new NormalizedComponentStorage(
          std::move(component), exposed_types, memory_pool, NormalizedComponentStorage::WithPermanentCompression())),
      allocator(normalized_component_storage_ptr->fixed_size_allocator_data),
      bindings(normalized_component_storage_ptr->bindings, (DummyNode<TypeId, NormalizedBinding>*)nullptr,
               (DummyNode<TypeId, NormalizedBinding>*)nullptr, memory_pool),
      multibindings(std::move(normalized_component_storage_ptr->multibindings)) {

#if FRUIT_EXTRA_DEBUG
  bindings.checkFullyConstructed();
#endif
}

InjectorStorage::InjectorStorage(const NormalizedComponentStorage& normalized_component, ComponentStorage&& component,
                                 MemoryPool& memory_pool) {

  FixedSizeAllocator::FixedSizeAllocatorData fixed_size_allocator_data;
  using new_bindings_vector_t = std::vector<ComponentStorageEntry, ArenaAllocator<ComponentStorageEntry>>;
  new_bindings_vector_t new_bindings_vector = new_bindings_vector_t(ArenaAllocator<ComponentStorageEntry>(memory_pool));

  BindingNormalization::normalizeBindingsAndAddTo(std::move(component).release(), memory_pool, normalized_component,
                                                  fixed_size_allocator_data, new_bindings_vector, multibindings);

  allocator = FixedSizeAllocator(fixed_size_allocator_data);

  bindings = Graph(normalized_component.bindings, BindingDataNodeIter{new_bindings_vector.begin()},
                   BindingDataNodeIter{new_bindings_vector.end()}, memory_pool);
#if FRUIT_EXTRA_DEBUG
  bindings.checkFullyConstructed();
#endif
}

InjectorStorage::~InjectorStorage() {}

void InjectorStorage::ensureConstructedMultibinding(NormalizedMultibindingSet& multibinding_set) {
  for (NormalizedMultibinding& multibinding : multibinding_set.elems) {
    if (!multibinding.is_constructed) {
      multibinding.object = multibinding.create(*this);
      multibinding.is_constructed = true;
    }
  }
}

void* InjectorStorage::getMultibindings(TypeId typeInfo) {
  NormalizedMultibindingSet* multibinding_set = getNormalizedMultibindingSet(typeInfo);
  if (multibinding_set == nullptr) {
    // Not registered.
    return nullptr;
  }
  return multibinding_set->get_multibindings_vector(*this).get();
}

void InjectorStorage::eagerlyInjectMultibindings() {
  std::lock_guard<std::recursive_mutex> lock(mutex);
  for (auto& typeInfoInfoPair : multibindings) {
    typeInfoInfoPair.second.get_multibindings_vector(*this);
  }
}

} // namespace impl
// We need a LCOV_EXCL_BR_LINE below because for some reason gcov/lcov think there's a branch there.
} // namespace fruit LCOV_EXCL_BR_LINE