AlexaClientSDK  3.0.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
CookBook.h
Go to the documentation of this file.
1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. 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  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0/
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 #ifndef ACSDKMANUFACTORY_INTERNAL_COOKBOOK_H_
17 #define ACSDKMANUFACTORY_INTERNAL_COOKBOOK_H_
18 
19 #include <functional>
20 #include <memory>
21 #include <typeindex>
22 #include <unordered_map>
23 #include <unordered_set>
24 #include <vector>
25 
27 
31 
32 namespace alexaClientSDK {
33 namespace acsdkManufactory {
34 namespace internal {
35 
36 // Forward declaration
37 class RuntimeManufactory;
38 
42 class CookBook {
43 public:
47  CookBook();
48 
57  template <typename Type, typename... Dependencies>
58  CookBook& addUniqueFactory(std::function<std::unique_ptr<Type>(Dependencies...)> factory);
59 
68  template <typename Type, typename... Dependencies>
69  CookBook& addUniqueFactory(std::unique_ptr<Type> (*factory)(Dependencies...));
70 
82  template <typename Type, typename... Dependencies>
83  CookBook& addPrimaryFactory(std::function<std::shared_ptr<Type>(Dependencies...)> factory);
84 
97  template <typename Annotation, typename Type, typename... Dependencies>
98  CookBook& addPrimaryFactory(std::function<Annotated<Annotation, Type>(Dependencies...)> factory);
99 
111  template <typename Type, typename... Dependencies>
112  CookBook& addPrimaryFactory(std::shared_ptr<Type> (*factory)(Dependencies...));
113 
126  template <typename Annotation, typename Type, typename... Dependencies>
127  CookBook& addPrimaryFactory(Annotated<Annotation, Type> (*factory)(Dependencies...));
128 
138  template <typename Type, typename... Dependencies>
139  CookBook& addRequiredFactory(std::function<std::shared_ptr<Type>(Dependencies...)> factory);
140 
151  template <typename Annotation, typename Type, typename... Dependencies>
152  CookBook& addRequiredFactory(std::function<Annotated<Annotation, Type>(Dependencies...)> factory);
153 
163  template <typename Type, typename... Dependencies>
164  CookBook& addRequiredFactory(std::shared_ptr<Type> (*factory)(Dependencies...));
165 
176  template <typename Annotation, typename Type, typename... Dependencies>
177  CookBook& addRequiredFactory(Annotated<Annotation, Type> (*factory)(Dependencies...));
178 
188  template <typename Type, typename... Dependencies>
189  CookBook& addRetainedFactory(std::function<std::shared_ptr<Type>(Dependencies...)> factory);
190 
201  template <typename Annotation, typename Type, typename... Dependencies>
202  CookBook& addRetainedFactory(std::function<Annotated<Annotation, Type>(Dependencies...)> factory);
203 
213  template <typename Type, typename... Dependencies>
214  CookBook& addRetainedFactory(std::shared_ptr<Type> (*factory)(Dependencies...));
215 
226  template <typename Annotation, typename Type, typename... Dependencies>
227  CookBook& addRetainedFactory(Annotated<Annotation, Type> (*factory)(Dependencies...));
228 
238  template <typename Type, typename... Dependencies>
239  CookBook& addUnloadableFactory(std::function<std::shared_ptr<Type>(Dependencies...)> factory);
240 
251  template <typename Annotation, typename Type, typename... Dependencies>
252  CookBook& addUnloadableFactory(std::function<Annotated<Annotation, Type>(Dependencies...)> factory);
253 
263  template <typename Type, typename... Dependencies>
264  CookBook& addUnloadableFactory(std::shared_ptr<Type> (*factory)(Dependencies...));
265 
276  template <typename Annotation, typename Type, typename... Dependencies>
277  CookBook& addUnloadableFactory(Annotated<Annotation, Type> (*factory)(Dependencies...));
278 
287  template <typename Type>
288  CookBook& addInstance(const Type& instance);
289 
299  CookBook& addCookBook(const CookBook& cookBook);
300 
307  bool checkCompleteness();
308 
316  bool doRequiredGets(RuntimeManufactory& runtimeManufactory);
317 
325  template <typename Type>
326  std::unique_ptr<Type> createUniquePointer(RuntimeManufactory& runtimeManufactory);
327 
334  template <typename Type>
335  std::unique_ptr<AbstractPointerCache> createPointerCache();
336 
337 private:
341  class FactoryRecipe : public AbstractRecipe {
342  public:
344  using Factory = void* (*)();
345 
354  FactoryRecipe(
355  Factory factory,
356  CachedInstanceLifecycle lifecycle,
357  std::vector<avsCommon::utils::TypeIndex> dependencies,
358  ProduceInstanceFunction produceFunction,
359  DeleteInstanceFunction deleteFunction);
360 
364  ~FactoryRecipe() = default;
365 
370  Factory getFactory() const;
371 
374  bool isEquivalent(const std::shared_ptr<AbstractRecipe>& recipe) const override;
376 
377  private:
379  Factory m_factory;
380  };
381 
385  class FunctionRecipe : public AbstractRecipe {
386  public:
389  using Function = void*;
390 
393  using DeleteRecipeFunction = void (*)(void*);
394 
404  FunctionRecipe(
405  Function function,
406  CachedInstanceLifecycle lifecycle,
407  std::vector<avsCommon::utils::TypeIndex> dependencies,
408  ProduceInstanceFunction produceFunction,
409  DeleteInstanceFunction deleteFunction,
410  DeleteRecipeFunction deleteRecipeFunction);
411 
415  ~FunctionRecipe();
416 
421  Function getFunction() const;
422 
425  bool isEquivalent(const std::shared_ptr<AbstractRecipe>& recipe) const override;
427  private:
429  Function m_function;
430 
432  DeleteRecipeFunction m_deleteRecipeFunction;
433  };
434 
439  class InstanceRecipe : public AbstractRecipe {
440  public:
447  InstanceRecipe(
448  std::shared_ptr<void> instance,
449  ProduceInstanceFunction produceFunction,
450  DeleteInstanceFunction deleteFunction);
451 
455  ~InstanceRecipe() = default;
456 
461  std::shared_ptr<void> getInstance() const;
462 
465  bool isEquivalent(const std::shared_ptr<AbstractRecipe>& recipe) const override;
467  private:
468  std::shared_ptr<void> m_instance;
469  };
470 
484  template <typename Type, typename... Dependencies>
485  static void* produceFromSharedFactoryRecipe(
486  std::shared_ptr<AbstractRecipe> recipe,
487  RuntimeManufactory& runtimeManufactory,
488  void* cachedValue);
489 
504  template <typename Type, typename Annotation, typename... Dependencies>
505  static void* produceFromAnnotatedSharedFactoryRecipe(
506  std::shared_ptr<AbstractRecipe> recipe,
507  RuntimeManufactory& runtimeManufactory,
508  void* cachedValue);
509 
523  template <typename Type, typename... Dependencies>
524  static void* produceFromSharedFunctionRecipe(
525  std::shared_ptr<AbstractRecipe> recipe,
526  RuntimeManufactory& runtimeManufactory,
527  void* cachedValue);
528 
543  template <typename Type, typename Annotation, typename... Dependencies>
544  static void* produceFromAnnotatedSharedFunctionRecipe(
545  std::shared_ptr<AbstractRecipe> recipe,
546  RuntimeManufactory& runtimeManufactory,
547  void* cachedValue);
548 
555  template <typename Type>
556  static void deleteFunctionForSharedRecipe(void* cachedValue);
557 
565  template <typename Type, typename... Dependencies>
566  static void deleteFunctionForFunctionRecipeFunction(void* function);
567 
581  template <typename Type, typename... Dependencies>
582  static void* produceFromWeakFactoryRecipe(
583  std::shared_ptr<AbstractRecipe> recipe,
584  RuntimeManufactory& runtimeManufactory,
585  void* cachedValue);
586 
601  template <typename Type, typename Annotation, typename... Dependencies>
602  static void* produceFromAnnotatedWeakFactoryRecipe(
603  std::shared_ptr<AbstractRecipe> recipe,
604  RuntimeManufactory& runtimeManufactory,
605  void* cachedValue);
606 
620  template <typename Type, typename... Dependencies>
621  static void* produceFromWeakFunctionRecipe(
622  std::shared_ptr<AbstractRecipe> recipe,
623  RuntimeManufactory& runtimeManufactory,
624  void* cachedValue);
625 
640  template <typename Type, typename Annotation, typename... Dependencies>
641  static void* produceFromAnnotatedWeakFunctionRecipe(
642  std::shared_ptr<AbstractRecipe> recipe,
643  RuntimeManufactory& runtimeManufactory,
644  void* cachedValue);
645 
658  template <typename Type, typename... Dependencies>
659  static void* produceFromUniqueFactoryRecipe(
660  std::shared_ptr<AbstractRecipe> recipe,
661  RuntimeManufactory& runtimeManufactory,
662  void* cachedValue);
663 
676  template <typename Type, typename... Dependencies>
677  static void* produceFromUniqueFunctionRecipe(
678  std::shared_ptr<AbstractRecipe> recipe,
679  RuntimeManufactory& runtimeManufactory,
680  void* cachedValue);
681 
687  static void deleteFunctionForUniqueRecipe(void* cachedValue);
688 
697  static void* produceFromInstanceRecipe(
698  std::shared_ptr<AbstractRecipe> recipe,
699  RuntimeManufactory& runtimeManufactory,
700  void* cachedValue);
701 
707  static void deleteFunctionForInstanceRecipe(void* cachedValue);
708 
719  class GetWrapperCollection {
723  using GetWrapper = bool (*)(RuntimeManufactory& runtimeManufactory);
724 
726  using GetWrapperIterator =
727  std::vector<bool (*)(RuntimeManufactory&), std::allocator<bool (*)(RuntimeManufactory&)>>::iterator;
728  using ConstGetWrapperIterator =
729  std::vector<bool (*)(RuntimeManufactory&), std::allocator<bool (*)(RuntimeManufactory&)>>::const_iterator;
730 
731  public:
739  template <typename Type>
740  bool append(GetWrapper getWrapper);
741 
747  void append(const std::shared_ptr<GetWrapperCollection>& collection);
748 
749  /*
750  * Return iterator to beginning of m_getWrappers.
751  * @return iterator of beginning of m_getWrappers.
752  */
753  GetWrapperIterator begin();
754 
755  /*
756  * Return iterator to end of m_getWrappers.
757  * @return iterator of end of m_getWrappers.
758  */
759  GetWrapperIterator end();
760 
761  /*
762  * Return const_iterator to beginning of m_getWrappers.
763  * @return const_iterator of beginning of m_getWrappers.
764  */
765  ConstGetWrapperIterator cbegin() const;
766 
767  /*
768  * Return const_iterator to end of m_getWrappers.
769  * @return const_iterator of end of m_getWrappers.
770  */
771  ConstGetWrapperIterator cend() const;
772 
773  /*
774  * Return const_iterator to beginning of m_getWrappers.
775  * @return const_iterator of beginning of m_getWrappers.
776  */
777  ConstGetWrapperIterator begin() const;
778 
779  /*
780  * Return const_iterator to end of m_getWrappers.
781  * @return const_iterator of end of m_getWrappers.
782  */
783  ConstGetWrapperIterator end() const;
784 
785  private:
787  std::unordered_map<avsCommon::utils::TypeIndex, std::size_t> m_types;
788 
790  std::vector<GetWrapper> m_orderedGetWrappers;
791  };
792 
796  template <typename Type, typename... Dependencies>
797  using InstanceGetter = std::function<Type(Dependencies...)>;
798 
806  bool addInstance(avsCommon::utils::TypeIndex type, const std::shared_ptr<AbstractRecipe>& newInstanceRecipe);
807 
815  bool addRecipe(avsCommon::utils::TypeIndex type, const std::shared_ptr<AbstractRecipe>& newRecipe);
816 
822  bool checkForCyclicDependencies();
823 
830  bool checkIsValid(const char* functionName) const;
831 
839  void markInvalid(const std::string& event, const std::string& reason, const std::string& type = "");
840 
844  void logDependencies() const;
845 
855  template <typename Type, typename... Dependencies>
856  static Type invokeWithDependencies(
857  RuntimeManufactory& runtimeManufactory,
858  std::function<Type(Dependencies...)> function);
859 
870  template <typename Type, typename FunctionType, typename... Dependencies>
871  static Type innerInvokeWithDependencies(FunctionType function, Dependencies... dependencies);
872 
876  static std::string getLoggerTag();
877 
879  bool m_isValid;
880 
882  std::unordered_map<avsCommon::utils::TypeIndex, std::shared_ptr<AbstractRecipe>> m_recipes;
883 
886  std::shared_ptr<GetWrapperCollection> m_primaryGets;
887 
890  std::shared_ptr<GetWrapperCollection> m_requiredGets;
891 };
892 
893 } // namespace internal
894 } // namespace acsdkManufactory
895 } // namespace alexaClientSDK
896 
898 
899 #endif // ACSDKMANUFACTORY_INTERNAL_COOKBOOK_H_
bool doRequiredGets(RuntimeManufactory &runtimeManufactory)
Definition: CookBook_imp.h:612
CookBook & addPrimaryFactory(std::function< std::shared_ptr< Type >(Dependencies...)> factory)
Definition: CookBook_imp.h:93
CookBook & addRetainedFactory(std::function< std::shared_ptr< Type >(Dependencies...)> factory)
Definition: CookBook_imp.h:325
::std::string string
Definition: gtest-port.h:1097
bool checkCompleteness()
Definition: CookBook_imp.h:700
CookBook & addUnloadableFactory(std::function< std::shared_ptr< Type >(Dependencies...)> factory)
Definition: CookBook_imp.h:429
CookBook & addUniqueFactory(std::function< std::unique_ptr< Type >(Dependencies...)> factory)
Definition: CookBook_imp.h:41
CookBook & addCookBook(const CookBook &cookBook)
Definition: CookBook_imp.h:582
std::unique_ptr< Type > createUniquePointer(RuntimeManufactory &runtimeManufactory)
Definition: CookBook_imp.h:630
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
std::unique_ptr< AbstractPointerCache > createPointerCache()
Definition: CookBook_imp.h:670
type
Definition: upload.py:443
CookBook & addRequiredFactory(std::function< std::shared_ptr< Type >(Dependencies...)> factory)
Definition: CookBook_imp.h:209
CookBook & addInstance(const Type &instance)
Definition: CookBook_imp.h:533

AlexaClientSDK 3.0.0 - Copyright 2016-2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0