Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r73837 - sandbox/SOC/2011/checks/boost/checks
From: pierre.talbot.6114_at_[hidden]
Date: 2011-08-16 20:19:04


Author: trademark
Date: 2011-08-16 20:19:03 EDT (Tue, 16 Aug 2011)
New Revision: 73837
URL: http://svn.boost.org/trac/boost/changeset/73837

Log:
Add algorithm and usage functions.
Text files modified:
   sandbox/SOC/2011/checks/boost/checks/modulus97.hpp | 76 +++++++++++++++++++++++++++++++++++----
   1 files changed, 67 insertions(+), 9 deletions(-)

Modified: sandbox/SOC/2011/checks/boost/checks/modulus97.hpp
==============================================================================
--- sandbox/SOC/2011/checks/boost/checks/modulus97.hpp (original)
+++ sandbox/SOC/2011/checks/boost/checks/modulus97.hpp 2011-08-16 20:19:03 EDT (Tue, 16 Aug 2011)
@@ -9,29 +9,87 @@
 #define BOOST_CHECKS_MOD97_HPP
 
 #include <boost/preprocessor/repetition.hpp>
+#include <boost/lexical_cast.hpp>
+#include <boost/checks/traduction_exception.hpp>
 #include <boost/checks/weight.hpp>
+#include <boost/checks/iteration_sense.hpp>
+#include <boost/checks/weighted_sum.hpp>
 
 namespace boost{
   namespace checks{
 
-
+template <typename mod97_weight, typename iteration_sense, unsigned int number_of_virtual_value_skipped = 0>
+struct modulus97_algorithm : boost::checks::weighted_sum_algorithm<mod97_weight, iteration_sense, number_of_virtual_value_skipped>
+{
+ static bool validate_checksum(int checksum)
+ {
+ return checksum % 97 == 1 ;
+ }
+
+ template <typename checkdigits_iter>
+ static typename checkdigits_iter compute_multicheckdigit( int checksum, checkdigits_iter mod97_checkdigits )
+ {
+ unsigned int checkdigits = 98 - (checksum % 97) ;
+
+ try{
+ *mod97_checkdigits = boost::lexical_cast<checkdigits_iter::value_type>(checkdigits / 10) ;
+ ++mod97_checkdigits;
+ *mod97_checkdigits = boost::lexical_cast<checkdigits_iter::value_type>(checkdigits % 10) ;
+ ++mod97_checkdigits;
+ }
+ catch( boost::bad_lexical_cast ){
+ throw boost::checks::traduction_exception() ;
+ }
+ return mod97_checkdigits;
+ }
+};
+
+// cf. ISO/IEC 7064:2003 for the mod97-10 algorithm.
 template <unsigned int weight_value>
-struct _mod97_weight
+struct make_mod97_weight
 {
   static const unsigned int value = weight_value ;
- typedef _mod97_weight<weight_value * 10 % 97> next ;
+ typedef make_mod97_weight<weight_value * 10 % 97> next ;
 };
 template<>
-struct _mod97_weight<68>
+struct make_mod97_weight<68>
 {
   static const unsigned int value = 68 ;
- typedef _mod97_weight type ;
+ typedef make_mod97_weight type ;
 };
+#define NEXT(z,n,unused) next::
+#define MOD97_make_weight(z, n ,unused) make_mod97_weight<1>::BOOST_PP_REPEAT(n, NEXT, ~)value
+typedef boost::checks::weight< BOOST_PP_ENUM(96, MOD97_make_weight, ~) > mod97_10_weight ;
 
-#define _next(z,n,unused) next::
-#define _MOD97_make_weight(z, n ,unused) _mod97_weight<1>::BOOST_PP_REPEAT(n, _next, ~)value
-typedef boost::checks::weight< BOOST_PP_ENUM(96, _MOD97_make_weight, ~) > mod97_weight ;
+typedef boost::checks::rightmost mod97_10_sense ;
 
-}}
+typedef modulus97_algorithm< mod97_10_weight, mod97_10_sense, 0 > mod97_10_check_algorithm ;
+typedef modulus97_algorithm< mod97_10_weight, mod97_10_sense, 2 > mod97_10_compute_algorithm ;
+
+template <size_t size_expected, typename check_range>
+bool check_mod97_10 (const check_range& check_seq)
+{
+ return boost::checks::check_sequence<mod97_10_check_algorithm, size_expected> ( check_seq ) ;
+}
+
+template <typename check_range>
+bool check_mod97_10 (const check_range& check_seq)
+{
+ return boost::checks::check_sequence<mod97_10_check_algorithm> ( check_seq ) ;
+}
+
+template <size_t size_expected, typename check_range, typename checkdigits_iter>
+typename checkdigits_iter compute_mod97_10 (const check_range& check_seq, checkdigits_iter mod97_checkdigits)
+{
+ return boost::checks::compute_multicheckdigit<mod97_10_compute_algorithm, size_expected> ( check_seq, mod97_checkdigits ) ;
+}
 
+template <typename check_range, typename checkdigits_iter>
+typename checkdigits_iter compute_mod97_10 (const check_range& check_seq, checkdigits_iter mod97_checkdigits)
+{
+ return boost::checks::compute_multicheckdigit<mod97_10_compute_algorithm> ( check_seq, mod97_checkdigits ) ;
+}
+
+
+}}
 #endif //BOOST_CHECKS_MOD97_HPP
\ No newline at end of file


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