Instance

Instance#

template<typename F>
std::enable_if_t<!std::is_base_of<internal::CrossLangClass, F>::value, YR::internal::InstanceCreator<F>> YR::Instance(F constructor)#

Create an InstanceCreator for constructing an instance of a class.

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

   auto counter = YR::Instance(Counter::FactoryCreate).Invoke(1);
   auto c = counter.Function(&Counter::Add).Invoke(1);
   std::cout << "counter is " << *YR::Get(c) << std::endl;

   return 0;
}

Template Parameters:

F – The class type that must not inherit from internal::CrossLangClass.

Parameters:

constructor – A function that constructs an instance of the class and returns a pointer to the constructed object.

Returns:

InstanceCreator<Cls>, A creator object that can be used to create the instance and specify additional options for the instance’s resources.

template<typename F>
std::enable_if_t<!std::is_base_of<internal::CrossLangClass, F>::value, YR::internal::InstanceCreator<F>> YR::Instance(F constructor, const std::string &name)#

Create an InstanceCreator for constructing an instance of a class.

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

   // The instance name of this named instance is name_1
   auto counter = YR::Instance(Counter::FactoryCreate, "name_1").Invoke(1);
   auto c = counter.Function(&Counter::Add).Invoke(1);
   std::cout << "counter is " << *YR::Get(c) << std::endl;

   // A handle to a named instance of name_1 will be generated, reusing the name_1 instance
   counter = YR::Instance(Counter::FactoryCreate, "name_1").Invoke(1);
   c = counter.Function(&Counter::Add).Invoke(1);
   std::cout << "counter is " << *YR::Get(c) << std::endl;

   return 0;
}

Template Parameters:

F – The class type that must not inherit from internal::CrossLangClass.

Parameters:
  • constructor – A function that constructs an instance of the class and returns a pointer to the constructed object.

  • name – Optional. The name of the named instance. If provided, the instance can be reused by this name.

Returns:

InstanceCreator<Cls>, A creator object that can be used to create the instance and specify additional options for the instance’s resources.

template<typename F>
std::enable_if_t<!std::is_base_of<internal::CrossLangClass, F>::value, YR::internal::InstanceCreator<F>> YR::Instance(F constructor, const std::string &name, const std::string &ns)#

Create an InstanceCreator for constructing an instance of a class.

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

    // The instance name of this named instance is ns_1-name_1
    auto counter = YR::Instance(Counter::FactoryCreate, "name_1", "ns_1").Invoke(1);
    auto c = counter.Function(&Counter::Add).Invoke(1);
    std::cout << "counter is " << *YR::Get(c) << std::endl;

    // A handle to the named instance ns_1-name_1 will be generated, and the ns_1-name_1 instance will be reused.
    counter = YR::Instance(Counter::FactoryCreate, "name_1", "ns_1").Invoke(1);
    c = counter.Function(&Counter::Add).Invoke(1);
    std::cout << "counter is " << *YR::Get(c) << std::endl;

    return 0;
}

Note

This function is designed for object-oriented programming scenarios. The InstanceCreator returned by this function can only execute member functions of the class constructed by the provided function cls. If name is passed, or both name and nameSpace are passed, the instance is a named instance, and the instance name is specified by the user. The instance can be reused by the instance name.

Template Parameters:

F – The class type that must not inherit from internal::CrossLangClass.

Parameters:
  • constructor – A function that constructs an instance of the class and returns a pointer to the constructed object.

  • name – Optional. The name of the named instance. If provided, the instance can be reused by this name.

  • ns – Optional. The namespace for the named instance. If both name and ns are provided, the instance name will be concatenated as ns-name.

Returns:

InstanceCreator<Cls>, A creator object that can be used to create the instance and specify additional options for the instance’s resources.