Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // A filter factory handles the creation of filters given a FilterType (i.e., | 5 // A filter factory handles the creation of filters given a FilterType (i.e., |
| 6 // FILTER_AUDIO_DECODER) and a MediaFormat. Generally a filter factory handles | 6 // FILTER_AUDIO_DECODER) and a MediaFormat. Generally a filter factory handles |
| 7 // creating a single type of filter, with multiple factories combined into a | 7 // creating a single type of filter, with multiple factories combined into a |
| 8 // FilterFactoryCollection. We use some template tricks to enforce type-safety | 8 // FilterFactoryCollection. We use some template tricks to enforce type-safety |
| 9 // and eliminate casting for callers. | 9 // and eliminate casting for callers. |
| 10 // | 10 // |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 75 public: | 75 public: |
| 76 FilterFactoryCollection() {} | 76 FilterFactoryCollection() {} |
| 77 | 77 |
| 78 // Adds a factory to the end of the collection. | 78 // Adds a factory to the end of the collection. |
| 79 void AddFactory(FilterFactory* factory) { | 79 void AddFactory(FilterFactory* factory) { |
| 80 factories_.push_back(factory); | 80 factories_.push_back(factory); |
| 81 } | 81 } |
| 82 | 82 |
| 83 protected: | 83 protected: |
| 84 // Attempts to create a filter by walking down the list of filter factories. | 84 // Attempts to create a filter by walking down the list of filter factories. |
| 85 MediaFilter* Create(FilterType filter_type, const MediaFormat& media_format) { | 85 MediaFilter* Create(FilterType filter_type, const MediaFormat& media_format) { |
|
M-A Ruel
2009/11/05 20:31:28
virtual
| |
| 86 MediaFilter* filter = NULL; | 86 MediaFilter* filter = NULL; |
| 87 for (FactoryVector::iterator factory = factories_.begin(); | 87 for (FactoryVector::iterator factory = factories_.begin(); |
| 88 !filter && factory != factories_.end(); | 88 !filter && factory != factories_.end(); |
| 89 ++factory) { | 89 ++factory) { |
| 90 filter = (*factory)->Create(filter_type, media_format); | 90 filter = (*factory)->Create(filter_type, media_format); |
| 91 } | 91 } |
| 92 return filter; | 92 return filter; |
| 93 } | 93 } |
| 94 | 94 |
| 95 private: | 95 private: |
| 96 ~FilterFactoryCollection() {} | |
|
M-A Ruel
2009/11/05 20:31:28
virtual
jam
2009/11/05 21:52:37
no child classes. if they get added in the future
| |
| 97 | |
| 96 typedef std::vector< scoped_refptr<FilterFactory> > FactoryVector; | 98 typedef std::vector< scoped_refptr<FilterFactory> > FactoryVector; |
| 97 FactoryVector factories_; | 99 FactoryVector factories_; |
| 98 | 100 |
| 99 DISALLOW_COPY_AND_ASSIGN(FilterFactoryCollection); | 101 DISALLOW_COPY_AND_ASSIGN(FilterFactoryCollection); |
| 100 }; | 102 }; |
| 101 | 103 |
| 102 //----------------------------------------------------------------------------- | 104 //----------------------------------------------------------------------------- |
| 103 | 105 |
| 104 // This template is used by classes to implement a type-safe filter factory. | 106 // This template is used by classes to implement a type-safe filter factory. |
| 105 // If the derived class needs to examine the |media_format| passed to the | 107 // If the derived class needs to examine the |media_format| passed to the |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 116 const MediaFormat& media_format) { | 118 const MediaFormat& media_format) { |
| 117 Filter* filter = NULL; | 119 Filter* filter = NULL; |
| 118 if (Filter::filter_type() == filter_type && | 120 if (Filter::filter_type() == filter_type && |
| 119 Filter::IsMediaFormatSupported(media_format)) { | 121 Filter::IsMediaFormatSupported(media_format)) { |
| 120 filter = new Filter(); | 122 filter = new Filter(); |
| 121 } | 123 } |
| 122 return filter; | 124 return filter; |
| 123 } | 125 } |
| 124 | 126 |
| 125 private: | 127 private: |
| 128 ~FilterFactoryImpl0() {} | |
|
M-A Ruel
2009/11/05 20:31:28
virtual
| |
| 129 | |
| 126 DISALLOW_COPY_AND_ASSIGN(FilterFactoryImpl0); | 130 DISALLOW_COPY_AND_ASSIGN(FilterFactoryImpl0); |
| 127 }; | 131 }; |
| 128 | 132 |
| 129 // This template can be used by classes that need to be constructed with a | 133 // This template can be used by classes that need to be constructed with a |
| 130 // parameter that needs to be used in the construction of the actual filter. | 134 // parameter that needs to be used in the construction of the actual filter. |
| 131 // This would usually be a "parent" object which the instantiated filter needs | 135 // This would usually be a "parent" object which the instantiated filter needs |
| 132 // to communicate with. The class's CreateFactory method would look like: | 136 // to communicate with. The class's CreateFactory method would look like: |
| 133 // static FilterFactory* CreateFactory(MyRequiredParentClass* parent) { | 137 // static FilterFactory* CreateFactory(MyRequiredParentClass* parent) { |
| 134 // return new FilterFactoryImpl1<MyClass>(parent); | 138 // return new FilterFactoryImpl1<MyClass>(parent); |
| 135 // } | 139 // } |
| 136 // The class would be constructed with the same pointer passed to the | 140 // The class would be constructed with the same pointer passed to the |
| 137 // CreateFactory method. | 141 // CreateFactory method. |
| 138 template <class Filter, class A> | 142 template <class Filter, class A> |
| 139 class FilterFactoryImpl1 : public FilterFactory { | 143 class FilterFactoryImpl1 : public FilterFactory { |
| 140 public: | 144 public: |
| 141 explicit FilterFactoryImpl1(A a) : a_(a) {} | 145 explicit FilterFactoryImpl1(A a) : a_(a) {} |
| 142 | 146 |
| 143 protected: | 147 protected: |
| 144 virtual MediaFilter* Create(FilterType filter_type, | 148 virtual MediaFilter* Create(FilterType filter_type, |
| 145 const MediaFormat& media_format) { | 149 const MediaFormat& media_format) { |
| 146 Filter* filter = NULL; | 150 Filter* filter = NULL; |
| 147 if (Filter::filter_type() == filter_type && | 151 if (Filter::filter_type() == filter_type && |
| 148 Filter::IsMediaFormatSupported(media_format)) { | 152 Filter::IsMediaFormatSupported(media_format)) { |
| 149 filter = new Filter(a_); | 153 filter = new Filter(a_); |
| 150 } | 154 } |
| 151 return filter; | 155 return filter; |
| 152 } | 156 } |
| 153 | 157 |
| 154 private: | 158 private: |
| 159 ~FilterFactoryImpl1() {} | |
| 160 | |
| 155 A const a_; | 161 A const a_; |
| 156 DISALLOW_COPY_AND_ASSIGN(FilterFactoryImpl1); | 162 DISALLOW_COPY_AND_ASSIGN(FilterFactoryImpl1); |
| 157 }; | 163 }; |
| 158 | 164 |
| 159 template <class Filter, class A, class B> | 165 template <class Filter, class A, class B> |
| 160 class FilterFactoryImpl2 : public FilterFactory { | 166 class FilterFactoryImpl2 : public FilterFactory { |
| 161 public: | 167 public: |
| 162 FilterFactoryImpl2(A a, B b) : a_(a), b_(b) {} | 168 FilterFactoryImpl2(A a, B b) : a_(a), b_(b) {} |
| 163 | 169 |
| 164 protected: | 170 protected: |
| 165 virtual MediaFilter* Create(FilterType filter_type, | 171 virtual MediaFilter* Create(FilterType filter_type, |
| 166 const MediaFormat& media_format) { | 172 const MediaFormat& media_format) { |
| 167 Filter* filter = NULL; | 173 Filter* filter = NULL; |
| 168 if (Filter::filter_type() == filter_type && | 174 if (Filter::filter_type() == filter_type && |
| 169 Filter::IsMediaFormatSupported(media_format)) { | 175 Filter::IsMediaFormatSupported(media_format)) { |
| 170 filter = new Filter(a_, b_); | 176 filter = new Filter(a_, b_); |
| 171 } | 177 } |
| 172 return filter; | 178 return filter; |
| 173 } | 179 } |
| 174 | 180 |
| 175 private: | 181 private: |
| 182 ~FilterFactoryImpl2() {} | |
| 183 | |
| 176 A const a_; | 184 A const a_; |
| 177 B const b_; | 185 B const b_; |
| 178 | 186 |
| 179 DISALLOW_COPY_AND_ASSIGN(FilterFactoryImpl2); | 187 DISALLOW_COPY_AND_ASSIGN(FilterFactoryImpl2); |
| 180 }; | 188 }; |
| 181 | 189 |
| 182 //------------------------------------------------------------------------------ | 190 //------------------------------------------------------------------------------ |
| 183 | 191 |
| 184 // This specialized factory is typically used by test programs that create | 192 // This specialized factory is typically used by test programs that create |
| 185 // a filter instance, and want to return that specific instance of the test | 193 // a filter instance, and want to return that specific instance of the test |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 207 create_called_ = true; | 215 create_called_ = true; |
| 208 return filter_; | 216 return filter_; |
| 209 } else { | 217 } else { |
| 210 NOTREACHED(); | 218 NOTREACHED(); |
| 211 } | 219 } |
| 212 } | 220 } |
| 213 return NULL; | 221 return NULL; |
| 214 } | 222 } |
| 215 | 223 |
| 216 private: | 224 private: |
| 225 ~InstanceFilterFactory() {} | |
| 226 | |
| 217 scoped_refptr<Filter> filter_; | 227 scoped_refptr<Filter> filter_; |
| 218 bool create_called_; | 228 bool create_called_; |
| 219 | 229 |
| 220 DISALLOW_COPY_AND_ASSIGN(InstanceFilterFactory); | 230 DISALLOW_COPY_AND_ASSIGN(InstanceFilterFactory); |
| 221 }; | 231 }; |
| 222 | 232 |
| 223 } // namespace media | 233 } // namespace media |
| 224 | 234 |
| 225 #endif // MEDIA_BASE_FACTORY_H_ | 235 #endif // MEDIA_BASE_FACTORY_H_ |
| OLD | NEW |