forked from libMesh/libmesh
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial infrastruture to support libMesh#64 -- better singleton support
- Loading branch information
Showing
8 changed files
with
221 additions
and
19 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// The libMesh Finite Element Library. | ||
// Copyright (C) 2002-2013 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner | ||
|
||
// This library is free software; you can redistribute it and/or | ||
// modify it under the terms of the GNU Lesser General Public | ||
// License as published by the Free Software Foundation; either | ||
// version 2.1 of the License, or (at your option) any later version. | ||
|
||
// This library 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 | ||
// Lesser General Public License for more details. | ||
|
||
// You should have received a copy of the GNU Lesser General Public | ||
// License along with this library; if not, write to the Free Software | ||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
|
||
|
||
|
||
#ifndef LIBMESH_LIBMESH_SINGLETON_H | ||
#define LIBMESH_LIBMESH_SINGLETON_H | ||
|
||
#include "libmesh/libmesh_common.h" | ||
|
||
namespace libMesh { | ||
|
||
/** | ||
* Base class for all library singleton objects. | ||
*/ | ||
class Singleton | ||
{ | ||
protected: | ||
|
||
/** | ||
* Constructor. Adds the derived object to the singleton cache list. | ||
*/ | ||
Singleton(); | ||
|
||
/** | ||
* Destructor. | ||
*/ | ||
virtual ~Singleton(); | ||
|
||
public: | ||
|
||
/** | ||
* Cleanup function. Removes all dynamically created \p Singleton | ||
* objects. | ||
*/ | ||
static void cleanup(); | ||
}; | ||
|
||
} // namespace libMesh | ||
|
||
#endif // LIBMESH_LIBMESH_SINGLETON_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// The libMesh Finite Element Library. | ||
// Copyright (C) 2002-2013 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner | ||
|
||
// This library is free software; you can redistribute it and/or | ||
// modify it under the terms of the GNU Lesser General Public | ||
// License as published by the Free Software Foundation; either | ||
// version 2.1 of the License, or (at your option) any later version. | ||
|
||
// This library 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 | ||
// Lesser General Public License for more details. | ||
|
||
// You should have received a copy of the GNU Lesser General Public | ||
// License along with this library; if not, write to the Free Software | ||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
|
||
|
||
|
||
// Local includes | ||
#include "libmesh/libmesh_singleton.h" | ||
#include "libmesh/threads.h" | ||
|
||
// C/C++ includes | ||
#include <vector> | ||
|
||
|
||
// -------------------------------------------------------- | ||
// Local anonymous namespace to hold miscelaneous bits | ||
namespace | ||
{ | ||
using namespace libMesh; | ||
|
||
// Mutex object for required locking | ||
typedef Threads::spin_mutex SingletonMutex; | ||
SingletonMutex singleton_mtx; | ||
|
||
// global list of runtime Singleton objects - created dynamically, | ||
// cleaned up in reverse order. | ||
typedef std::vector<Singleton*> SingletonList; | ||
SingletonList singleton_cache; | ||
|
||
} // end anonymous namespace | ||
|
||
|
||
|
||
// -------------------------------------------------------- | ||
// Local anonymous namespace to hold miscelaneous bits | ||
namespace libMesh | ||
{ | ||
|
||
Singleton::Singleton () | ||
{ | ||
SingletonMutex::scoped_lock lock(singleton_mtx); | ||
|
||
singleton_cache.push_back (this); | ||
} | ||
|
||
|
||
|
||
Singleton::~Singleton () | ||
{ | ||
} | ||
|
||
|
||
|
||
void Singleton::cleanup () | ||
{ | ||
SingletonMutex::scoped_lock lock(singleton_mtx); | ||
|
||
for (SingletonList::reverse_iterator it = singleton_cache.rbegin(); | ||
it!=singleton_cache.rend(); ++it) | ||
{ | ||
libmesh_assert (*it != NULL); | ||
delete *it; | ||
*it = NULL; | ||
} | ||
|
||
singleton_cache.clear(); | ||
} | ||
|
||
} // namespace libMesh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters