本文整理汇总了C++中Signal::Connect方法的典型用法代码示例。如果您正苦于以下问题:C++ Signal::Connect方法的具体用法?C++ Signal::Connect怎么用?C++ Signal::Connect使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Signal
的用法示例。
在下文中一共展示了Signal::Connect方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: nameChanged
TEST(ConnectionList, MoveAssignmentOperator)
{
Signal<void(std::string)> nameChanged;
std::string name;
ConnectionList connections1;
{
ConnectionList connections2;
connections2 += nameChanged.Connect([&](std::string const& n) {
name = n;
});
nameChanged("alice");
EXPECT_EQ("alice", name);
connections1 = std::move(connections2);
nameChanged("bob");
EXPECT_EQ("bob", name);
connections2.Disconnect();
nameChanged("chuck");
EXPECT_EQ("chuck", name);
}
nameChanged("norris");
EXPECT_EQ("norris", name);
connections1.Disconnect();
nameChanged("gates");
EXPECT_EQ("norris", name);
}
示例2:
TEST(lagi_signal, basic) {
Signal<> s;
int x = 0;
Connection c = s.Connect(increment(x));
EXPECT_EQ(0, x);
s();
EXPECT_EQ(1, x);
}
示例3:
TEST(lagi_signal, basic) {
Signal<> s;
int x = 0;
Connection c = s.Connect([&] { ++x; });
EXPECT_EQ(0, x);
s();
EXPECT_EQ(1, x);
}
示例4: ConnectSingleShot
template <typename...Arguments, typename Func> POMDOG_EXPORT
Connection ConnectSingleShot(Signal<void(Arguments...)> & signal, Func && func)
{
auto connection = std::make_shared<Connection>();
*connection = signal.Connect([
conn = connection,
func = std::forward<Func>(func)
](Arguments... args) {
func(std::forward<Arguments>(args)...);
POMDOG_ASSERT(conn);
conn->Disconnect();
});
return *connection;
}
示例5: main
int main(int argc, char* argv[])
{
Classe myClass;
Slot<int, float> slot; //declaring a slot that holds functions and functors which receive 2 parameters, an int and a float
Signal<int, float> signal; //declares a signal compatible with the previous slot
slot.Attach(&myClass, &Classe::miMethod); //adds to the slot a member method of myClass
slot.Attach(&myFloat); //adds to the
slot.Attach(&Classe::StaticMethod); //adds a static method to the slot
signal.Connect(&slot); //connects the signal and the slot, so that firing the signal will also fire all the elements connected to the slot
signal(2, 3); //fires the signal, which will fire the three elements connected to the slot
slot.Detach(&myFloat); //detaches the function from the slot
signal(2, 4); //now, firing the signal will only fire two elements of the slot
slot.Detach(&myClass, &Classe::miMethod); //detaches the member of myClass
signal(2, 5); //fires the only one element of the slot
slot.Detach(&myClass, &Classe::miMethod2); //tryies to detach an element that is not present in the slot. Nothing happens
signal(2, 6);
slot.Detach(&Classe::StaticMethod); //detaches the last element of the signal
signal(2, 7); //slot empty, nothing is fired
//signal and slot for members with empty parameter list
Signal<> empParamSignal;
Slot<> empParamSlot;
empParamSlot.Attach(&empty);
empParamSignal.Connect(&empParamSlot);
empParamSignal();
return 0;
}
示例6: valueChanged
TEST(ConnectionList, ScopeGuardWithThreeConnections)
{
Signal<void(int)> valueChanged;
std::vector<int> integers;
{
ConnectionList connections;
auto slot = [&](int n){ integers.push_back(n); };
connections += valueChanged.Connect(slot);
connections += valueChanged.Connect(slot);
connections += valueChanged.Connect(slot);
valueChanged(42);
valueChanged(43);
}
valueChanged(44);
ASSERT_EQ(6, integers.size());
EXPECT_EQ(42, integers[0]);
EXPECT_EQ(42, integers[1]);
EXPECT_EQ(42, integers[2]);
EXPECT_EQ(43, integers[3]);
EXPECT_EQ(43, integers[4]);
EXPECT_EQ(43, integers[5]);
}
示例7: RegisterKeyBoardCallback
void CInputManager::RegisterKeyBoardCallback( SKeyTriggerType type, const Function<void(void)>& function, bool exclusive)
{
WORD id = (WORD)((type.m_code << 8) + type.m_state);
Signal<void(void)>* pSignal = NULL;
TCallbackMap::iterator iter = m_callBacks.find(id);
bool canAddNewSignal = iter == m_callBacks.end() || exclusive;
BEATS_ASSERT(canAddNewSignal, _T("Can't register keybaord callback twice!"));
if (iter != m_callBacks.end())
{
m_stack[id].push_back(iter->second);
}
pSignal = new Signal<void(void)>();
m_callBacks[id] = pSignal;
pSignal->Connect(function);
}