diff options
author | Marco Poletti <poletti.marco@gmail.com> | 2016-05-15 18:21:26 +0100 |
---|---|---|
committer | Marco Poletti <poletti.marco@gmail.com> | 2016-05-15 18:21:26 +0100 |
commit | 695a93f19ca753e27af6892ed8b772cdd1fe3ccf (patch) | |
tree | b9b3b22b71c44c739317304fe1fe003a002347e3 /examples | |
parent | 2f36ab092659d2375a12a7cbd7422dca24ef2090 (diff) | |
download | google-fruit-695a93f19ca753e27af6892ed8b772cdd1fe3ccf.tar.gz |
Add an example that shows how to use annotated injection.
Diffstat (limited to 'examples')
-rw-r--r-- | examples/CMakeLists.txt | 1 | ||||
-rw-r--r-- | examples/annotated_injection/BUILD | 10 | ||||
-rw-r--r-- | examples/annotated_injection/CMakeLists.txt | 10 | ||||
-rw-r--r-- | examples/annotated_injection/brake.h | 26 | ||||
-rw-r--r-- | examples/annotated_injection/car.cpp | 47 | ||||
-rw-r--r-- | examples/annotated_injection/car.h | 29 | ||||
-rw-r--r-- | examples/annotated_injection/emergency_brake.cpp | 31 | ||||
-rw-r--r-- | examples/annotated_injection/emergency_brake.h | 30 | ||||
-rw-r--r-- | examples/annotated_injection/main.cpp | 28 | ||||
-rw-r--r-- | examples/annotated_injection/main_brake.cpp | 31 | ||||
-rw-r--r-- | examples/annotated_injection/main_brake.h | 30 |
11 files changed, 273 insertions, 0 deletions
diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 05f16c3..dec43f5 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -6,3 +6,4 @@ add_subdirectory(hello_world) add_subdirectory(server) add_subdirectory(multibindings) add_subdirectory(scaling_doubles) +add_subdirectory(annotated_injection) diff --git a/examples/annotated_injection/BUILD b/examples/annotated_injection/BUILD new file mode 100644 index 0000000..e94ee20 --- /dev/null +++ b/examples/annotated_injection/BUILD @@ -0,0 +1,10 @@ + + +cc_binary( + name = "annotated_injection", + srcs = glob([ + "*.cpp", + "*.h", + ]), + deps = ["//:fruit"], +) diff --git a/examples/annotated_injection/CMakeLists.txt b/examples/annotated_injection/CMakeLists.txt new file mode 100644 index 0000000..5fbf754 --- /dev/null +++ b/examples/annotated_injection/CMakeLists.txt @@ -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) diff --git a/examples/annotated_injection/brake.h b/examples/annotated_injection/brake.h new file mode 100644 index 0000000..0eb7d9b --- /dev/null +++ b/examples/annotated_injection/brake.h @@ -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 diff --git a/examples/annotated_injection/car.cpp b/examples/annotated_injection/car.cpp new file mode 100644 index 0000000..32f47fa --- /dev/null +++ b/examples/annotated_injection/car.cpp @@ -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()); +} diff --git a/examples/annotated_injection/car.h b/examples/annotated_injection/car.h new file mode 100644 index 0000000..45a9662 --- /dev/null +++ b/examples/annotated_injection/car.h @@ -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 diff --git a/examples/annotated_injection/emergency_brake.cpp b/examples/annotated_injection/emergency_brake.cpp new file mode 100644 index 0000000..8fe69aa --- /dev/null +++ b/examples/annotated_injection/emergency_brake.cpp @@ -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>(); +} diff --git a/examples/annotated_injection/emergency_brake.h b/examples/annotated_injection/emergency_brake.h new file mode 100644 index 0000000..335d9f1 --- /dev/null +++ b/examples/annotated_injection/emergency_brake.h @@ -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 diff --git a/examples/annotated_injection/main.cpp b/examples/annotated_injection/main.cpp new file mode 100644 index 0000000..68c0248 --- /dev/null +++ b/examples/annotated_injection/main.cpp @@ -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; +} diff --git a/examples/annotated_injection/main_brake.cpp b/examples/annotated_injection/main_brake.cpp new file mode 100644 index 0000000..9383305 --- /dev/null +++ b/examples/annotated_injection/main_brake.cpp @@ -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>(); +} diff --git a/examples/annotated_injection/main_brake.h b/examples/annotated_injection/main_brake.h new file mode 100644 index 0000000..6b96f10 --- /dev/null +++ b/examples/annotated_injection/main_brake.h @@ -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 |