From cd04d29584a704f3afceeed51249f28a0f464d83 Mon Sep 17 00:00:00 2001 From: groogy Date: Fri, 3 Dec 2010 13:44:59 +0000 Subject: [PATCH] Added a NonCopyable mixin-module which prohibits any attempt to copy instances of classes that includes this module. git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2@1737 4e206d99-4929-0410-ac5d-dfc041789085 --- .../ruby/sfml-system/system/NonCopyable.cpp | 65 +++++++++++++++++++ .../ruby/sfml-system/system/NonCopyable.hpp | 30 +++++++++ bindings/ruby/sfml-system/system/main.cpp | 2 + 3 files changed, 97 insertions(+) create mode 100644 bindings/ruby/sfml-system/system/NonCopyable.cpp create mode 100644 bindings/ruby/sfml-system/system/NonCopyable.hpp diff --git a/bindings/ruby/sfml-system/system/NonCopyable.cpp b/bindings/ruby/sfml-system/system/NonCopyable.cpp new file mode 100644 index 000000000..c3a4ef555 --- /dev/null +++ b/bindings/ruby/sfml-system/system/NonCopyable.cpp @@ -0,0 +1,65 @@ +/* rbSFML - Copyright (c) 2010 Henrik Valter Vogelius Hansson - groogy@groogy.se + * This software is provided 'as-is', without any express or + * implied warranty. In no event will the authors be held + * liable for any damages arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute + * it freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; + * you must not claim that you wrote the original software. + * If you use this software in a product, an acknowledgment + * in the product documentation would be appreciated but + * is not required. + * + * 2. Altered source versions must be plainly marked as such, + * and must not be misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any + * source distribution. + */ + +#include "NonCopyable.hpp" +#include "main.hpp" + +VALUE globalNonCopyableClass; + +/* call-seq: + * non_copyable.clone( source ) -> copy + * non_copyable.dup( source ) -> copy + * + * Override the copy methods so that we can't copy these instances.. + */ +static VALUE NonCopyable_InitializeCopy( VALUE self, VALUE aSource) +{ + rb_raise( rb_eRuntimeError, "The object you tried to copy is NonCopyable" ); + return self; +} + +void Init_NonCopyable( void ) +{ +/* SFML namespace which contains the classes of this module. */ + VALUE sfml = rb_define_module( "SFML" ); +/* Utility mixin-module that makes any derived class non-copyable. + * This module makes its instances non-copyable, by explicitely disabling its initialize_copy method. + * + * To create a non-copyable class, simply include the SFML::NonCopyable module. + * + * Usage example: + * + * class MyNonCopyableClass + * include SFML::NonCopyable + * ... + * end + + * Deciding whether the instances of a class can be copied or not is a very important design choice. You are strongly + * encouraged to think about it before writing a class, and to use SFML::NonCopyable when necessary to prevent many + * potential future errors when using it. This is also a very important indication to users of your class. + */ + globalNonCopyableModule = rb_define_module_under( sfml, "NonCopyable" ); + + // Instance methods + rb_define_method( globalNonCopyableModule, "initialize_copy", NonCopyable_Initialize, 1 ); + +} diff --git a/bindings/ruby/sfml-system/system/NonCopyable.hpp b/bindings/ruby/sfml-system/system/NonCopyable.hpp new file mode 100644 index 000000000..5f8166e50 --- /dev/null +++ b/bindings/ruby/sfml-system/system/NonCopyable.hpp @@ -0,0 +1,30 @@ +/* rbSFML - Copyright (c) 2010 Henrik Valter Vogelius Hansson - groogy@groogy.se + * This software is provided 'as-is', without any express or + * implied warranty. In no event will the authors be held + * liable for any damages arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute + * it freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; + * you must not claim that you wrote the original software. + * If you use this software in a product, an acknowledgment + * in the product documentation would be appreciated but + * is not required. + * + * 2. Altered source versions must be plainly marked as such, + * and must not be misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any + * source distribution. + */ + +#ifndef SFML_RUBYEXT_NON_COPYABLE_HEADER_ +#define SFML_RUBYEXT_NON_COPYABLE_HEADER_ + +#include "ruby.h" + +void Init_NonCopyable( void ); + +#endif // SFML_RUBYEXT_NON_COPYABLE_HEADER_ diff --git a/bindings/ruby/sfml-system/system/main.cpp b/bindings/ruby/sfml-system/system/main.cpp index c6ce1b67a..0439606f8 100644 --- a/bindings/ruby/sfml-system/system/main.cpp +++ b/bindings/ruby/sfml-system/system/main.cpp @@ -24,6 +24,7 @@ #include "Clock.hpp" #include "Vector2.hpp" #include "Vector3.hpp" +#include "NonCopyable.hpp" VALUE globalSFMLNamespace; @@ -46,4 +47,5 @@ void Init_system( void ) Init_Clock(); Init_Vector2(); Init_Vector3(); + Init_NonCopyable(); }