xref: /aosp_15_r20/external/google-fruit/examples/annotated_injection/car.cpp (revision a65addddcf69f38db5b288d787b6b7571a57bb8f)
1 /*
2  * Copyright 2014 Google Inc. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "car.h"
18 #include "emergency_brake.h"
19 #include "main_brake.h"
20 
21 class CarImpl : public Car {
22 private:
23   Brake* mainBrake;
24   Brake* emergencyBrake;
25 
26 public:
27   // NOLINTBEGIN(bugprone-easily-swappable-parameters)
INJECT(CarImpl (ANNOTATED (MainBrake,Brake *)mainBrake,ANNOTATED (EmergencyBrake,Brake *)emergencyBrake))28   INJECT(CarImpl(
29     ANNOTATED(MainBrake, Brake*) mainBrake,
30     ANNOTATED(EmergencyBrake, Brake*) emergencyBrake))
31       : mainBrake(mainBrake), emergencyBrake(emergencyBrake) {}
32   // NOLINTEND(bugprone-easily-swappable-parameters)
33 
brake()34   void brake() override {
35     try {
36       mainBrake->activate();
37     } catch (...) {
38       // The main brake failed!
39       emergencyBrake->activate();
40     }
41   }
42 };
43 
getCarComponent()44 fruit::Component<Car> getCarComponent() {
45   return fruit::createComponent()
46       .bind<Car, CarImpl>()
47       .install(getMainBrakeComponent)
48       .install(getEmergencyBrakeComponent);
49 }
50