这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 25 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,13 @@ using IFizzPtr = std::unique_ptr<IFizz>;
```cpp

namespace symbols {
inline const inversify::Symbol foo { "Foo" };
inline const inversify::Symbol bar { "Bar" };
inline const inversify::Symbol fizz { "Fizz" };
inline const inversify::Symbol fizzFactory { "FizzFactory" };

inline const inversify::Symbol autoFizz { "AutoFizz" };
inline const inversify::Symbol autoFizzUnique { "AutoFizzUnique" };
inline const inversify::Symbol autoFizzShared { "AutoFizzShared" };
inline extern inversify::Symbol<int> foo {};
inline extern inversify::Symbol<double> bar {};
inline extern inversify::Symbol<IFizzPtr> fizz {};
inline extern inversify::Symbol<std::function<IFizzPtr()>> fizzFactory {};

inline extern inversify::Symbol<IFizzUniquePtr> autoFizzUnique {};
inline extern inversify::Symbol<IFizzSharedPtr> autoFizzShared {};
}

```
Expand Down Expand Up @@ -97,8 +96,8 @@ struct Fizz : IFizz {
};

inline static auto injectFizz = inversify::Injectable<Fizz>::inject(
inversify::Inject<int>(symbols::foo),
inversify::Inject<double>(symbols::bar)
symbols::foo,
symbols::bar
);

```
Expand All @@ -107,7 +106,7 @@ inline static auto injectFizz = inversify::Injectable<Fizz>::inject(

```cpp

inversify::Container container {};
inversify::Container container;

```

Expand All @@ -117,8 +116,8 @@ Constant bindings are always singletons.

```cpp

container.bind<int>(symbols::foo).toConstantValue(10);
container.bind<double>(symbols::bar).toConstantValue(1.618);
container.bind(symbols::foo).toConstantValue(10);
container.bind(symbols::bar).toConstantValue(1.618);

```

Expand All @@ -132,10 +131,10 @@ Singleton scope dynamic bindings cache the first resolution of the binding.

```cpp

container.bind<IFizzPtr>(symbols::fizz).toDynamicValue(
container.bind(symbols::fizz).toDynamicValue(
[](const inversify::Context& ctx) {
auto foo = ctx.container.get<int>(symbols::foo);
auto bar = ctx.container.get<double>(symbols::bar);
auto foo = ctx.container.get(symbols::foo);
auto bar = ctx.container.get(symbols::bar);

auto fizz = std::make_shared<Fizz>(foo, bar);

Expand All @@ -151,11 +150,11 @@ Dynamic bindings can be used to resolve factory functions.

```cpp

container.bind<std::function<IFizzPtr()>>(symbols::fizzFactory).toDynamicValue(
container.bind(symbols::fizzFactory).toDynamicValue(
[](const inversify::Context& ctx) {
return [&]() {
auto foo = ctx.container.get<int>(symbols::foo);
auto bar = ctx.container.get<double>(symbols::bar);
auto foo = ctx.container.get(symbols::foo);
auto bar = ctx.container.get(symbols::bar);

auto fizz = std::make_shared<Fizz>(foo, bar);

Expand All @@ -174,33 +173,29 @@ Automatic bindings can generate instances, unique_ptr's, and shared_ptr's of a c

```cpp

container.bind<Fizz>(symbols::autoFizz).to<Fizz>();
container.bind<IFizzUniquePtr>(symbols::autoFizzUnique).to<Fizz>();
container.bind<IFizzSharedPtr>(symbols::autoFizzShared).to<Fizz>().inSingletonScope();
container.bind(symbols::autoFizzUnique).to<Fizz>();
container.bind(symbols::autoFizzShared).to<Fizz>().inSingletonScope();

```

#### Resolving Dependencies

```cpp

auto bar = container.get<double>(symbols::bar);
auto bar = container.get(symbols::bar);

auto fizz = container.get<IFizzPtr>(symbols::fizz);
auto fizz = container.get(symbols::fizz);
fizz->buzz();

auto fizzFactory = container.get<std::function<IFizzPtr()>>(symbols::fizzFactory);
auto fizzFactory = container.get(symbols::fizzFactory);
auto anotherFizz = fizzFactory();
anotherFizz->buzz();


auto autoFizz = container.get<Fizz>(symbols::autoFizz);
autoFizz.buzz();

auto autoFizzUnique = container.get<IFizzUniquePtr>(symbols::autoFizzUnique);
auto autoFizzUnique = container.get(symbols::autoFizzUnique);
autoFizzUnique->buzz();

auto autoFizzShared = container.get<IFizzSharedPtr>(symbols::autoFizzShared);
auto autoFizzShared = container.get(symbols::autoFizzShared);
autoFizzShared->buzz();

```
Expand Down
10 changes: 7 additions & 3 deletions example/api/symbols.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

#include <mosure/inversify.hpp>

#include <api/logger.hpp>
#include <api/service.hpp>
#include <api/settings.hpp>


namespace inversify = mosure::inversify;
namespace symbols {
inline const inversify::Symbol logger { "Logger" };
inline const inversify::Symbol service { "Service" };
inline const inversify::Symbol settings { "Settings" };
inline extern inversify::Symbol<ILoggerPtr> logger {};
inline extern inversify::Symbol<IServicePtr> service {};
inline extern inversify::Symbol<ISettings> settings {};
}
4 changes: 1 addition & 3 deletions example/src/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,4 @@ class Logger : public ILogger {
};

namespace inversify = mosure::inversify;
inline static auto injectLogger = inversify::Injectable<Logger>::inject(
inversify::Inject<ISettings>(symbols::settings)
);
inline static auto injectLogger = inversify::Injectable<Logger>::inject(symbols::settings);
10 changes: 5 additions & 5 deletions example/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
namespace inversify = mosure::inversify;

int main() {
inversify::Container container {};
inversify::Container container;

container.bind<ILoggerPtr>(symbols::logger).to<Logger>().inSingletonScope();
container.bind<IServicePtr>(symbols::service).to<Service>();
container.bind<ISettings>(symbols::settings).to<Settings>().inSingletonScope();
container.bind(symbols::logger).to<Logger>().inSingletonScope();
container.bind(symbols::service).to<Service>();
container.bind(symbols::settings).to<Settings>().inSingletonScope();

//container.bind<ILoggerPtr>(symbols::logger).to<MockLogger>().inSingletonScope();

container.get<IServicePtr>(symbols::service)->run();
container.get(symbols::service)->run();

return 0;
}
4 changes: 1 addition & 3 deletions example/src/service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,4 @@ class Service : public IService {
};

namespace inversify = mosure::inversify;
inline static auto injectService = inversify::Injectable<Service>::inject(
inversify::Inject<ILoggerPtr>(symbols::logger)
);
inline static auto injectService = inversify::Injectable<Service>::inject(symbols::logger);
6 changes: 3 additions & 3 deletions include/mosure/binding.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,21 @@ namespace mosure::inversify {
template <typename T>
class Binding : public BindingTo<T> {
public:
explicit Binding(const inversify::Symbol& symbol)
explicit Binding(const inversify::Symbol<T>& symbol)
:
symbol_(symbol)
{ }

T resolve(const Context& context) const {
if (!this->resolver_) {
throw inversify::exceptions::ResolutionException("inversify::Resolver not found. Malformed binding: " + symbol_);
throw inversify::exceptions::ResolutionException("inversify::Resolver not found. Malformed binding.");
}

return this->resolver_->resolve(context);
}

private:
const inversify::Symbol& symbol_;
const inversify::Symbol<T>& symbol_;
};

}
22 changes: 11 additions & 11 deletions include/mosure/container.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,36 @@ namespace mosure::inversify {
class Container : public inversify::IContainer<Container> {
public:
template <typename T>
inversify::BindingTo<T>& bind(const inversify::Symbol& type) {
inversify::BindingTo<T>& bind(const inversify::Symbol<T>& type) {
static_assert(!std::is_abstract<T>(), "inversify::Container cannot bind/get abstract class value (use a smart pointer instead).");

auto binding = inversify::Binding<T>(type);
auto binding = inversify::Binding(type);

auto lookup = bindings_.find(type);
auto lookup = bindings_.find(type.id);
if (lookup != bindings_.end()) {
bindings_.erase(type);
bindings_.erase(type.id);
}

auto pair = std::make_pair(type, std::any(binding));
auto pair = std::make_pair(type.id, std::any(binding));
bindings_.insert(pair);

return std::any_cast<inversify::Binding<T>&>(bindings_.at(type));
return std::any_cast<inversify::Binding<T>&>(bindings_.at(type.id));
}

template <typename T>
T get(const inversify::Symbol& type) const {
auto symbolBinding = bindings_.find(type);
T get(const inversify::Symbol<T>& type) const {
auto symbolBinding = bindings_.find(type.id);
if (symbolBinding == bindings_.end()) {
throw inversify::exceptions::SymbolException(type);
throw inversify::exceptions::SymbolException();
}

auto binding = std::any_cast<Binding<T>>(symbolBinding->second);
auto binding = std::any_cast<inversify::Binding<T>>(symbolBinding->second);

return binding.resolve(context_);
}

private:
std::unordered_map<inversify::Symbol, std::any> bindings_ { };
std::unordered_map<int, std::any> bindings_ { };
inversify::Context context_ { *this };
};

Expand Down
4 changes: 1 addition & 3 deletions include/mosure/exceptions/symbol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

#include <stdexcept>

#include <mosure/symbol.hpp>


namespace mosure::inversify::exceptions {

struct SymbolException : public std::runtime_error {
explicit SymbolException(const inversify::Symbol& symbol) : std::runtime_error("inversify::Symbol not found: " + symbol) { }
explicit SymbolException() : std::runtime_error("inversify::Symbol not found.") { }
};

}
23 changes: 5 additions & 18 deletions include/mosure/injectable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,9 @@

namespace mosure::inversify {

struct InjectBase {
explicit InjectBase(const inversify::Symbol& symbol) : symbol(symbol) { }

inversify::Symbol symbol;
};

template <typename Interface>
struct Inject : InjectBase {
explicit Inject(inversify::Symbol symbol) : InjectBase(symbol) { }

using value = Interface;
};

template <typename ...Types>
inline constexpr bool valid_inject_types_v = std::conjunction_v<
is_specialization<Types, Inject>...
meta::is_specialization<Types, Symbol>...
>;

template <typename Implementation>
Expand All @@ -39,7 +26,7 @@ namespace mosure::inversify {

template <typename... Dependencies>
inline static Injectable inject(Dependencies... dependencies) {
static_assert(valid_inject_types_v<Dependencies...>, "inversify::Injectable dependencies must be of type inversify::Inject");
static_assert(valid_inject_types_v<Dependencies...>, "inversify::Injectable dependencies must be of type inversify::Symbol");

factory = [
deps = std::make_tuple(dependencies...)
Expand Down Expand Up @@ -79,10 +66,10 @@ namespace mosure::inversify {
private:
template <typename Dependency>
inline static typename Dependency::value resolve_dependency(const inversify::Context& context, Dependency dep) {
auto symbol = static_cast<InjectBase>(dep).symbol;

using Interface = typename Dependency::value;
return context.container.template get<Interface>(symbol);
auto symbol = static_cast<Symbol<Interface>>(dep);

return context.container.get(symbol);
}
};

Expand Down
8 changes: 4 additions & 4 deletions include/mosure/interfaces/icontainer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ namespace mosure::inversify {
class IContainer {
public:
template <typename T>
inversify::BindingTo<T>& bind(const inversify::Symbol& type) {
inversify::BindingTo<T>& bind(const inversify::Symbol<T>& type) {
auto crtpImplementation = static_cast<Implementation const *>(this);

return crtpImplementation->template bind<T>(type);
return crtpImplementation->bind(type);
}

template <typename T>
T get(const inversify::Symbol& type) const {
T get(const inversify::Symbol<T>& type) const {
auto crtpImplementation = static_cast<Implementation const *>(this);

return crtpImplementation->template get<T>(type);
return crtpImplementation->get(type);
}
};

Expand Down
3 changes: 2 additions & 1 deletion include/mosure/meta.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#pragma once

#include <type_traits>
#include <utility>


namespace mosure::inversify {
namespace mosure::inversify::meta {

template <class T, template <class...> class Template>
struct is_specialization : std::false_type { };
Expand Down
4 changes: 2 additions & 2 deletions include/mosure/resolver.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <atomic>
#include <memory>
#include <type_traits>

Expand Down Expand Up @@ -101,7 +102,6 @@ namespace mosure::inversify {
explicit CachedResolver(ResolverPtr<T> parent) : parent_(parent) { }

T resolve(const inversify::Context& context) override {
// TODO: add lock for multi-thread support
if (!hasCached_) {
hasCached_ = true;
cached_ = parent_->resolve(context);
Expand All @@ -112,7 +112,7 @@ namespace mosure::inversify {

private:
T cached_;
bool hasCached_ { false };
std::atomic<bool> hasCached_ { false };
ResolverPtr<T> parent_;
};

Expand Down
14 changes: 12 additions & 2 deletions include/mosure/symbol.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
#pragma once

#include <string>
#include <iostream>


namespace mosure::inversify {

using Symbol = std::string;
namespace {
inline static int counter = 0;
}

template <typename Interface>
struct Symbol {
Symbol() : id(counter++) { }

const int id;
using value = Interface;
};

}
Loading