当前位置:网站首页>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
边栏推荐
- Solve importerror: cannot import name'abs'import tensorflow error
- 【着色器实现Shake随机摇动效果_Shader效果第十篇】
- Yiwen counts NFT projects valued at more than US $100million
- 容斥原理 AcWing 890. 能被整除的数
- 最长上升子序列模型 AcWing 1010. 拦截导弹
- Find the combination number acwing 888. find the combination number IV
- 2022 Niuke multi school (3) j.journey
- What is the issuing price of NFT (Interpretation of NFT and establishment of NFT world outlook)
- Ten year structure five year life-07 young and vigorous transformation
- The influence of the number of non-zero values in the picture on Classification
猜你喜欢

Play with the cluster configuration center and learn about the Taier console

栈 AcWing 3302. 表达式求值

【着色器实现Shake随机摇动效果_Shader效果第十篇】

Influence of black and white pixel distribution on iteration times

Redis high availability principle
![Take you hand-in-hand to develop a complete classic game [Tetris] from scratch, with less than 200 lines of logic.](/img/7f/f42f9267cdff35522c260ef073bab9.png)
Take you hand-in-hand to develop a complete classic game [Tetris] from scratch, with less than 200 lines of logic.

Gaussian elimination acwing 883. solving linear equations with Gaussian elimination

Opengauss kernel analysis - statistics and row count estimation

How to build a data index system is the most effective. From 0 to 1, we will take you a quick start - 02 live review

博弈论 AcWing 892. 台阶-Nim游戏
随机推荐
Kepserver configuration
IO stream_ Overview and explanation of data input and output flow
什么是私域流量?
15th largest value of data flow
IO流_字符流、IO流小结、IO流案例总结
Where is the big data open source project, one-stop fully automated full life cycle operation and maintenance steward Chengying (background)?
最长上升子序列模型 AcWing 1017. 怪盗基德的滑翔翼
Memory search acwing 901. Skiing
11 wrong set
Learning notes - wechat payment
Pat (Grade B) 2022 summer exam
Yonbuilder enables innovation, and the "golden keyboard Award" of the fourth UFIDA developer competition is open!
Taishan Office Technology Lecture: scaling and opening files
A verification test of the relationship between iteration number and entropy
Longest ascending subsequence model acwing 1012. Sister Cities
Digital triangle model acwing 275. pass note
Learning notes uni app
Inclusion exclusion principle acwing 890. divisible numbers
Derivation of the detailed expansion sto overlap integrals
Why is the data service API the standard configuration of the data midrange when we take the last mile of the data midrange?