singleton.hpp
Go to the documentation of this file.
1 //==============================================================================
2 //
3 // This file is part of GNSSTk, the ARL:UT GNSS Toolkit.
4 //
5 // The GNSSTk is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published
7 // by the Free Software Foundation; either version 3.0 of the License, or
8 // any later version.
9 //
10 // The GNSSTk is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with GNSSTk; if not, write to the Free Software Foundation,
17 // Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
18 //
19 // This software was developed by Applied Research Laboratories at the
20 // University of Texas at Austin.
21 // Copyright 2004-2022, The Board of Regents of The University of Texas System
22 //
23 //==============================================================================
24 
25 //==============================================================================
26 //
27 // This software was developed by Applied Research Laboratories at the
28 // University of Texas at Austin, under contract to an agency or agencies
29 // within the U.S. Department of Defense. The U.S. Government retains all
30 // rights to use, duplicate, distribute, disclose, or release this software.
31 //
32 // Pursuant to DoD Directive 523024
33 //
34 // DISTRIBUTION STATEMENT A: This software has been approved for public
35 // release, distribution is unlimited.
36 //
37 //==============================================================================
38 
39 #ifndef SINGLETON_TEMPLATE_INCLUDE
40 #define SINGLETON_TEMPLATE_INCLUDE
41 
44 namespace gnsstk
45 {
46 template <class T> class Singleton {
47 public:
48  static T& Instance() {
49  static T theInstance;
50  return theInstance;
51  }
52 protected:
53  Singleton() {} // c'tor protected
54  virtual ~Singleton() {} // d'tor virtual and protected
55 private:
56  Singleton(Singleton const&); // copy c'tor prohibited
57  Singleton& operator=(Singleton const&); // operator= prohibited
58 };
59 }
60 #endif //SINGLETON_TEMPLATE_INCLUDE
61 
62 /* use this class like this:
63 
64 // main.cpp cf. Docs/Cpp/singleton.pdf
65 #include <iostream>
66 #include <iomanip>
67 #include "singleton.hpp"
68 
69 class MyClass : public Singleton<MyClass> { // Curiously Recurring Template Pattern
70 public:
71  void set(int n) { x = n; } // these are required only if data is private
72  int get() const { return x; } // and you wish to control access.
73  double y; // public data is fine.
74 protected:
75  // MyClass must have empty c'tor; there are
76  // ways to mod this - see s'ton articles.
77  MyClass() : x(0),y(3.14159) { } // set defaults here
78  friend class Singleton<MyClass>; // so Instance() can call MyClass c'tor
79 private:
80  int x; // private data is fine.
81 };
82 
83 int main(void) {
84  // 'get' the singleton; this may appear anywhere MyClass is defined.
85  MyClass& CFG=MyClass::Instance();
86 
87  std::cout << "x = " << CFG.get() << " y = " << CFG.y << std::endl;
88  std::cout << "set x to 17 and y *= 2" << std::endl;
89  CFG.set(17);
90  CFG.y *= 2.0;
91  std::cout << "x = " << CFG.get() << " y = " << CFG.y << std::endl;
92 
93  std::cout << "Get a new object" << std::endl;
94  MyClass& newCFG=MyClass::Instance();
95  std::cout << "new x = " << newCFG.get() << " y = " << newCFG.y << std::endl;
96  std::cout << "set old x to 23 and new y to 37." << std::endl;
97  CFG.set(23);
98  newCFG.y = 37.;
99  std::cout << "old x = " << CFG.get() << " y = " << CFG.y << std::endl;
100  std::cout << "new x = " << newCFG.get() << " y = " << newCFG.y << std::endl;
101 
102  return 0;
103 }
104 */
gnsstk::Singleton::Instance
static T & Instance()
Definition: singleton.hpp:48
gnsstk::Singleton
Definition: singleton.hpp:46
gnsstk::Singleton::operator=
Singleton & operator=(Singleton const &)
gnsstk
For Sinex::InputHistory.
Definition: BasicFramework.cpp:50
gnsstk::Singleton::~Singleton
virtual ~Singleton()
Definition: singleton.hpp:54
gnsstk::Singleton::Singleton
Singleton()
Definition: singleton.hpp:53


gnsstk
Author(s):
autogenerated on Wed Oct 25 2023 02:40:41