File: weak.hpp

package info (click to toggle)
vart 2.5-5
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 4,404 kB
  • sloc: cpp: 30,188; python: 7,493; sh: 969; makefile: 37; ansic: 36
file content (108 lines) | stat: -rw-r--r-- 3,450 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright 2019 Xilinx Inc.
 *
 * 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.
 */
#pragma once
#include <assert.h>
#include <iostream>
#include <memory>
#include <unordered_map>
namespace vitis {
namespace ai {
template <typename T>
struct WeakSingleton {
  static std::weak_ptr<T> the_instance_;
  template <typename... Args>
  static std::shared_ptr<T> create(Args&&... args) {
    std::shared_ptr<T> ret;
    if (the_instance_.expired()) {
      ret = std::make_shared<T>(std::forward<Args>(args)...);
      the_instance_ = ret;
    }
    ret = the_instance_.lock();
    assert(ret != nullptr);
    return ret;
  }
};
template <typename T>
std::weak_ptr<T> WeakSingleton<T>::the_instance_;

// we don't support c++17 yet.
template <class...>
using my_void_t = void;
template <typename T, class = void>
struct invoke_initialize_if_possible {
  static void initialize(T* t) {}
};
template <typename T>
struct WithInjection;
template <typename T>
using is_derived_from_with_injection =
    typename std::enable_if<std::is_base_of<WithInjection<T>, T>::value>::type;

template <typename T>
using is_not_derived_from_with_injection =
    typename std::enable_if<!std::is_base_of<WithInjection<T>, T>::value>::type;

template <typename T>
struct invoke_initialize_if_possible<T, is_derived_from_with_injection<T>> {
  static void initialize(T* t) {
    // with_injection<T>::create(...) invokes initialize() already,
    // void invoke it twice;
  }
};

template <typename T>
struct invoke_initialize_if_possible<
    T, my_void_t<decltype(std::declval<T>().initialize()),
                 // see comment above, otherwise, ambigurous template defined.
                 is_not_derived_from_with_injection<T>>> {
  static void initialize(T* t) { t->initialize(); }
};

template <typename K, typename T>
struct WeakStore {
  static std::unordered_map<K, std::weak_ptr<T>> the_store_;
  template <typename... Args>
  static std::shared_ptr<T> create(const K& key, Args&&... args) {
    std::shared_ptr<T> ret;
    if (the_store_[key].expired()) {
      ret = create_1(std::forward<Args>(args)...);
      invoke_initialize_if_possible<T>::initialize(ret.get());
      the_store_[key] = ret;
    }
    ret = the_store_[key].lock();
    assert(ret != nullptr);
    return ret;
  }

 private:
  template <typename... Args>
  static typename std::enable_if<!std::is_constructible<T, Args...>::value,
                                 std::shared_ptr<T>>::type
  create_1(Args&&... args) {
    return T::create(std::forward<Args>(args)...);
  }
  template <typename... Args>
  static typename std::enable_if<std::is_constructible<T, Args...>::value,
                                 std::shared_ptr<T>>::type
  create_1(Args&&... args) {
    return std::make_shared<T>(std::forward<Args>(args)...);
  }
};
template <typename K, typename T>
std::unordered_map<K, std::weak_ptr<T>> WeakStore<K, T>::the_store_;

}  // namespace ai
}  // namespace vitis