Skip to content

Commit

Permalink
Add an example that shows how to use annotated injection.
Browse files Browse the repository at this point in the history
  • Loading branch information
poletti-marco committed May 15, 2016
1 parent 2f36ab0 commit 695a93f
Show file tree
Hide file tree
Showing 11 changed files with 273 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ add_subdirectory(hello_world)
add_subdirectory(server)
add_subdirectory(multibindings)
add_subdirectory(scaling_doubles)
add_subdirectory(annotated_injection)
10 changes: 10 additions & 0 deletions examples/annotated_injection/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@


cc_binary(
name = "annotated_injection",
srcs = glob([
"*.cpp",
"*.h",
]),
deps = ["//:fruit"],
)
10 changes: 10 additions & 0 deletions examples/annotated_injection/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

set(ANNOTATED_INJECTION_SOURCES
main.cpp
car.cpp
main_brake.cpp
emergency_brake.cpp
)

add_executable(annotated_injection ${ANNOTATED_INJECTION_SOURCES})
target_link_libraries(annotated_injection fruit)
26 changes: 26 additions & 0 deletions examples/annotated_injection/brake.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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.
*/

#ifndef BRAKE_H
#define BRAKE_H

class Brake {
public:
// Activates the brake. Throws an exception if braking failed.
virtual void activate() = 0;
};

#endif // BRAKE_H
47 changes: 47 additions & 0 deletions examples/annotated_injection/car.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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.
*/

#include "car.h"
#include "main_brake.h"
#include "emergency_brake.h"

class CarImpl : public Car {
private:
Brake* mainBrake;
Brake* emergencyBrake;

public:
INJECT(CarImpl(ANNOTATED(MainBrake, Brake*) mainBrake,
ANNOTATED(EmergencyBrake, Brake*) emergencyBrake))
: mainBrake(mainBrake), emergencyBrake(emergencyBrake) {
}

void brake() override {
try {
mainBrake->activate();
} catch (...) {
// The main brake failed!
emergencyBrake->activate();
}
}
};

fruit::Component<Car> getCarComponent() {
return fruit::createComponent()
.bind<Car, CarImpl>()
.install(getMainBrakeComponent())
.install(getEmergencyBrakeComponent());
}
29 changes: 29 additions & 0 deletions examples/annotated_injection/car.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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.
*/

#ifndef CAR_H
#define CAR_H

#include <fruit/fruit.h>

class Car {
public:
virtual void brake() = 0;
};

fruit::Component<Car> getCarComponent();

#endif // CAR_H
31 changes: 31 additions & 0 deletions examples/annotated_injection/emergency_brake.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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.
*/

#include "emergency_brake.h"

class EmergencyBrakeImpl : public Brake {
public:
INJECT(EmergencyBrakeImpl()) = default;

void activate() override {
// ...
}
};

fruit::Component<fruit::Annotated<EmergencyBrake, Brake>> getEmergencyBrakeComponent() {
return fruit::createComponent()
.bind<fruit::Annotated<EmergencyBrake, Brake>, EmergencyBrakeImpl>();
}
30 changes: 30 additions & 0 deletions examples/annotated_injection/emergency_brake.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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.
*/

#ifndef EMERGENCY_BRAKE_H
#define EMERGENCY_BRAKE_H

#include "brake.h"

#include <fruit/fruit.h>

// This type is not meaningful by itself, it's only used for annotated injection.
// This marks a the Brake instance that represents the main brake.
struct EmergencyBrake {};

fruit::Component<fruit::Annotated<EmergencyBrake, Brake>> getEmergencyBrakeComponent();

#endif // EMERGENCY_BRAKE_H
28 changes: 28 additions & 0 deletions examples/annotated_injection/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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.
*/

#include "car.h"

using fruit::Injector;

int main() {
Injector<Car> injector(getCarComponent());
Car* car(injector);

car->brake();

return 0;
}
31 changes: 31 additions & 0 deletions examples/annotated_injection/main_brake.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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.
*/

#include "main_brake.h"

class MainBrakeImpl : public Brake {
public:
INJECT(MainBrakeImpl()) = default;

void activate() override {
// ...
}
};

fruit::Component<fruit::Annotated<MainBrake, Brake>> getMainBrakeComponent() {
return fruit::createComponent()
.bind<fruit::Annotated<MainBrake, Brake>, MainBrakeImpl>();
}
30 changes: 30 additions & 0 deletions examples/annotated_injection/main_brake.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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.
*/

#ifndef MAIN_BRAKE_H
#define MAIN_BRAKE_H

#include "brake.h"

#include <fruit/fruit.h>

// This type is not meaningful by itself, it's only used for annotated injection.
// This marks a the Brake instance that represents the main brake.
struct MainBrake {};

fruit::Component<fruit::Annotated<MainBrake, Brake>> getMainBrakeComponent();

#endif // MAIN_BRAKE_H

0 comments on commit 695a93f

Please sign in to comment.