Changeset 9223
- Timestamp:
- May 19, 2012, 11:35:32 AM (13 years ago)
- Location:
- code/branches/testing
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/testing/src/libraries/util/MultiType.h
r9222 r9223 58 58 If you want to change the type, there are three possibilities: 59 59 - @ref orxonox::MultiType::convert "convert<T>()" sets the type to T and converts the currently assigned value 60 - @ref orxonox::MultiType:: setType "setType<T>()" sets the type to T and resets the value to zero using zeroise<T>()60 - @ref orxonox::MultiType::reset "reset<T>()" sets the type to T and resets the value to zero using zeroise<T>() 61 61 - setValue<T>(value) assigns a new value and changes the type to T. 62 62 … … 339 339 return this->assignValue (static_cast<void*>(const_cast<typename Loki::TypeTraits<V>::UnqualifiedType*>(value))); 340 340 } 341 /// Changes the type to T and assigns the new value (which might be of another type than T - it gets converted). 342 template <typename T, typename V> inline bool setValue(const V& value) { this->reset<T>(); return this->setValue(value); } 341 343 /// Assigns the value of the other MultiType and converts it to the current type. 342 344 bool setValue(const MultiType& other) { if (this->value_) { return this->value_->assimilate(other); } else { if (other.value_) { this->value_ = other.value_->clone(); } return true; } } 343 /// Changes the type to T and assigns the new value (which might be of another type than T - it gets converted).344 template <typename T, typename V> inline bool setValue(const V& value) { this->setType<T>(); return this->setValue(value); }345 346 345 347 346 /// Copies the other MultiType by assigning value and type. … … 353 352 /// Resets value and type. Type will be void afterwards and null() returns true. 354 353 inline void reset() { if (this->value_) delete this->value_; this->value_ = 0; } 355 /// Current content gets overridden with default zero value 354 /// Resets the value and changes the internal type to T. 355 template <typename T> inline void reset() { this->assignValue(typename Loki::TypeTraits<T>::UnqualifiedReferredType()); } 356 /// Current value gets overridden with default zero value 356 357 inline void resetValue() { if (this->value_) this->value_->reset(); } 357 358 /// Resets the value and changes the internal type to T.359 template <typename T> inline void setType() { this->assignValue(typename Loki::TypeTraits<T>::UnqualifiedReferredType()); }360 358 361 359 /// Returns true if the current type is T. … … 377 375 378 376 /// Checks if the MT contains no value. 379 bool null() const { return (!this->value_); }377 bool null() const { return !this->value_; } 380 378 381 379 operator char() const; … … 497 495 _UtilExport inline std::ostream& operator<<(std::ostream& outstream, const MultiType& mt) { if (mt.value_) { mt.value_->toString(outstream); } return outstream; } 498 496 499 template <> inline bool MultiType::isType<void>() const { return this->null(); 497 template <> inline bool MultiType::isType<void>() const { return this->null(); } ///< Returns true if the current type equals the given type. 500 498 template <> inline bool MultiType::isType<char>() const { return (this->value_ && this->value_->type_ == Type::Char); } ///< Returns true if the current type equals the given type. 501 499 template <> inline bool MultiType::isType<unsigned char>() const { return (this->value_ && this->value_->type_ == Type::UnsignedChar); } ///< Returns true if the current type equals the given type. -
code/branches/testing/test/util/MultiTypeTest.cc
r9221 r9223 18 18 // x convert<type>() 19 19 // x reset 20 // x reset<type>() 20 21 // x resetValue 21 // x setType<type>()22 22 23 23 // x isType<type>() … … 310 310 } 311 311 312 /////////////////// 313 // reset<type>() // 314 /////////////////// 315 TEST(MultiType, SetType) 316 { 317 MultiType mt(10); 318 319 EXPECT_TRUE(mt.isType<int>()); 320 EXPECT_FALSE(mt.isType<float>()); 321 EXPECT_EQ(10, mt.getInt()); 322 323 mt.reset<float>(); 324 325 EXPECT_FALSE(mt.isType<int>()); 326 EXPECT_TRUE(mt.isType<float>()); 327 EXPECT_EQ(0, mt.getInt()); 328 } 329 312 330 //////////////// 313 331 // resetValue // … … 323 341 324 342 EXPECT_TRUE(mt.isType<int>()); 325 EXPECT_EQ(0, mt.getInt());326 }327 328 /////////////////////329 // setType<type>() //330 /////////////////////331 TEST(MultiType, SetType)332 {333 MultiType mt(10);334 335 EXPECT_TRUE(mt.isType<int>());336 EXPECT_FALSE(mt.isType<float>());337 EXPECT_EQ(10, mt.getInt());338 339 mt.setType<float>();340 341 EXPECT_FALSE(mt.isType<int>());342 EXPECT_TRUE(mt.isType<float>());343 343 EXPECT_EQ(0, mt.getInt()); 344 344 }
Note: See TracChangeset
for help on using the changeset viewer.