int main() { Int i = { 3 }; pInt(&i); i.c = 1; pInt(&i); printf("%d\n", i.val); }
TEST(Optional, MakeOptional) { // const L-value version const std::string s("abc"); auto optStr = make_optional(s); ASSERT_TRUE(optStr.hasValue()); EXPECT_EQ(*optStr, "abc"); *optStr = "cde"; EXPECT_EQ(s, "abc"); EXPECT_EQ(*optStr, "cde"); // L-value version std::string s2("abc"); auto optStr2 = make_optional(s2); ASSERT_TRUE(optStr2.hasValue()); EXPECT_EQ(*optStr2, "abc"); *optStr2 = "cde"; // it's vital to check that s2 wasn't clobbered EXPECT_EQ(s2, "abc"); // L-value reference version std::string& s3(s2); auto optStr3 = make_optional(s3); ASSERT_TRUE(optStr3.hasValue()); EXPECT_EQ(*optStr3, "abc"); *optStr3 = "cde"; EXPECT_EQ(s3, "abc"); // R-value ref version unique_ptr<int> pInt(new int(3)); auto optIntPtr = make_optional(std::move(pInt)); EXPECT_TRUE(pInt.get() == nullptr); ASSERT_TRUE(optIntPtr.hasValue()); EXPECT_EQ(**optIntPtr, 3); }
void testSmartPointers() { boost::shared_ptr<int> pInt( new int(2) ); std::cout << "pInt: " << *pInt << std::endl; boost::shared_ptr<Object> pObject( new Object(4) ); boost::shared_ptr<A> pA; boost::shared_ptr<B> pB; { pA.reset( new A() ); pB.reset( new B() ); pA->value = 4; /* A -> B */ pA->b = pB; pB->value = 8; /* B -> A */ //pB->a = pA; } std::cout << "Value of pA: " << pA->value << std::endl; std::cout << "Value of pB: " << pB->value << std::endl; boost::shared_ptr<A> pA2= pB->a.lock(); if(pA2.get() != 0L) { std::cout << "Value of pA: " << pA2->value << std::endl; } std::cout << "Value of pB: " << pA->b->value << std::endl; std::cout << "Refs to pA: " << pA.use_count() << std::endl; std::cout << "Refs to pB: " << pB.use_count() << std::endl; }
/** * Adds an int value to the parameter map. * @param comp :: Component to which the new parameter is related * @param name :: Name for the new parameter * @param value :: Parameter value as an int * @param pDescription :: a pointer (may be NULL) to a string, containing * parameter's * description. If provided, the contents of the string is copied to the * parameters * memory */ void ParameterMap::addInt(const IComponent *comp, const std::string &name, int value, const std::string *const pDescription) { add(pInt(), comp, name, value, pDescription); }