Example #1
0
 void
 set( shared_ptr<error_info_base> const & x, type_info_ const & typeid_ )
     {
     NDNBOOST_ASSERT(x);
     info_[typeid_] = x;
     diagnostic_info_str_.clear();
     }
Example #2
0
void rational<IntType>::normalize()
{
    // Avoid repeated construction
    IntType zero(0);

    if (den == zero)
        throw bad_rational();

    // Handle the case of zero separately, to avoid division by zero
    if (num == zero) {
        den = IntType(1);
        return;
    }

    IntType g = math::gcd(num, den);

    num /= g;
    den /= g;

    // Ensure that the denominator is positive
    if (den < zero) {
        num = -num;
        den = -den;
    }

    NDNBOOST_ASSERT( this->test_invariant() );
}
 std::streamsize write(Sink& snk, const char_type* s, std::streamsize n)
 {
     std::streamsize result = iostreams::write(snk, s, n);
     std::streamsize result2 = iostreams::write(this->component(), s, result);
     (void) result2; // Suppress 'unused variable' warning.
     NDNBOOST_ASSERT(result == result2);
     return result;
 }
 std::streamsize read(Source& src, char_type* s, std::streamsize n)
 {
     std::streamsize result = iostreams::read(src, s, n);
     if (result != -1) {
         std::streamsize result2 = iostreams::write(this->component(), s, result);
         (void) result2; // Suppress 'unused variable' warning.
         NDNBOOST_ASSERT(result == result2);
     }
     return result;
 }
 std::streamsize write(const char_type* s, std::streamsize n)
 {
     NDNBOOST_STATIC_ASSERT((
         is_convertible<
             NDNBOOST_DEDUCED_TYPENAME iostreams::category_of<Device>::type, output
         >::value
     ));
     std::streamsize result1 = iostreams::write(dev_, s, n);
     std::streamsize result2 = iostreams::write(sink_, s, n);
     (void) result1; // Suppress 'unused variable' warning.
     (void) result2;
     NDNBOOST_ASSERT(result1 == n && result2 == n);
     return n;
 }
Example #6
0
            shared_ptr<error_info_base>
            get( type_info_ const & ti ) const
                {
                error_info_map::const_iterator i=info_.find(ti);
                if( info_.end()!=i )
                    {
                    shared_ptr<error_info_base> const & p = i->second;
#ifndef NDNBOOST_NO_RTTI
                    NDNBOOST_ASSERT( *NDNBOOST_EXCEPTION_DYNAMIC_TYPEID(*p).type_==*ti.type_ );
#endif
                    return p;
                    }
                return shared_ptr<error_info_base>();
                }
 inline void* align(std::size_t alignment, std::size_t size,
     void*& ptr, std::size_t& space)
 {
     NDNBOOST_ASSERT(detail::is_alignment(alignment));
     std::size_t n = detail::address_t(ptr) & (alignment - 1);
     if (n != 0) {
         n = alignment - n;
     }
     void* p = 0;
     if (n <= space && size <= space - n) {
         p = static_cast<char*>(ptr) + n;
         ptr = p;
         space -= n;
     }
     return p;
 }
Example #8
0
bool rational<IntType>::operator< (param_type i) const
{
    // Avoid repeated construction
    int_type const  zero( 0 );

    // Break value into mixed-fraction form, w/ always-nonnegative remainder
    NDNBOOST_ASSERT( this->den > zero );
    int_type  q = this->num / this->den, r = this->num % this->den;
    while ( r < zero )  { r += this->den; --q; }

    // Compare with just the quotient, since the remainder always bumps the
    // value up.  [Since q = floor(n/d), and if n/d < i then q < i, if n/d == i
    // then q == i, if n/d == i + r/d then q == i, and if n/d >= i + 1 then
    // q >= i + 1 > i; therefore n/d < i iff q < i.]
    return q < i;
}
 std::streamsize read(char_type* s, std::streamsize n)
 {
     NDNBOOST_STATIC_ASSERT((
         is_convertible<
             NDNBOOST_DEDUCED_TYPENAME iostreams::category_of<Device>::type, input
         >::value
     ));
     std::streamsize result1 = iostreams::read(dev_, s, n);
     if (result1 != -1) {
         std::streamsize result2 = iostreams::write(sink_, s, result1);
         (void) result1; // Suppress 'unused variable' warning.
         (void) result2;
         NDNBOOST_ASSERT(result1 == result2);
     }
     return result1;
 }
Example #10
0
    bool operator()(InputIterator& next, InputIterator end, Token& tok)
    {
      typedef tokenizer_detail::assign_or_plus_equal<
        NDNBOOST_DEDUCED_TYPENAME tokenizer_detail::get_iterator_category<
          InputIterator
        >::iterator_category
      > assigner;

      NDNBOOST_ASSERT(!offsets_.empty());

      assigner::clear(tok);
      InputIterator start(next);

      if (next == end)
        return false;

      if (current_offset_ == offsets_.size())
      {
        if (wrap_offsets_)
          current_offset_=0;
        else
          return false;
      }

      int c = offsets_[current_offset_];
      int i = 0;
      for (; i < c; ++i) {
        if (next == end)break;
        assigner::plus_equal(tok,*next++);
      }
      assigner::assign(start,next,tok);

      if (!return_partial_last_)
        if (i < (c-1) )
          return false;

      ++current_offset_;
      return true;
    }
        inline
        char const *
        get_diagnostic_information( exception const & x, char const * header )
            {
#ifndef NDNBOOST_NO_EXCEPTIONS
            try
                {
#endif
                error_info_container * c=x.data_.get();
                if( !c )
                    x.data_.adopt(c=new exception_detail::error_info_container_impl);
                char const * di=c->diagnostic_information(header);
                NDNBOOST_ASSERT(di!=0);
                return di;
#ifndef NDNBOOST_NO_EXCEPTIONS
                }
            catch(...)
                {
                return 0;
                }
#endif
            }
Example #12
0
ndnboost::shared_ptr<Object const> object_cache<Key, Object>::do_get(const Key& k, size_type l_max_cache_size)
{
   typedef typename object_cache<Key, Object>::data object_data;
   typedef typename map_type::size_type map_size_type;
   static object_data s_data;

   //
   // see if the object is already in the cache:
   //
   map_iterator mpos = s_data.index.find(k);
   if(mpos != s_data.index.end())
   {
      //
      // Eureka! 
      // We have a cached item, bump it up the list and return it:
      //
      if(--(s_data.cont.end()) != mpos->second)
      {
         // splice out the item we want to move:
         list_type temp;
         temp.splice(temp.end(), s_data.cont, mpos->second);
         // and now place it at the end of the list:
         s_data.cont.splice(s_data.cont.end(), temp, temp.begin());
         NDNBOOST_ASSERT(*(s_data.cont.back().second) == k);
         // update index with new position:
         mpos->second = --(s_data.cont.end());
         NDNBOOST_ASSERT(&(mpos->first) == mpos->second->second);
         NDNBOOST_ASSERT(&(mpos->first) == s_data.cont.back().second);
      }
      return s_data.cont.back().first;
   }
   //
   // if we get here then the item is not in the cache,
   // so create it:
   //
   ndnboost::shared_ptr<Object const> result(new Object(k));
   //
   // Add it to the list, and index it:
   //
   s_data.cont.push_back(value_type(result, static_cast<Key const*>(0)));
   s_data.index.insert(std::make_pair(k, --(s_data.cont.end())));
   s_data.cont.back().second = &(s_data.index.find(k)->first);
   map_size_type s = s_data.index.size();
   NDNBOOST_ASSERT(s_data.index[k]->first.get() == result.get());
   NDNBOOST_ASSERT(&(s_data.index.find(k)->first) == s_data.cont.back().second);
   NDNBOOST_ASSERT(s_data.index.find(k)->first == k);
   if(s > l_max_cache_size)
   {
      //
      // We have too many items in the list, so we need to start
      // popping them off the back of the list, but only if they're
      // being held uniquely by us:
      //
      list_iterator pos = s_data.cont.begin();
      list_iterator last = s_data.cont.end();
      while((pos != last) && (s > l_max_cache_size))
      {
         if(pos->first.unique())
         {
            list_iterator condemmed(pos);
            ++pos;
            // now remove the items from our containers, 
            // then order has to be as follows:
            NDNBOOST_ASSERT(s_data.index.find(*(condemmed->second)) != s_data.index.end());
            s_data.index.erase(*(condemmed->second));
            s_data.cont.erase(condemmed); 
            --s;
         }
         else
            ++pos;
      }
      NDNBOOST_ASSERT(s_data.index[k]->first.get() == result.get());
      NDNBOOST_ASSERT(&(s_data.index.find(k)->first) == s_data.cont.back().second);
      NDNBOOST_ASSERT(s_data.index.find(k)->first == k);
   }
   return result;
}
 const T* get() const
 { 
     NDNBOOST_ASSERT(initialized_);
     return static_cast<const T*>(address()); 
 }
Example #14
0
bool rational<IntType>::operator< (const rational<IntType>& r) const
{
    // Avoid repeated construction
    int_type const  zero( 0 );

    // This should really be a class-wide invariant.  The reason for these
    // checks is that for 2's complement systems, INT_MIN has no corresponding
    // positive, so negating it during normalization keeps it INT_MIN, which
    // is bad for later calculations that assume a positive denominator.
    NDNBOOST_ASSERT( this->den > zero );
    NDNBOOST_ASSERT( r.den > zero );

    // Determine relative order by expanding each value to its simple continued
    // fraction representation using the Euclidian GCD algorithm.
    struct { int_type  n, d, q, r; }
     ts = { this->num, this->den, static_cast<int_type>(this->num / this->den),
     static_cast<int_type>(this->num % this->den) },
     rs = { r.num, r.den, static_cast<int_type>(r.num / r.den),
     static_cast<int_type>(r.num % r.den) };
    unsigned  reverse = 0u;

    // Normalize negative moduli by repeatedly adding the (positive) denominator
    // and decrementing the quotient.  Later cycles should have all positive
    // values, so this only has to be done for the first cycle.  (The rules of
    // C++ require a nonnegative quotient & remainder for a nonnegative dividend
    // & positive divisor.)
    while ( ts.r < zero )  { ts.r += ts.d; --ts.q; }
    while ( rs.r < zero )  { rs.r += rs.d; --rs.q; }

    // Loop through and compare each variable's continued-fraction components
    while ( true )
    {
        // The quotients of the current cycle are the continued-fraction
        // components.  Comparing two c.f. is comparing their sequences,
        // stopping at the first difference.
        if ( ts.q != rs.q )
        {
            // Since reciprocation changes the relative order of two variables,
            // and c.f. use reciprocals, the less/greater-than test reverses
            // after each index.  (Start w/ non-reversed @ whole-number place.)
            return reverse ? ts.q > rs.q : ts.q < rs.q;
        }

        // Prepare the next cycle
        reverse ^= 1u;

        if ( (ts.r == zero) || (rs.r == zero) )
        {
            // At least one variable's c.f. expansion has ended
            break;
        }

        ts.n = ts.d;         ts.d = ts.r;
        ts.q = ts.n / ts.d;  ts.r = ts.n % ts.d;
        rs.n = rs.d;         rs.d = rs.r;
        rs.q = rs.n / rs.d;  rs.r = rs.n % rs.d;
    }

    // Compare infinity-valued components for otherwise equal sequences
    if ( ts.r == rs.r )
    {
        // Both remainders are zero, so the next (and subsequent) c.f.
        // components for both sequences are infinity.  Therefore, the sequences
        // and their corresponding values are equal.
        return false;
    }
    else
    {
#ifdef NDNBOOST_MSVC
#pragma warning(push)
#pragma warning(disable:4800)
#endif
        // Exactly one of the remainders is zero, so all following c.f.
        // components of that variable are infinity, while the other variable
        // has a finite next c.f. component.  So that other variable has the
        // lesser value (modulo the reversal flag!).
        return ( ts.r != zero ) != static_cast<bool>( reverse );
#ifdef NDNBOOST_MSVC
#pragma warning(pop)
#endif
    }
}
 T* operator->() 
 { 
     NDNBOOST_ASSERT(initialized_);
     return static_cast<T*>(address()); 
 }
Example #16
0
 void increment(){
     NDNBOOST_ASSERT(valid_);
     valid_ = f_(begin_,end_,tok_);
 }
Example #17
0
 const Type&  dereference() const {
     NDNBOOST_ASSERT(valid_);
     return tok_;
 }
Example #18
0
 inline void throw_bad_alloc()
 {
    NDNBOOST_ASSERT(!"ndnboost::container bad_alloc thrown");
    std::abort();
 }
 const T& operator*() const
 { 
     NDNBOOST_ASSERT(initialized_);
     return *static_cast<const T*>(address()); 
 }