Skip to main content

Featured

Build docker image from multiple build contexts

Build docker image from multiple build contexts Building a docker image requires specifying a source of truth to include in the image from a local directory or a remote git repository. In the previous version, the docker BuildKit allows users to specify the build context from a single source of truth only. However, the engineers may need to have the context from different locations based on the type of files. For instance, icons, images or other resources that are not included in the same package, including the resource from other docker images. Fortunately, the Docker Buildx toolkit supports multiple build context flag for Docker 1.4. Let's learn how to use this new feature. The following list is a shortcut for jumping into a specific topic handy. What version of Docker is this tutorial targeting? How to specify the version of Dockerfile frontend? Ho

[C++, C#]class之間的callback

最近在工作上有看到比較好的方法,所以就在這邊記錄起來。
這邊的作法,就是利用繼承的方式,來讓callback的介面,可以傳遞到下一層的類別內。

[C++]
1.先建立要用來當通知器的介面
//用來Callback用的通知器
class INotifier
{
//要用來Callback的介面
    public:
        virtual void NoReturnFunc(void)  = 0;
        virtual bool BoolReturnFunc(void) = 0;
        virtual void PassValFunc(int Val) = 0;
}

2.建立要用來操作的類別
<head file>
class COperator
{ 
    private:
        INotifier mNotifyTarget;   //被通知的對象

    public:
        COperator(INotifier NotifyTarget); //建構子
}

2.1.這邊的範例是建立操作物件時,就順便指定被通知的對象。
<cpp>
COperator::COperator(INotifier NotifyTarget)
{
    this->mNotifyTarget = NotifyTarget;

    if( this->mNotifyTarget != NULL )
        this->mNotifyTarget->NoReturnFunc();
}

3.建立被通知的對象,被通知的對象需要繼承通知介面。
<head file>
class CReceiver : public INotifier
{
    private:
        COperator *mOperator;

    public:
        CReceiver(void);
  
//繼承來的介面,內容要自行實做
    public:
        virtual void NoReturnFunc(void);
        virtual bool BoolReturnFunc(void);
        virtual void PassValFunc(int Val);
}

3.1.這邊是在被通知對象被建立時,就把自己塞給操作物件。
<cpp>
CReceiver::CReceiver(void)
{
    this->mOperator = new COperator(this);
}

之後,COperator就可以透過INotifier所提供的介面,把事件回傳給CReceiver了。

[C#]
在C#底下,要做這件事情就變得比較簡單了。一開始要先在操作物件內先宣告要用來當事件的函式。
public class COperator
{
//使用委派宣告一個函式介面
    public delegate void OnNoReturnFuncEvent();
 
//宣告此介面為一個事件
    public event OnNoReturnFuncEvent NoReturnFuncNotifier = null;
  
//要做callback事件處理的函式
    void OperatingFunc()
    {
        if(this.NoReturnFuncNotifier != null)
            this.NoReturnFuncNotifier();
    }
}

之後就在要被通知的對象內,將自己要用來作回呼的函式,加到事件內。
這樣的作法,可以添加複數個事件,也就是可以有2個以上的物件將自己的回呼介面,加到這個事件內。
而當事件發生的時候,這些有加入事件的物件,都會依序收到同樣的事件通知。
public class Receiver
{
//宣告操作物件
    private COperator mOperator = new COperator();

    public Receiver()
    {
    //把自已的callback介面加到操作對象去
        this.mOperator.NoReturnFuncNotifier += this.OnNoReturnFucn;
    }

    //這邊的介面宣告要與COperator所宣告的委派函式介面一樣
    public void OnNoReturnFucn()
    {
        //做要做的事情
    }
}

Comments

Popular Posts