//
// Created by william on 2021/4/17.
//
#include "base.h"
using namespace std;
#define STR(T) #T
#define DEBUG_PRINT_THIS(T) \
T() \
{ \
printf("constructor of %s is called for (%p).\n", STR(T), this); \
} \
~T() \
{ \
printf("destructor of %s is called for (%p).\n", STR(T), this); \
}
/// SDK-G
// Gç代ç ï¼ç¼è¯æ¶ä¸ä¾èµä»»ä½Fçå
容
class G
{
// éè¿åè°å½æ°æ¥è°ç¨F䏿ä¾çåè½(ä¸å¼å
¥Fçæä»¶ãä¹ä¸äº§çç¼è¯ä¾èµ)
std::function<:string std::string name> m_readFileFunc;
public:
void setFunc(std::function<:string std::string name> func)
{
m_readFileFunc = func;
}
DEBUG_PRINT_THIS(G);
};
/// SDK-F
class FConfig
{
public:
// FConfigçä¸äºå
¬ç¨å½æ°(å¯è½æ¥èªäºå
¶ä»SDK)
std::function<:string std::string> _readFileFunc;
std::shared_ptr m_pG; // åºäºæäºéæ±ï¼éè¦éè¿FConfigæ¥ä¼ éGç对象ã
std::string readFile(const std::string& name)
{
assert(_readFileFunc);
return _readFileFunc(name);
}
DEBUG_PRINT_THIS(FConfig);
};
class F
{
public:
void createG(const std::shared_ptr& config)
{
assert(config.use_count() == 1); // å¼ç¨ä¼ éconfigï¼ä¸äº§çæ·è´ãå¼ç¨è®¡æ°ä¸æ´æ¹
m_pG = std::make_shared();
m_pG->setFunc(
[&config](const std::string& name)->std::string
{
assert(config);
return config->readFile(name);
}
);
assert(config.use_count() == 1);
}
void init()
{
std::shared_ptr config = std::make_shared();
// æ¤å¤çç¥å¯¹configçåå§å
assert(config.use_count() == 1);
createG(config);
assert(config.use_count() == 1);
config->m_pG = m_pG;
assert(m_pG.use_count() == 2);
config.reset();
assert(config == nullptr); // å®é
æªè°ç¨FConfigçææå½æ°ã
assert(m_pG.use_count() == 1); // pGå¼ç¨è®¡æ°åºè¯¥ä¸º1ãå®é
为2.
}
DEBUG_PRINT_THIS(F);
private:
std::shared_ptr m_pG;
};
/// APP
void lambdaErrorTest()
{
auto f = std::make_shared();
f->init();
f.reset(); // æ¤æ¶ï¼GåFConfigçææå½æ°æ²¡æè°ç¨
}
/*
以ä¸ä¸ºç¨åºè¾åº
constructor of F is called for (0x95de80).
constructor of FConfig is called for (0x95eec0).
constructor of G is called for (0x95ef10).
destructor of F is called for (0x95de80).
*/