Sha256: 7ffda2aece5f50c95faf6c2190d836c81d4963f50e53dbdead9607779a2b3e8c

Contents?: true

Size: 1.59 KB

Versions: 16

Compression:

Stored size: 1.59 KB

Contents

// Copyright (c) 2018-2020 Dr. Colin Hirsch and Daniel Frey
// Please see LICENSE for license or visit https://github.com/taocpp/json/

#include <cstddef>
#include <limits>
#include <stdexcept>
#include <string>
#include <utility>

#include <tao/json/value.hpp>

#include <tao/json/events/discard.hpp>
#include <tao/json/events/from_value.hpp>

namespace tao::json
{
   template< typename Consumer, std::uint64_t Min, std::uint64_t Max >
   struct validate_integer
      : public Consumer
   {
      static_assert( Max >= Min );
      static_assert( Max <= std::uint64_t( ( std::numeric_limits< std::int64_t >::max )() ), "Max may not be larger than 2^63-1" );

      using Consumer::Consumer;

      void number( const std::int64_t v )
      {
         if( ( v < std::int64_t( Min ) ) || ( v > std::int64_t( Max ) ) ) {
            throw std::runtime_error( "integer range violated: " + std::to_string( v ) );
         }
         Consumer::number( v );
      }

      void number( const std::uint64_t v )
      {
         if( ( v < Min ) || ( v > Max ) ) {
            throw std::runtime_error( "unsigned range violated: " + std::to_string( v ) );
         }
         Consumer::number( v );
      }

      void number( const double v ) noexcept( noexcept( std::declval< Consumer >().number( v ) ) )
      {
         Consumer::number( v );
      }
   };

}  // namespace tao::json

int main( int /*unused*/, char** /*unused*/ )
{
   tao::json::value v = { { "a", 20 }, { "b", 30 } };
   tao::json::validate_integer< tao::json::events::discard, 10, 40 > consumer;
   tao::json::events::from_value( consumer, v );
   return 0;
}

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
couchbase-3.0.0.alpha.4-x86_64-linux ext/third_party/json/src/example/json/validate_integer.cpp
couchbase-3.0.0.alpha.4-x86_64-darwin-19 ext/third_party/json/src/example/json/validate_integer.cpp
couchbase-3.0.0.alpha.4-universal-darwin-19 ext/third_party/json/src/example/json/validate_integer.cpp
couchbase-3.0.0.alpha.4 ext/third_party/json/src/example/json/validate_integer.cpp
couchbase-3.0.0.alpha.3-x86_64-linux ext/third_party/json/src/example/json/validate_integer.cpp
couchbase-3.0.0.alpha.3-x86_64-darwin-19 ext/third_party/json/src/example/json/validate_integer.cpp
couchbase-3.0.0.alpha.3-universal-darwin-19 ext/third_party/json/src/example/json/validate_integer.cpp
couchbase-3.0.0.alpha.3 ext/third_party/json/src/example/json/validate_integer.cpp
couchbase-3.0.0.alpha.2-x86_64-linux ext/third_party/json/src/example/json/validate_integer.cpp
couchbase-3.0.0.alpha.2-x86_64-darwin-19 ext/third_party/json/src/example/json/validate_integer.cpp
couchbase-3.0.0.alpha.2-universal-darwin-19 ext/third_party/json/src/example/json/validate_integer.cpp
couchbase-3.0.0.alpha.2 ext/third_party/json/src/example/json/validate_integer.cpp
couchbase-3.0.0.alpha.1-x86_64-linux ext/third_party/json/src/example/json/validate_integer.cpp
couchbase-3.0.0.alpha.1-x86_64-darwin-19 ext/third_party/json/src/example/json/validate_integer.cpp
couchbase-3.0.0.alpha.1-universal-darwin-19 ext/third_party/json/src/example/json/validate_integer.cpp
couchbase-3.0.0.alpha.1 ext/third_party/json/src/example/json/validate_integer.cpp