diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 05f16c3c..dec43f5c 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 00000000..e94ee20f --- /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 00000000..5fbf7543 --- /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 00000000..0eb7d9b0 --- /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 00000000..32f47fa4 --- /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 getCarComponent() { + return fruit::createComponent() + .bind() + .install(getMainBrakeComponent()) + .install(getEmergencyBrakeComponent()); +} diff --git a/examples/annotated_injection/car.h b/examples/annotated_injection/car.h new file mode 100644 index 00000000..45a96621 --- /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 + +class Car { +public: + virtual void brake() = 0; +}; + +fruit::Component 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 00000000..8fe69aa0 --- /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> getEmergencyBrakeComponent() { + return fruit::createComponent() + .bind, EmergencyBrakeImpl>(); +} diff --git a/examples/annotated_injection/emergency_brake.h b/examples/annotated_injection/emergency_brake.h new file mode 100644 index 00000000..335d9f13 --- /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 + +// 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> getEmergencyBrakeComponent(); + +#endif // EMERGENCY_BRAKE_H diff --git a/examples/annotated_injection/main.cpp b/examples/annotated_injection/main.cpp new file mode 100644 index 00000000..68c0248d --- /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 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 00000000..9383305b --- /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> getMainBrakeComponent() { + return fruit::createComponent() + .bind, MainBrakeImpl>(); +} diff --git a/examples/annotated_injection/main_brake.h b/examples/annotated_injection/main_brake.h new file mode 100644 index 00000000..6b96f107 --- /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 + +// 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> getMainBrakeComponent(); + +#endif // MAIN_BRAKE_H