Re: Layered data structures
16.7.2012 2:05, Wil Evers kirjoitti:
I think the key is to make sure the A instance is closed for
modification before it is shared.
Here's a complete example program demonstrating both the problem and
its solution by Option 4 (read-only objects).
#include <cassert>
#include <iostream>
#include <memory>
#include <set>
typedef std::set<int> NumberSet;
typedef NumberSet::const_iterator Number_ConstIterator;
template <typename Type>
class ReadOnly
{
public:
ReadOnly(Type&& data)
: data_(new Type(std::move(data)))
{
}
const Type& operator()() const
{
return *data_;
}
Type release()
{
assert(data_.use_count() == 1);
return std::move(*data_);
}
private:
std::shared_ptr<Type> data_;
};
class NumberAnalysis
{
public:
NumberAnalysis(
ReadOnly<NumberSet> numberSet)
: numberSet_(numberSet)
, minEven_()
{
analyze();
}
Number_ConstIterator minEven() const
{
return minEven_;
}
private:
void analyze()
{
for (auto iter = numberSet_().cbegin();
iter != numberSet_().cend();
++iter)
{
if ((*iter & 1) == 0)
{
minEven_ = iter;
break;
}
};
}
ReadOnly<NumberSet> numberSet_;
Number_ConstIterator minEven_;
};
int main()
{
int numbers[] = {1, 5, 4, 2};
NumberSet numberSet;
numberSet.insert(std::begin(numbers), std::end(numbers));
// Close for modification.
ReadOnly<NumberSet> cNumberSet =
std::move(numberSet);
{
// Analyze the read-only number-set.
NumberAnalysis analysis(cNumberSet);
if (analysis.minEven() != cNumberSet().cend())
{
std::cout << "The minimum even number in the set is "
<< *analysis.minEven() << "." << std::endl;
}
else
{
std::cout << "There are no even numbers in the set."
<< std::endl;
}
}
// Recover the number-set for modification.
numberSet = std::move(cNumberSet.release());
numberSet.insert(10);
return 0;
}
This demonstrates Option 4. Somehow I dislike this solution. Also,
when writing this program I first wrote "!= numberSet.cend()", which
caused the program to crash. It seems easy to get confused between the
read-only object 'cNumberSet' and the original object 'numberSet'.
--
http://kaba.hilvi.org
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]