Changeset 5953 for branches/newapi

Show
Ignore:
Timestamp:
12/11/07 07:45:58 (12 months ago)
Author:
erijo
Message:

Added properties to events

Location:
branches/newapi/licq
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • branches/newapi/licq/licq/event/event.h

    r5894 r5953  
    2121#define LICQ_EVENT_H 
    2222 
     23#include "licq/exception/invalidargumentexception.h" 
    2324#include "licq/macros.h" 
    2425 
     26#include <boost/any.hpp> 
    2527#include <boost/noncopyable.hpp> 
    2628#include <boost/shared_ptr.hpp> 
     
    5052  Id getId() const; 
    5153 
     54  /** 
     55   * @returns True if the event has a property @a key; otherwise 
     56   * false. 
     57   */ 
     58  bool hasProperty(const std::string& key) const; 
     59   
     60  /** 
     61   * Sets the property @a key to @a value. 
     62   */ 
     63  template<typename ValueType> 
     64  void setProperty(const std::string& key, const ValueType& value); 
     65 
     66  /** 
     67   * Fetches the value of the property @a key and stores it in @a 
     68   * value. 
     69   * 
     70   * @returns True if the event had a property @a key; otherwise 
     71   * false. @a value is only valid if getProperty returns true. 
     72   */ 
     73  template<typename ValueType> 
     74  bool getProperty(const std::string& key, ValueType* value) const; 
     75 
     76  /** 
     77   * Fetches the value of the property @a key. 
     78   * 
     79   * @returns The property value. 
     80   * 
     81   * @throws Licq::InvalidArgumentException If @a key don't exists. 
     82   * @throws boost::bad_any_cast If the ValueType is wrong. 
     83   */ 
     84  template<typename ValueType> 
     85  const ValueType& getProperty(const std::string& key) const; 
     86 
     87  /** 
     88   * Unsets the property @a key. 
     89   */ 
     90  void unsetProperty(const std::string& key); 
     91 
    5292protected: 
    5393  Event(const std::string& name); 
     
    5999  const Id myId; 
    60100}; 
     101 
     102template<> 
     103void Event::setProperty(const std::string& key, const boost::any& value); 
     104 
     105template<> 
     106bool Event::getProperty(const std::string& key, const boost::any** value) const; 
     107 
     108template<> 
     109const boost::any& Event::getProperty(const std::string& key) const; 
    61110 
    62111} // namespace Licq 
     
    72121} 
    73122 
     123template<typename ValueType> 
     124inline void Licq::Event::setProperty(const std::string& key, 
     125                                     const ValueType& value) 
     126{ 
     127  setProperty(key, boost::any(value)); 
     128} 
     129 
     130template<typename ValueType> 
     131inline bool Licq::Event::getProperty(const std::string& key, 
     132                                     ValueType* value) const 
     133{ 
     134  const boost::any* any; 
     135  if (!getProperty(key, &any)) 
     136    return false; 
     137 
     138  const ValueType* ptr = boost::any_cast<const ValueType>(any); 
     139  if (ptr == NULL) 
     140    return false; 
     141 
     142  *value = *ptr; 
     143  return true; 
     144} 
     145 
     146template<typename ValueType> 
     147inline const ValueType& Licq::Event::getProperty(const std::string& key) const 
     148{ 
     149  const boost::any& any = getProperty<boost::any>(key); 
     150  return boost::any_cast<const ValueType&>(any); 
     151} 
     152 
    74153#endif 
  • branches/newapi/licq/src/event/event.cpp

    r5894 r5953  
    2121#include "licq/thread/mutex.h" 
    2222#include "licq/thread/mutexlocker.h" 
     23#include "util/tr.h" 
     24 
     25#include <cassert> 
     26#include <map> 
    2327 
    2428static Licq::Event::Id NextEventId = 1; 
     
    3337{ 
    3438 
     39typedef std::map<std::string, boost::any> Properties; 
     40 
    3541class Event::Private 
    3642{ 
     
    4046    delete event; 
    4147  } 
     48 
     49  Properties myProperties; 
    4250}; 
    4351 
     
    5967  delete myPrivate; 
    6068} 
     69 
     70bool Licq::Event::hasProperty(const std::string& key) const 
     71{ 
     72  return (myPrivate->myProperties.find(key) != myPrivate->myProperties.end()); 
     73} 
     74 
     75template<> 
     76void Licq::Event::setProperty(const std::string& key, const boost::any& value) 
     77{ 
     78  assert(!value.empty()); 
     79  myPrivate->myProperties[key] = value; 
     80} 
     81 
     82template<> 
     83bool Licq::Event::getProperty(const std::string& key, 
     84                              const boost::any** value) const 
     85{ 
     86  Properties::const_iterator prop = myPrivate->myProperties.find(key); 
     87  if (prop == myPrivate->myProperties.end()) 
     88    return false; 
     89 
     90  *value = &prop->second; 
     91  return true; 
     92} 
     93 
     94template<> 
     95const boost::any& Licq::Event::getProperty(const std::string& key) const 
     96{ 
     97  Properties::const_iterator prop = myPrivate->myProperties.find(key); 
     98  if (prop == myPrivate->myProperties.end()) 
     99  { 
     100    throw InvalidArgumentException("Event::getProperty", 
     101                                   tr("no property named '%1%'") % key); 
     102  } 
     103 
     104  return prop->second; 
     105} 
     106 
     107void Licq::Event::unsetProperty(const std::string& key) 
     108{ 
     109  myPrivate->myProperties.erase(key); 
     110} 
  • branches/newapi/licq/src/event/test/eventtest.cpp

    r5894 r5953  
    3737} 
    3838 
     39BOOST_AUTO_TEST_CASE(properties) 
     40{ 
     41  boost::shared_ptr<Licq::Event> test = Licq::Event::create("test"); 
     42 
     43  const int i = 42; 
     44  test->setProperty("i", i + 1); 
     45  test->setProperty("i", i); 
     46  BOOST_CHECK(test->hasProperty("i")); 
     47   
     48  int j = 0; 
     49  BOOST_CHECK(test->getProperty("i", &j)); 
     50  BOOST_CHECK_EQUAL(i, j); 
     51  BOOST_CHECK_EQUAL(test->getProperty<int>("i"), i); 
     52 
     53  float k = 0; 
     54  BOOST_CHECK(!test->getProperty("i", &k)); 
     55  BOOST_CHECK_THROW(test->getProperty<float>("i"), boost::bad_any_cast); 
     56 
     57  test->unsetProperty("i"); 
     58  BOOST_CHECK(!test->hasProperty("i")); 
     59  BOOST_CHECK(!test->getProperty<int>("i", &j)); 
     60  BOOST_CHECK_THROW(test->getProperty<int>("i"), Licq::InvalidArgumentException); 
     61} 
     62 
    3963BOOST_AUTO_TEST_SUITE_END()