nrtb-core team mailing list archive
-
nrtb-core team
-
Mailing list archive
-
Message #00196
[Branch ~fpstovall/nrtb/cpp_common] Rev 48: Ported ricklibIII::singleton to nrtb, and updated nrtb::confreader to use it.
Merge authors:
Rick Stovall (fpstovall)
------------------------------------------------------------
revno: 48 [merge]
committer: fpstovall@xxxxxxxxx
branch nick: dev
timestamp: Sat 2011-08-13 11:10:35 -0400
message:
Ported ricklibIII::singleton to nrtb, and updated nrtb::confreader to use it.
added:
common/singleton/
common/singleton/Makefile
common/singleton/singleton.h
common/singleton/singleton_test.cpp
modified:
common/Makefile
common/confreader/Makefile
common/confreader/confreader.cpp
common/confreader/confreader.h
common/confreader/conftest.cpp
--
lp:~fpstovall/nrtb/cpp_common
https://code.launchpad.net/~fpstovall/nrtb/cpp_common
Your team NRTB Core is subscribed to branch lp:~fpstovall/nrtb/cpp_common.
To unsubscribe from this branch go to https://code.launchpad.net/~fpstovall/nrtb/cpp_common/+edit-subscription
=== modified file 'common/Makefile'
--- common/Makefile 2011-08-10 00:48:42 +0000
+++ common/Makefile 2011-08-13 14:23:21 +0000
@@ -42,6 +42,7 @@
@cd threads; make ${action}
@cd sockets; make ${action}
@cd serializer; make ${action}
+ @cd singleton; make ${action}
@cd logger; make ${action}
@cd confreader; make ${action}
@cd GPB; make ${action}
=== modified file 'common/confreader/Makefile'
--- common/confreader/Makefile 2010-12-31 14:48:07 +0000
+++ common/confreader/Makefile 2011-08-13 15:06:08 +0000
@@ -24,20 +24,14 @@
@cp -v confreader.o ../obj
@echo build complete
-../include/common.h:
- @cd ../common_rl; make lib
-
-../include/log_setup.h:
- @cd ../logger; make lib
-
-confreader.o: confreader.h confreader.cpp Makefile ../include/common.h ../include/log_setup.h
+confreader.o: confreader.h confreader.cpp Makefile
@rm -f confreader.o
- g++ -c confreader.cpp -I ../include
+ g++ -c confreader.cpp -I../include
conftest: confreader.o conftest.cpp
@rm -f conftest
- g++ -c conftest.cpp
- g++ -o conftest conftest.o confreader.o ../obj/common.o ../obj/log_setup.o -lPocoFoundation -lPocoUtil
+ g++ -c conftest.cpp -I../include
+ g++ -o conftest conftest.o confreader.o ../obj/common.o ../obj/log_setup.o ../obj/base_thread.o -lpthread -lPocoFoundation -lPocoUtil
clean:
@rm -rvf *.o conftest ../include/confreader.h ../obj/confreader.o conf_test.log
=== modified file 'common/confreader/confreader.cpp'
--- common/confreader/confreader.cpp 2010-12-27 16:31:29 +0000
+++ common/confreader/confreader.cpp 2011-08-13 15:06:08 +0000
@@ -29,7 +29,6 @@
const std::string logname = "conf_reader";
-
namespace nrtb
{
=== modified file 'common/confreader/confreader.h'
--- common/confreader/confreader.h 2010-12-27 02:01:26 +0000
+++ common/confreader/confreader.h 2011-08-13 15:06:08 +0000
@@ -22,10 +22,10 @@
#include <string.h>
#include <map>
#include <vector>
+#include <string>
#include <fstream>
#include <boost/lexical_cast.hpp>
-#include <string.h>
-#include "Poco/SingletonHolder.h"
+#include <singleton.h>
namespace nrtb
{
@@ -99,14 +99,6 @@
/** NOP virtual destructor to allow safe inheritance.
**/
virtual ~conf_reader();
- /** Static singletion access point. Unlike common practice, this
- ** returns a reference to avoid anyone else taking control of a pointer.
- **/
- static conf_reader & get_instance()
- {
- static Poco::SingletonHolder<conf_reader> sh;
- return * sh.get();
- }
/** Reads and parses the command line and the provided file.
**
** Returns the number of values stored. argc and argv are, of
@@ -228,6 +220,8 @@
T get(const std::string & key, const T & def);
};
+typedef singleton<conf_reader> global_conf_reader;
+
template < class T >
typename std::vector<T> conf_reader::getall(const std::string & key)
{
=== modified file 'common/confreader/conftest.cpp'
--- common/confreader/conftest.cpp 2010-12-31 14:37:14 +0000
+++ common/confreader/conftest.cpp 2011-08-13 15:06:08 +0000
@@ -19,11 +19,11 @@
/* confreader test program */
#include "confreader.h"
-#include "../include/log_setup.h"
+#include <log_setup.h>
#include <iostream>
#include "Poco/Logger.h"
#include "Poco/SimpleFileChannel.h"
-#include "Poco/AutoPtr.h"
+//#include "Poco/AutoPtr.h"
using namespace nrtb;
using namespace std;
@@ -34,7 +34,7 @@
setup_global_logging("conf_test.log");
Poco::Logger & log = Poco::Logger::get("conftest");
log.information("=-=-=-=-=-= conftest Init =-=-=-=-=-=-=");
- conf_reader & config = conf_reader::get_instance();
+ global_conf_reader & config = global_conf_reader::get_instance();
try
{
log.information("Starting read");
=== added directory 'common/singleton'
=== added file 'common/singleton/Makefile'
--- common/singleton/Makefile 1970-01-01 00:00:00 +0000
+++ common/singleton/Makefile 2011-08-13 14:19:56 +0000
@@ -0,0 +1,33 @@
+#***********************************************
+# This file is part of the NRTB project (https://launchpad.net/nrtb).
+#
+# NRTB 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.
+#
+# NRTB 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 NRTB. If not, see <http://www.gnu.org/licenses/>.
+#
+#***********************************************
+
+
+lib: singleton_test
+ @./singleton_test
+ @cp -v singleton.h ../include
+ @echo build complete
+
+singleton_test: singleton_test.cpp Makefile
+ @rm -f singleton_test
+ g++ -c singleton_test.cpp -I ../include
+ g++ -o singleton_test singleton_test.o ../obj/base_thread.o ../obj/common.o ../obj/serializer.o -lpthread
+
+clean:
+ @rm -rvf *.o singleton_test ../include/singleton.h
+ @echo all objects and executables have been erased.
+
=== added file 'common/singleton/singleton.h'
--- common/singleton/singleton.h 1970-01-01 00:00:00 +0000
+++ common/singleton/singleton.h 2011-08-13 13:13:25 +0000
@@ -0,0 +1,123 @@
+/***********************************************
+ This file is part of the NRTB project (https://*launchpad.net/nrtb).
+
+ NRTB 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.
+
+ NRTB 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 NRTB. If not, see <http://www.gnu.org/licenses/>.
+
+ **********************************************/
+
+/* implements a template that takes any class provided and
+ * creates a singleton for it.
+ *
+ * fps 2002-5-29
+ */
+
+#ifndef nrtb_singleton_h
+#define nrtb_singleton_h
+
+#include <base_thread.h>
+
+namespace nrtb
+{
+
+/** Wrapper template to create singleton classes.
+ **
+ ** Classic Singleton pattern template, with thread-safe
+ ** methods for getting an instance and destruction.
+ **
+ ** Singletons are classes that:
+ ** (1) Insure that there are no more than one instance of the
+ ** class at any time.
+ ** (2) Allocate themselves automagically when accessed for the
+ ** first time.
+ ** (3) Provide a globally available access method to allow any
+ ** scope to gain access and use the instance.
+ **
+ ** This template can create a singleton version of any class that has a
+ ** no-argument constructor. The resulting class will have the same
+ ** inteface as the object used to create the template with the constructor
+ ** and destructor made protected and the addition of the get_instance()
+ ** and delete_me() methods.
+ **
+ ** See the documentation on the get_instance() and delete_me() methods for
+ ** usage.
+ **/
+template <class T, int mytag=0>
+class singleton: public T
+{
+ private:
+ static mutex __mylock;
+ static singleton * __me;
+ protected:
+ singleton() : T() {};
+ virtual ~singleton() {};
+ singleton(const singleton &) {};
+ public:
+ /** Used to access the object.
+ **
+ ** Returns a reference to the instanciated singleton object. If
+ ** the object has not been accessed before, the object will be
+ ** instanciated automatically.
+ **
+ ** As is usual for the method that provides access to a singleton,
+ ** this method is static, allowing it to be called via the class
+ ** name as shown. Remember you can only assign the return value to
+ ** a reference:
+ **
+ ** mytype & a = mytype::get_instance();
+ **
+ ** Attempts to make a copy of the instance should throw a compiler
+ ** error.
+ **
+ ** This method is thread safe, but that does not imply that the
+ ** class returned is thread-safe overall; that would depend on the
+ ** implementation of the class used to instaciate the template.
+ **/
+ static singleton & get_instance()
+ {
+ // First test avoids expensive mutex cycle
+ // if the object is already allocated.
+ if (!__me)
+ {
+ scope_lock lock(__mylock);
+ // second test required in case multiple threads
+ // get past the first check.
+ if (!__me)
+ {
+ __me = new singleton;
+ };
+ };
+ return *__me;
+ };
+
+ /** Destructs and deallocates the singleton object.
+ **
+ ** After a call to this method, the singleton object will
+ ** be destructed and deallocated from memory. However, it
+ ** will be automatically reconstruted and allocated if
+ ** get_instance() called at any time afterword.
+ **/
+ void delete_me()
+ {
+ scope_lock lock(__mylock);
+ if (__me) delete __me;
+ __me = 0;
+ };
+};
+
+template <class T, int mytag> mutex singleton<T,mytag>::__mylock;
+template <class T, int mytag> singleton<T,mytag> * singleton<T,mytag>::__me = 0;
+
+} // namespace nrtb;
+
+#endif // nrtb_singleton_h
=== added file 'common/singleton/singleton_test.cpp'
--- common/singleton/singleton_test.cpp 1970-01-01 00:00:00 +0000
+++ common/singleton/singleton_test.cpp 2011-08-13 14:19:56 +0000
@@ -0,0 +1,55 @@
+/***********************************************
+ This file is part of the NRTB project (https://*launchpad.net/nrtb).
+
+ NRTB 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.
+
+ NRTB 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 NRTB. If not, see <http://www.gnu.org/licenses/>.
+
+ **********************************************/
+
+// singleton template test program
+
+#include "singleton.h"
+#include <serializer.h>
+#include <iostream>
+
+using namespace nrtb;
+using namespace std;
+
+typedef singleton<serializer> sequence_type;
+
+int main()
+{
+
+ cout << "============== singleton unit test ================"
+ << endl;
+ int er_count = 0;
+
+ sequence_type & a = sequence_type::get_instance();
+
+ for (int i=0; i<10; i++)
+ {
+ cout << a();
+ };
+
+ sequence_type & b = sequence_type::get_instance();
+
+ if ( b() != 10)
+ {
+ er_count++;
+ };
+
+ cout << "\n=========== singleton test " << (er_count ? "failed" : "passed")
+ << " =============" << endl;
+
+ return er_count;
+};