Home | History | Annotate | Download | only in annotated_injection
      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   INJECT(CarImpl(ANNOTATED(MainBrake, Brake*) mainBrake, ANNOTATED(EmergencyBrake, Brake*) emergencyBrake))
     28       : mainBrake(mainBrake), emergencyBrake(emergencyBrake) {}
     29 
     30   void brake() override {
     31     try {
     32       mainBrake->activate();
     33     } catch (...) {
     34       // The main brake failed!
     35       emergencyBrake->activate();
     36     }
     37   }
     38 };
     39 
     40 fruit::Component<Car> getCarComponent() {
     41   return fruit::createComponent()
     42       .bind<Car, CarImpl>()
     43       .install(getMainBrakeComponent)
     44       .install(getEmergencyBrakeComponent);
     45 }
     46