Changeset 5953 for branches/newapi
- Timestamp:
- 12/11/07 07:45:58 (12 months ago)
- Location:
- branches/newapi/licq
- Files:
-
- 3 modified
-
licq/event/event.h (modified) (4 diffs)
-
src/event/event.cpp (modified) (4 diffs)
-
src/event/test/eventtest.cpp (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
branches/newapi/licq/licq/event/event.h
r5894 r5953 21 21 #define LICQ_EVENT_H 22 22 23 #include "licq/exception/invalidargumentexception.h" 23 24 #include "licq/macros.h" 24 25 26 #include <boost/any.hpp> 25 27 #include <boost/noncopyable.hpp> 26 28 #include <boost/shared_ptr.hpp> … … 50 52 Id getId() const; 51 53 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 52 92 protected: 53 93 Event(const std::string& name); … … 59 99 const Id myId; 60 100 }; 101 102 template<> 103 void Event::setProperty(const std::string& key, const boost::any& value); 104 105 template<> 106 bool Event::getProperty(const std::string& key, const boost::any** value) const; 107 108 template<> 109 const boost::any& Event::getProperty(const std::string& key) const; 61 110 62 111 } // namespace Licq … … 72 121 } 73 122 123 template<typename ValueType> 124 inline void Licq::Event::setProperty(const std::string& key, 125 const ValueType& value) 126 { 127 setProperty(key, boost::any(value)); 128 } 129 130 template<typename ValueType> 131 inline 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 146 template<typename ValueType> 147 inline 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 74 153 #endif -
branches/newapi/licq/src/event/event.cpp
r5894 r5953 21 21 #include "licq/thread/mutex.h" 22 22 #include "licq/thread/mutexlocker.h" 23 #include "util/tr.h" 24 25 #include <cassert> 26 #include <map> 23 27 24 28 static Licq::Event::Id NextEventId = 1; … … 33 37 { 34 38 39 typedef std::map<std::string, boost::any> Properties; 40 35 41 class Event::Private 36 42 { … … 40 46 delete event; 41 47 } 48 49 Properties myProperties; 42 50 }; 43 51 … … 59 67 delete myPrivate; 60 68 } 69 70 bool Licq::Event::hasProperty(const std::string& key) const 71 { 72 return (myPrivate->myProperties.find(key) != myPrivate->myProperties.end()); 73 } 74 75 template<> 76 void Licq::Event::setProperty(const std::string& key, const boost::any& value) 77 { 78 assert(!value.empty()); 79 myPrivate->myProperties[key] = value; 80 } 81 82 template<> 83 bool 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 94 template<> 95 const 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 107 void Licq::Event::unsetProperty(const std::string& key) 108 { 109 myPrivate->myProperties.erase(key); 110 } -
branches/newapi/licq/src/event/test/eventtest.cpp
r5894 r5953 37 37 } 38 38 39 BOOST_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 39 63 BOOST_AUTO_TEST_SUITE_END()
