Twitch SDK (Internal)
resourcefactorychain.h
Go to the documentation of this file.
1 /********************************************************************************************
2 * Twitch Broadcasting SDK
3 *
4 * This software is supplied under the terms of a license agreement with Twitch Interactive, Inc. and
5 * may not be copied or used except in accordance with the terms of that agreement
6 * Copyright (c) 2012-2015 Twitch Interactive, Inc.
7 *********************************************************************************************/
8 
9 #pragma once
10 
11 #include <functional>
12 #include <memory>
13 
14 namespace ttv
15 {
16  template <typename RESOURCE, typename FACTORY>
18  {
19  public:
20  typedef std::function<TTV_ErrorCode(const std::shared_ptr<FACTORY>& factory, std::shared_ptr<RESOURCE>& result)> CreateFunc;
21  typedef std::function<bool(const std::shared_ptr<FACTORY>& factory)> QueryFunc;
22 
23  public:
24  ResourceFactoryChain(const std::string& name)
25  : mName(name)
26  , mRegisteredDefaults(false)
27  {
28  }
29 
31  {
32  }
33 
34  virtual TTV_ErrorCode Register(const std::shared_ptr<FACTORY>& factory)
35  {
36  if (factory == nullptr)
37  {
38  ttv::trace::Message("Core", TTV_ML_ERROR, "ResourceFactoryChain::Register: null factory given");
39 
40  return TTV_EC_INVALID_ARG;
41  }
42 
43  auto iter = std::find(mChain.begin(), mChain.end(), factory);
44  if (iter != mChain.end())
45  {
46  ttv::trace::Message("Core", TTV_ML_ERROR, "ResourceFactoryChain::Register: factory already registered");
47 
48  return TTV_EC_INVALID_ARG;
49  }
50 
51  mChain.insert(mChain.begin(), factory);
52 
53  return TTV_EC_SUCCESS;
54  }
55 
56  virtual TTV_ErrorCode Unregister(const std::shared_ptr<FACTORY>& factory)
57  {
58  if (factory == nullptr)
59  {
60  ttv::trace::Message("Core", TTV_ML_ERROR, "ResourceFactoryChain::Unregister: null factory given");
61 
62  return TTV_EC_INVALID_ARG;
63  }
64 
65  auto iter = std::find(mChain.begin(), mChain.end(), factory);
66  if (iter == mChain.end())
67  {
68  ttv::trace::Message("Core", TTV_ML_ERROR, "ResourceFactoryChain::Register: factory not registered");
69 
70  return TTV_EC_INVALID_ARG;
71  }
72 
73  mChain.erase(iter);
74 
75  return TTV_EC_SUCCESS;
76  }
77 
78  virtual TTV_ErrorCode Create(const CreateFunc& createFunc, std::shared_ptr<RESOURCE>& result) const
79  {
80  result.reset();
81 
82  if (mChain.empty())
83  {
84  ttv::trace::Message("Core", TTV_ML_ERROR, "There are no %s factories registered", mName.c_str());
85  return TTV_EC_NO_FACTORIES_REGISTERED;
86  }
87 
88  auto copy = mChain;
89 
90  for (const auto& factory : copy)
91  {
92  TTV_ErrorCode ec = createFunc(factory, result);
93  if (TTV_SUCCEEDED(ec) && result != nullptr)
94  {
95  break;
96  }
97  }
98 
99  if (result != nullptr)
100  {
101  return TTV_EC_SUCCESS;
102  }
103  else
104  {
105  return TTV_EC_UNIMPLEMENTED;
106  }
107  }
108 
109  bool BoolQuery(const QueryFunc& queryFunc) const
110  {
111  auto copy = mChain;
112 
113  for (const auto& factory : copy)
114  {
115  if (queryFunc(factory))
116  {
117  return true;
118  }
119  }
120 
121  return false;
122  }
123 
125  {
126  mRegisteredDefaults = true;
127  }
128 
129  bool RegisteredDefaults() const
130  {
131  return mRegisteredDefaults;
132  }
133 
134  bool Empty() const
135  {
136  return mChain.empty();
137  }
138 
139  protected:
140  std::string mName;
141  std::vector<std::shared_ptr<FACTORY>> mChain;
143  };
144 }
virtual TTV_ErrorCode Register(const std::shared_ptr< FACTORY > &factory)
Definition: resourcefactorychain.h:34
ResourceFactoryChain(const std::string &name)
Definition: resourcefactorychain.h:24
std::function< bool(const std::shared_ptr< FACTORY > &factory)> QueryFunc
Definition: resourcefactorychain.h:21
Definition: tracingtypes.h:19
JSON (JavaScript Object Notation).
Definition: adsapi.h:16
#define TTV_SUCCEEDED(ec)
Definition: errortypes.h:230
void Message(const char *component, const TTV_MessageLevel messageLevel, const char *format,...)
virtual TTV_ErrorCode Unregister(const std::shared_ptr< FACTORY > &factory)
Definition: resourcefactorychain.h:56
virtual ~ResourceFactoryChain()
Definition: resourcefactorychain.h:30
std::vector< std::shared_ptr< FACTORY > > mChain
Definition: resourcefactorychain.h:141
void SetRegisteredDefaults()
Definition: resourcefactorychain.h:124
uint32_t TTV_ErrorCode
Definition: errortypes.h:30
Definition: resourcefactorychain.h:17
bool RegisteredDefaults() const
Definition: resourcefactorychain.h:129
bool Empty() const
Definition: resourcefactorychain.h:134
bool BoolQuery(const QueryFunc &queryFunc) const
Definition: resourcefactorychain.h:109
bool mRegisteredDefaults
Definition: resourcefactorychain.h:142
virtual TTV_ErrorCode Create(const CreateFunc &createFunc, std::shared_ptr< RESOURCE > &result) const
Definition: resourcefactorychain.h:78
std::function< TTV_ErrorCode(const std::shared_ptr< FACTORY > &factory, std::shared_ptr< RESOURCE > &result)> CreateFunc
Definition: resourcefactorychain.h:20
std::string mName
Definition: resourcefactorychain.h:140