Changeset 3253


Ignore:
Timestamp:
Jun 8, 2014, 1:48:42 AM (7 years ago)
Author:
Peter
Message:

refs #799. get_error: don't use operator<< on Key just to create an
error message; it limits which types can be used heavily. Instead
throw get_error that holds a copy of the key.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/utility.cc

    r3238 r3253  
    301301    suite.out() << "expected exception thrown with what() = "
    302302                << e.what() << "\n";
     303  }
     304  // don't run compiler test code
     305  if (false) {
     306    typedef boost::default_constructible_archetype<
     307      boost::sgi_assignable_archetype<> > key_type;
     308
     309    typedef boost::default_constructible_archetype<
     310      boost::sgi_assignable_archetype<
     311        boost::null_archetype<std::string> > > value_type;
     312
     313    typedef boost::default_constructible_archetype<
     314      boost::binary_function_archetype<key_type, key_type, bool> > Compare;
     315
     316    std::map<key_type, value_type, Compare> m3;
     317    key_type my_key;
     318    utility::get(m3, my_key);
    303319  }
    304320}
  • trunk/yat/utility/stl_utility.h

    r3247 r3253  
    99  Copyright (C) 2006 Jari Häkkinen
    1010  Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson
    11   Copyright (C) 2009, 2010, 2011, 2012, 2013 Peter Johansson
     11  Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Peter Johansson
    1212
    1313  This file is part of the yat library, http://dev.thep.lu.se/yat
     
    485485     \return const reference to m[k]
    486486
     487     \throw get_error if key \a k does not exist in map
     488
    487489     \since New in yat 0.7
    488490   */
     
    490492  const Tp& get(const std::map<Key, Tp, Compare, Alloc>& m, const Key& k);
    491493
     494  /**
     495     \brief error class used in
     496     get(const std::map<Key, Tp, Compare, Alloc>& m, const Key& k)
     497
     498     \since yat 0.13
     499   */
     500  template<typename Key>
     501  class get_error : public runtime_error
     502  {
     503  public:
     504    /// \brief constructor
     505    get_error(const std::string& msg, const Key& key)
     506      : runtime_error(msg), key_(key) {}
     507    /// \brief destructor
     508    virtual ~get_error(void) throw () {}
     509    /// access the key object
     510    const Key& key(void) const { return key_; }
     511  private:
     512    Key key_;
     513  };
    492514
    493515  /**
     
    910932    typename std::map<Key, Tp, Compare,Alloc>::const_iterator iter(m.find(key));
    911933    if (iter==m.end()) {
    912       std::stringstream ss;
    913       ss << "utility::get(const Map&, const Key&): '"
    914          << key << "' not found in map\n";
    915       throw runtime_error(ss.str());
     934      throw
     935        get_error<Key>("utility::get(const Map&, const Key&): key not found",
     936                       key);
    916937    }
    917938    return iter->second;
Note: See TracChangeset for help on using the changeset viewer.