Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r73627 - sandbox/SOC/2011/checks/boost/checks
From: pierre.talbot.6114_at_[hidden]
Date: 2011-08-09 12:50:32


Author: trademark
Date: 2011-08-09 12:50:32 EDT (Tue, 09 Aug 2011)
New Revision: 73627
URL: http://svn.boost.org/trac/boost/changeset/73627

Log:
policy class for the luhn algorithm. For saving purpose. OLD NEED TO BE CHANGE.
Text files modified:
   sandbox/SOC/2011/checks/boost/checks/luhn.hpp | 56 ++++++++++++++++++++++++++++++++++++++++
   1 files changed, 56 insertions(+), 0 deletions(-)

Modified: sandbox/SOC/2011/checks/boost/checks/luhn.hpp
==============================================================================
--- sandbox/SOC/2011/checks/boost/checks/luhn.hpp (original)
+++ sandbox/SOC/2011/checks/boost/checks/luhn.hpp 2011-08-09 12:50:32 EDT (Tue, 09 Aug 2011)
@@ -8,10 +8,66 @@
 #ifndef BOOST_LUHN_INCLUDED
 #define BOOST_LUHN_INCLUDED
 
+#include <boost/checks/basic_checks.hpp>
 
 namespace boost {
     namespace checks{
 
+template <typename luhn_range, typename size_policy, typename checkdigit_policy>
+struct luhn_policy : mod_policy <weight_policy<1,2>, size_policy, checkdigit_policy, rightmost<luhn_range> >
+{
+ static const unsigned int modulus = 10;
+
+ template <typename character>
+ static unsigned int characterToDigit(const character& mod10_character)
+ {
+ return boost::lexical_cast<unsigned int>(mod10_character);
+ }
+ template <typename character>
+ static character compute_checkdigit( const unsigned int &sum )
+ {
+ return boost::lexical_cast<character>((10 - sum % 10 ) % 10) ;
+ }
+ static bool validate_checkdigit( const unsigned int &sum )
+ {
+ return !(sum % 10);
+ }
+
+ typedef unsigned int opdata_type ;
+
+ template <typename opdata_t>
+ static void init_opdata ( opdata_t &opdata){ }
+
+ template <typename weight_value, typename opdata_t>
+ static void sum_operation(unsigned int &sum, unsigned int &digit, opdata_t &opdata )
+ {
+ sum += (digit << (weight_value-1)) - 9 * ( digit << (weight_value-1) > 9) ;
+ }
+};
+
+template <size_t size_expected, typename luhn_range>
+bool check_luhn ( luhn_range luhn_number )
+{
+ return check_number<luhn_policy<luhn_range, mod_size<size_expected>, checkdigit_included > >( luhn_number ) ;
+}
+
+template <typename luhn_range>
+bool check_luhn ( luhn_range luhn_number )
+{
+ return check_number<luhn_policy<luhn_range, no_size_set , checkdigit_included > >( luhn_number ) ;
+}
+
+template <size_t size_expected, typename luhn_range>
+typename boost::range_value<luhn_range>::type compute_luhn ( luhn_range luhn_number )
+{
+ return compute_number<luhn_policy<luhn_range, mod_size<size_expected>, virtual_checkdigit> >( luhn_number ) ;
+}
+
+template <typename luhn_range>
+typename boost::range_value<luhn_range>::type compute_luhn ( luhn_range luhn_number )
+{
+ return compute_number<luhn_policy<luhn_range, no_size_set , virtual_checkdigit > >( luhn_number ) ;
+}
 
 } // namespace checks
 } // namespace boost


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk