diff options
author | scherkus@chromium.org <scherkus@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-11-04 23:14:18 +0000 |
---|---|---|
committer | scherkus@chromium.org <scherkus@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-11-04 23:14:18 +0000 |
commit | d69814028ebe5ec50ccedb7e64cba7e452371099 (patch) | |
tree | a817b23ad70777aa7850d2b2aadbffa4ad1f95a9 /media/base | |
parent | 95f3674d76b15309d64fc5b11449863bbec08f1a (diff) | |
download | chromium_src-d69814028ebe5ec50ccedb7e64cba7e452371099.zip chromium_src-d69814028ebe5ec50ccedb7e64cba7e452371099.tar.gz chromium_src-d69814028ebe5ec50ccedb7e64cba7e452371099.tar.bz2 |
Initial StateMatrix idea using composition and void*
BUG=none
TEST=media_unittests
Review URL: http://codereview.chromium.org/3416001
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@65128 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'media/base')
-rw-r--r-- | media/base/state_matrix.cc | 26 | ||||
-rw-r--r-- | media/base/state_matrix.h | 61 | ||||
-rw-r--r-- | media/base/state_matrix_unittest.cc | 53 |
3 files changed, 140 insertions, 0 deletions
diff --git a/media/base/state_matrix.cc b/media/base/state_matrix.cc new file mode 100644 index 0000000..f0ef07b --- /dev/null +++ b/media/base/state_matrix.cc @@ -0,0 +1,26 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "media/base/state_matrix.h" + +#include "base/stl_util-inl.h" + +namespace media { + +StateMatrix::StateMatrix() { +} + +StateMatrix::~StateMatrix() { + STLDeleteValues(&states_); +} + +bool StateMatrix::IsStateDefined(int state) { + return states_.find(state) != states_.end(); +} + +int StateMatrix::ExecuteHandler(void* instance, int state) { + return states_.find(state)->second->ExecuteHandler(instance); +} + +} // namespace media diff --git a/media/base/state_matrix.h b/media/base/state_matrix.h new file mode 100644 index 0000000..779646f --- /dev/null +++ b/media/base/state_matrix.h @@ -0,0 +1,61 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef MEDIA_BASE_STATE_MATRIX_H_ +#define MEDIA_BASE_STATE_MATRIX_H_ + +#include <map> + +#include "base/logging.h" + +namespace media { + +class StateMatrix { + public: + StateMatrix(); + ~StateMatrix(); + + template <typename HandlerType> + void AddState(int state, int (HandlerType::* handler)()) { + CHECK(!IsStateDefined(state)); + StateEntryBase* entry = new StateEntry<HandlerType>(handler); + states_.insert(std::make_pair(state, entry)); + } + + bool IsStateDefined(int state); + + int ExecuteHandler(void* instance, int state); + + private: + class StateEntryBase { + public: + StateEntryBase() {} + virtual ~StateEntryBase() {} + + virtual int ExecuteHandler(void* instance) = 0; + }; + + template <typename HandlerType> + class StateEntry : public StateEntryBase { + public: + explicit StateEntry(int (HandlerType::* handler)()) : handler_(handler) {} + virtual ~StateEntry() {} + + virtual int ExecuteHandler(void* instance) { + return (reinterpret_cast<HandlerType*>(instance)->*handler_)(); + } + + private: + int (HandlerType::* handler_)(); + }; + + typedef std::map<int, StateEntryBase*> StateMap; + StateMap states_; + + DISALLOW_COPY_AND_ASSIGN(StateMatrix); +}; + +} // namespace media + +#endif // MEDIA_BASE_STATE_MATRIX_H_ diff --git a/media/base/state_matrix_unittest.cc b/media/base/state_matrix_unittest.cc new file mode 100644 index 0000000..599ea6f --- /dev/null +++ b/media/base/state_matrix_unittest.cc @@ -0,0 +1,53 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "media/base/state_matrix.h" +#include "testing/gmock/include/gmock/gmock.h" + +using ::testing::Return; + +namespace media { + +class MockStateHandler { + public: + enum State { + A, + B, + C, + }; + + MOCK_METHOD0(HandleA, int()); + MOCK_METHOD0(HandleB, int()); + MOCK_METHOD0(HandleC, int()); +}; + +TEST(StateMatrixTest, AddState) { + MockStateHandler handler; + StateMatrix sm; + + EXPECT_FALSE(sm.IsStateDefined(MockStateHandler::A)); + EXPECT_FALSE(sm.IsStateDefined(MockStateHandler::B)); + EXPECT_FALSE(sm.IsStateDefined(MockStateHandler::C)); + + sm.AddState(MockStateHandler::A, &MockStateHandler::HandleA); + + EXPECT_TRUE(sm.IsStateDefined(MockStateHandler::A)); + EXPECT_FALSE(sm.IsStateDefined(MockStateHandler::B)); + EXPECT_FALSE(sm.IsStateDefined(MockStateHandler::C)); +} + +TEST(StateMatrixTest, ExecuteHandler) { + MockStateHandler handler; + StateMatrix sm; + + sm.AddState(MockStateHandler::A, &MockStateHandler::HandleA); + + EXPECT_CALL(handler, HandleA()) + .WillOnce(Return(MockStateHandler::B)); + + EXPECT_EQ(MockStateHandler::B, + sm.ExecuteHandler(&handler, MockStateHandler::A)); +} + +} // namespace media |