Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) Lazy_init

 

STLQt CreatorLubuntu

 

Lazy_init is a class

with lazy initialization.

Technical facts

 

 

 

 

 

 

./CppLazy_init/CppLazy_init.pri

 

INCLUDEPATH += \
    ../../Classes/CppLazy_init

SOURCES += \
    ../../Classes/CppLazy_init/lazy_init.cpp

HEADERS  += \
    ../../Classes/CppLazy_init/lazy_init.h

OTHER_FILES += \
    ../../Classes/CppLazy_init/Licence.txt

 

 

 

 

 

./CppLazy_init/lazy_init.h

 

//---------------------------------------------------------------------------
/*
Lazy_init. Class to postpone constructor call upon first use.
Copyright (C) 2012-2015 Richel Bilderbeek

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppLazy_init.htm
//---------------------------------------------------------------------------
#ifndef LAZY_INIT_H
#define LAZY_INIT_H

#include <string>
#include <vector>

#include <boost/scoped_ptr.hpp>

namespace ribi {

/*
//How to do this with variadic function templates???
template <class T, typename ... Args> struct LazyInit
{
  LazyInit(const Args ... args)
    : m_args(args)
  {

  }
  const T& Get() const
  {
    if(!m_data)
    {
      m_data.reset(new T(m_args));
    }
    return *m_data;
  }

  private:
  Args ... m_args;
  mutable boost::scoped_ptr<T> m_data;
};
*/

template <class T> struct LazyInit0
{
  LazyInit0() : m_data{} {}
  const T& Get() const
  {
    if(!m_data)
    {
      m_data.reset(new T);
    }
    return *m_data;
  }

  private:
  mutable boost::scoped_ptr<T> m_data;
};

template <class T, typename Arg0> struct LazyInit1
{
  LazyInit1(const Arg0 args)
    : m_args{args},
      m_data{}
  {

  }
  const T& Get() const
  {
    if(!m_data)
    {
      m_data.reset(new T(m_args));
    }
    return *m_data;
  }

  private:
  mutable Arg0 m_args;
  mutable boost::scoped_ptr<T> m_data;
};

template <class T, typename Arg0, typename Arg1> struct LazyInit2
{
  LazyInit2(const Arg0 arg0, const Arg1 arg1)
    : m_arg0{arg0},
      m_arg1{arg1},
      m_data{}
  {

  }
  const T& Get() const
  {
    if(!m_data)
    {
      m_data.reset(new T(m_arg0,m_arg1));
    }
    return *m_data;
  }

  private:
  mutable Arg0 m_arg0;
  mutable Arg1 m_arg1;
  mutable boost::scoped_ptr<T> m_data;
};

struct Lazy_initVersion
{
  ///Obtain the version of this class
  static std::string GetVersion() noexcept;

  ///Obtain the version history of this class
  static std::vector<std::string> GetVersionHistory() noexcept;
};

} //~namespace ribi

#endif // LAZY_INIT_H

 

 

 

 

 

./CppLazy_init/lazy_init.cpp

 

//---------------------------------------------------------------------------
/*
Lazy_init. Class to postpone constructor call upon first use.
Copyright (C) 2012-2015 Richel Bilderbeek

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppLazy_init.htm
//---------------------------------------------------------------------------
#include "lazy_init.h"

std::string ribi::Lazy_initVersion::GetVersion() noexcept
{
  return "1.0";
}

std::vector<std::string> ribi::Lazy_initVersion::GetVersionHistory() noexcept
{
  return {
    "2012-02-11: version 1.0: initial version"
  };
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml