GetInstance

Contents

GetInstance#

template<typename F>
NamedInstance<F> YR::GetInstance(const std::string &name, const std::string &nameSpace = "", int timeoutSec = 60)#

Get instance associated with the specified name and nameSpace within a timeout.

class Counter {
public:
    int count;

    Counter() {}

    explicit Counter(int init) : count(init) {}

    static Counter *FactoryCreate(int init)
    {
        return new Counter(init);
    }

    int Counter::Add(int x)
    {
        count += x;
        return count;
    }
};

YR_INVOKE(Counter::FactoryCreate, &Counter::Add);

int main(void)
{
    YR::Config conf;
    YR::Init(conf);

    std::string name = "test-get-instance";
    auto ins = YR::Instance(Counter::FactoryCreate, name).Invoke(1);
    auto res = ins.Function(&Counter::Add).Invoke(1);

    std::string ns = "";
    auto insGet = YR::GetInstance<Counter>(name, ns, 60);
    auto resGet = insGet.Function(&Counter::Add).Invoke(1);
    return 0;
}

Template Parameters:

F – The type of the instance.

Parameters:
  • name – The instance name of the named instance.

  • nameSpace – Namespace of the named instance. The default value is "".

  • timeoutSec – The timeout in seconds. The default value is 60.

Throws:

Exception

  1. Name should not be empty, and an exception “name should not be empty” will be thrown.

  2. Timeout should be greater than 0, and an exception “timeout should be greater than 0” will be thrown.

Returns:

A named instance that can be used to call member functions of the class using the Function method.