-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an example that shows how to use annotated injection.
- Loading branch information
1 parent
2f36ab0
commit 695a93f
Showing
11 changed files
with
273 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |