当前位置:网站首页>When std:: bind meets this
When std:: bind meets this
2022-07-27 11:21:00 【lqw198421】
background
Read the code of previous projects of the company , See a code as follows :
if (!m_api->SubFutureL1(
m_config.config.symbol[0].future, std::bind(
&ArbitrageFc::OnSymbol1FutureMd, this,
std::placeholders::_1)))
among ,SubFutureL1 and OnFutureL1MdFunc Is defined as follows :
bool StrategyEngineImp::SubFutureL1(int64_t symbol, OnFutureL1MdFunc callback) {
m_future_l1_callback[symbol] = callback;
return m_mm.SubFuture(symbol);
}
typedef std::function<void(const FutureL1MarketData&)> OnFutureL1MdFunc; //std::function Next time
void OnSymbol1FutureMd(const FutureL1MarketData& md); //( class ArbitrageFc Member function of )
About the “bind + this” The way is not understood , After checking , Special records ;
std::bind
origin
stay c++98 in , Yes std::bind1st and std::bind2nd, Can be used to bind func Of 2 Parameters , But the use is not friendly ; later c++11 Introduced in the std::bind, Yes, before bind 1st and 2nd With the essential improvement ; Reference resources C++11 Medium std::bind The saying in is :
bind In itself, it is an idea of delayed calculation , It can bind ordinary functions 、 Global function 、 Static functions 、 Class static functions and even class member functions
Example
Let's look directly at specific examples ( Also refer to the above link ):
#include <iostream>
#include <functional>
using namespace std;
int TestFunc(int a, char c, float f)
{
cout << a << endl;
cout << c << endl;
cout << f << endl;
return a;
}
int main()
{
auto bindFunc1 = bind(TestFunc, std::placeholders::_1, 'A', 100.1);
bindFunc1(10);
cout << "=================================\n";
// hold TestFunc Bound to the bindFunc2 On ,bindFunc2 The second parameter of is TestFunc The first parameter of
//bindFunc2 The first parameter of is TestFunc Second parameter of , The last parameter is fixed to 100.1
// Be similar to TestFunc(bindFunc2's_var_2, bindFunc2's_var_1, 100.1);
auto bindFunc2 = bind(TestFunc, std::placeholders::_2, std::placeholders::_1, 100.1);
bindFunc2('B', 10);
cout << "=================================\n";
auto bindFunc3 = bind(TestFunc, std::placeholders::_2, std::placeholders::_3, std::placeholders::_1);
bindFunc3(100.1, 30, 'C');
return 0;
}
use g++ Execute after compilation , give the result as follows :
As you can see from the code above ,bind It can bind some parameters at the same time , Parameters that are not provided are represented by placeholders , Then pass in the actual parameter value at run time .PS: The bound parameters will be passed to the specific function in the way of value passing , Placeholders will be passed by reference . as everyone knows , Static member functions can actually be regarded as global functions , Non static member functions need to be passed this Pointer as the first parameter , therefore std::bind Can easily bind member functions .
Combining theory with practice
For my question , What is more useful is Non static member functions need to be passed this Pointer as the first parameter
Answer my questions :
m_api->SubFutureL1(
m_config.config.symbol[0].future,
std::bind(&ArbitrageFc::OnSymbol1FutureMd, this, std::placeholders::_1));
It can be disassembled as follows :
1、bind relevant :
OnFutureL1MdFunc callback = std::bind(&ArbitrageFc::OnSymbol1FutureMd,
this, std::placeholders::_1);
Finally, it is equivalent to the effect :
callback = this->OnSymbol1FutureMd(const FutureL1MarketData& var);
//OnSymbol1FutureMd The first parameter of , Namely callback The first parameter of
In function SubFutureL1 Implemented callback binding , Then when there is a callback , That's what's called :
OnSymbol1FutureMd(var);
//var Namely “FutureL1MarketData& md”
And this md How did it come about ? It involves
// Futures market
while (m_mm.GetFutureL1Seq() > future_l1_seq) {
auto md = m_mm.GetFutureL1Market(++future_l1_seq);
if (md) {
auto iter = m_future_l1_callback.find(md->symbol);
if (iter != m_future_l1_callback.end()) {
tlogi(" Futures market ")
<< utils::symbol::ToString(md->symbol) << " " << md->update_time
<< " " << md->bid_price[0] << " " << md->bid_volume[0]
<< " " << md->ask_price[0] << " " << md->ask_volume[0]
<< " " << md->last_price << " " << md->volume;
iter->second(*md);
}
}
}
Among them iter->second(*md); This callback is implemented ;
Business understanding
Let's talk about the process ( This is strongly related to business , And you also need other code contexts , I guess I can only make a record for myself ):
1、 Complete the binding of quotation callback in initialization ; Initialization is from multiple levels , Two aspects :
class StrategyEngineImp : public StrategyEngine–>
member :market::MarketManager& m_mm–>
member :
xytp::market::client::XyMarketClient* m_fm_master = nullptr; and
xytp::market::client::XyMarketClientCallback* m_fm_master_cb = nullptr;
because :
m_fm_master =
xytp::market::client::XyMarketClient::Create(m_config.future_master_lib.c_str());
also
xytp::market::client::XyMarketClient* Create() {
return &market::redis::RedisMarketFuture::Instance();
}
therefore :
m_fm_master It's actually a RedisMarketFuture Example , stay
MarketManager::InitMarket() In the call :m_fm_master->Init
That is to say bool RedisMarketFuture::Init(const std::string& config) in :
m_redis->SetSubCallback(std::bind(
&RedisMarketFuture::OnPub,
this,
std::placeholders::_1,
std::placeholders::_2))
So as to achieve Pub Callback binding for :
So when the market comes , The quotation gateway receives ? And then through Redis Release , At this time ,StrategyEngineImp The members of :market::MarketManager& m_mm Inside m_fm_master and m_fm_master_cb , It's going to come back m_fm_master_cb , That is to say cb Member functions in :OnFutureMarket, So as to analyze the transmitted market to m_fm_master_cb The members of MarketManager& m_mm Inside market::FutureL1MarketData* future_md = m_mm.GetFutureL1Market(seq),
The whole relationship :
StrategyEngineImp–》market::MarketManager& m_mm;–》FutureMarketCallback–》m_mm(MarketManager::Instance())–》market::FutureL1MarketData* future_md = m_mm.GetFutureL1Market(seq);
Above 2 Members of classes m_mm In fact, it refers to the same object instance 【market::MarketManager::Instance()】
2、 stay StrategyTradingEngine::MainThread in , When the market comes , Will be based on seq To judge and know , Then from the previous callback binding , Get the latest market , And then through iter->second(*md); To achieve market driven
More learning references
边栏推荐
- An article reveals the NFT strategy of traditional game manufacturers such as Ubisoft
- ACM warm-up Exercise 1 in 2022 summer vacation (summary)
- 洛谷P1441 砝码称重
- Today's code farmer girl learned notes about event operation and ref attribute, and did the practice of form two-way binding
- Gaussian elimination acwing 883. solving linear equations with Gaussian elimination
- IO stream_ Overview and explanation of data input and output flow
- 2022牛客多校 (3)J.Journey
- Non progressive phenomena of entropy and morphology
- Wenzhou University X kangaroo cloud: how to "know well" in the construction of higher talent education
- TensorFlow张量运算函数集
猜你喜欢

最长上升子序列模型 AcWing 1017. 怪盗基德的滑翔翼

背包模型 AcWing 1024. 装箱问题

数字三角形模型 AcWing 1027. 方格取数

A verification test of the relationship between iteration number and entropy

2022牛客多校 (3)J.Journey

高斯消元 AcWing 884. 高斯消元解异或线性方程组

栈 AcWing 3302. 表达式求值

ethereum rpc

最长上升子序列模型 AcWing 272. 最长公共上升子序列

Tree DP acwing 285. dance without boss
随机推荐
An article reveals the NFT strategy of traditional game manufacturers such as Ubisoft
Kepserver configuration
MySQL installation (RPM package)
I've compromised. Since everyone wants to call me Yelin, there's nothing I can do
力扣——10. 正则表达式匹配
Taishan Office Technology Lecture: scaling and opening files
数字三角形模型 AcWing 1027. 方格取数
7 row K with the weakest combat effectiveness in the matrix
状态压缩DP AcWing 91. 最短Hamilton路径
Game theory acwing 893. Set Nim game
Today's code farmer girl learned notes about event operation and ref attribute, and did the practice of form two-way binding
IO stream_ Overview and explanation of data input and output flow
The article will not keep VIP charges all the time. It will be open for a period of time
Students, don't copy all my code, remember to change it, or we both want G
Memory search acwing 901. Skiing
tensorflow运行报错解决方法
Backpack model acwing 1022. Collection of pet elves
Wechat push - template message parameter configuration
Interval problem acwing 906. Interval grouping
The second method of calculating overlapping integral