Commit 29ebdcf7 by maarten

Cleanup with better C++

git-svn-id: svn+ssh://gitlab/srv/svn-repos/pdb-redo/trunk@410 a1961a4f-ab94-4bcc-80e8-33b5a54de466
parent 4206f266
...@@ -345,177 +345,61 @@ namespace detail ...@@ -345,177 +345,61 @@ namespace detail
ItemReference& ItemReference::operator=(const string& value); ItemReference& ItemReference::operator=(const string& value);
// some helper classes to help create tuple result types // some helper classes to help create tuple result types
template<typename...> struct tupleCatter;
template<typename... Ts>
struct tupleCatter<std::tuple<Ts...>>
{
typedef std::tuple<Ts...> type;
};
template<typename... T1s, typename... T2s, typename... Rem>
struct tupleCatter<std::tuple<T1s...>, std::tuple<T2s...>, Rem...>
{
typedef typename tupleCatter<std::tuple<T1s..., T2s...>, Rem...>::type type;
};
template<typename...> struct colGetter;
template<typename T>
struct colGetter<T>
{
typedef std::tuple<const ItemReference> type;
template<typename Res>
static type get(Res& rs)
{
size_t index = Res::N - 1;
return std::tuple<const ItemReference>{ rs[index] };
}
};
template<typename T, typename... Ts>
struct colGetter<T, Ts...>
{
typedef colGetter<Ts...> next;
typedef typename tupleCatter<std::tuple<const ItemReference>, typename next::type>::type type;
template<typename Res>
static type get(Res& rs)
{
typedef colGetter<Ts...> next;
size_t index = Res::N - 1 - sizeof...(Ts);
return std::tuple_cat(std::tuple<const ItemReference>{ rs[index]}, next::get(rs));
}
};
template<typename... C> template<typename... C>
struct getRowResult struct getRowResult
{ {
enum { N = sizeof...(C) }; static constexpr size_t N = sizeof...(C);
typedef typename colGetter<C...>::type tupleType;
// const ItemReference operator[](const string& col) const
// {
// return mRow[col];
// }
const ItemReference operator[](size_t ix) const
{
return mRow[mColumns[ix]];
}
getRowResult(const Row& r, std::array<size_t, N>&& columns) getRowResult(const Row& r, std::array<size_t, N>&& columns)
: mRow(r), mColumns(std::move(columns)) : mRow(r), mColumns(std::move(columns))
{ {
} }
template<typename... Ts> const ItemReference operator[](size_t ix) const
operator std::tuple<Ts...>() const;
const Row& mRow;
std::array<size_t, N> mColumns;
};
// we want to be able to tie some variables to a RowResult, for this we use tiewraps
template<int IX, typename... Ts>
struct tieWrap;
template<int IX, typename T>
struct tieWrap<IX,T>
{
tieWrap(T& t)
: mVal(t) {}
template<typename Res>
void operator=(const Res& rr)
{ {
typedef typename std::remove_reference<T>::type basicType; return mRow[mColumns[ix]];
const ItemReference v = rr[IX];
basicType tv = v.as<basicType>();
mVal = tv;
} }
T& mVal; template<typename... Ts, std::enable_if_t<N == sizeof...(Ts), int> = 0>
}; operator std::tuple<Ts...>() const
template<int IX, typename T, typename... Ts>
struct tieWrap<IX, T, Ts...>
{
typedef tieWrap<IX + 1, Ts...> next;
tieWrap(T& t, Ts&... ts)
: mVal(t), mNext(ts...) {}
template<typename Res>
void operator=(const Res& rr)
{ {
typedef typename std::remove_reference<T>::type basicType; return get<Ts...>(std::index_sequence_for<Ts...>{});
const ItemReference v = rr[IX];
basicType tv = v.as<basicType>();
mVal = tv;
mNext.operator=(rr);
} }
T& mVal;
next mNext;
};
// And to convert a getRowResult to a std::tuple template<typename... Ts, std::size_t... Is>
template<int IX, typename... Ts> std::tuple<Ts...> get(std::index_sequence<Is...>) const
struct toTuple;
template<int IX, typename T>
struct toTuple<IX,T>
{
template<typename RR>
std::tuple<T> operator()(RR& rr)
{ {
typedef typename std::remove_reference<T>::type basicType; return std::tuple<Ts...>{mRow[mColumns[Is]].template as<Ts>()...};
}
const ItemReference v = rr[IX]; const Row& mRow;
std::tuple<T> tv = std::make_tuple(v.as<basicType>()); std::array<size_t, N> mColumns;
return tv;
};
}; };
template<int IX, typename T, typename... Ts> // we want to be able to tie some variables to a RowResult, for this we use tiewraps
struct toTuple<IX,T,Ts...> template<typename... Ts>
struct tieWrap
{ {
typedef toTuple<IX + 1, Ts...> next; tieWrap(Ts... args) : mVal(args...) {}
template<typename RR> template<typename RR>
std::tuple<T,Ts...> operator()(RR& rr) void operator=(const RR&& rr)
{ {
typedef typename std::remove_reference<T>::type basicType; // getRowResult will do the conversion, but only if the types
// are compatible. That means the number of parameters to the get()
// of the row should be equal to the number of items in the tuple
// you are trying to tie.
mVal = rr;
}
const ItemReference v = rr[IX]; std::tuple<Ts...> mVal;
std::tuple<T> tv = std::make_tuple(v.as<basicType>());
next n;
return std::tuple_cat(tv, n(rr));
};
}; };
template<typename... C>
template<typename... Ts>
getRowResult<C...>::operator std::tuple<Ts...>() const
{
typedef toTuple<0, Ts...> ToTuple;
ToTuple tt;
return tt(*this);
}
} }
template<typename... Ts> template<typename... Ts>
auto tie(Ts&... v) -> detail::tieWrap<0, Ts...> auto tie(Ts&... v) -> detail::tieWrap<Ts...>
{ {
return detail::tieWrap<0, Ts...>(v...); return detail::tieWrap<Ts...>(v...);
} }
class Row class Row
...@@ -524,7 +408,7 @@ class Row ...@@ -524,7 +408,7 @@ class Row
friend class Category; friend class Category;
friend class CatIndex; friend class CatIndex;
friend class RowComparator; friend class RowComparator;
friend struct detail::ItemReference; friend class detail::ItemReference;
Row(ItemRow* data = nullptr, bool cascadeUpdate = true) Row(ItemRow* data = nullptr, bool cascadeUpdate = true)
: mData(data), mCascadeUpdate(cascadeUpdate) {} : mData(data), mCascadeUpdate(cascadeUpdate) {}
...@@ -543,7 +427,7 @@ class Row ...@@ -543,7 +427,7 @@ class Row
mCascadeUpdate = cascadeUpdate; mCascadeUpdate = cascadeUpdate;
} }
void setCascadeDelet(bool cascadeDelete) void setCascadeDelete(bool cascadeDelete)
{ {
mCascadeDelete = cascadeDelete; mCascadeDelete = cascadeDelete;
} }
...@@ -641,11 +525,11 @@ class Row ...@@ -641,11 +525,11 @@ class Row
{ {
std::swap(mData, rhs.mData); std::swap(mData, rhs.mData);
} }
private:
friend std::ostream& operator<<(std::ostream& os, const Row& row); friend std::ostream& operator<<(std::ostream& os, const Row& row);
private:
void assign(const string& name, const string& value, bool emplacing); void assign(const string& name, const string& value, bool emplacing);
void assign(size_t column, const string& value, bool emplacing); void assign(size_t column, const string& value, bool emplacing);
void assign(const Item& i, bool emplacing); void assign(const Item& i, bool emplacing);
...@@ -660,10 +544,6 @@ class Row ...@@ -660,10 +544,6 @@ class Row
bool mCascadeDelete = true; bool mCascadeDelete = true;
}; };
// swap for Rows is defined below
std::ostream& operator<<(std::ostream& os, const Row& row);
// -------------------------------------------------------------------- // --------------------------------------------------------------------
// some more templates to be able to do querying // some more templates to be able to do querying
...@@ -1111,7 +991,7 @@ class Category ...@@ -1111,7 +991,7 @@ class Category
public: public:
friend class Datablock; friend class Datablock;
friend class Row; friend class Row;
friend struct detail::ItemReference; friend class detail::ItemReference;
Category(Datablock& db, const string& name, Validator* Validator); Category(Datablock& db, const string& name, Validator* Validator);
Category(const Category&) = delete; Category(const Category&) = delete;
......
...@@ -1245,7 +1245,7 @@ void Category::sort(std::function<int(const Row&, const Row&)> comparator) ...@@ -1245,7 +1245,7 @@ void Category::sort(std::function<int(const Row&, const Row&)> comparator)
rows.push_back(itemRow); rows.push_back(itemRow);
std::stable_sort(rows.begin(), rows.end(), std::stable_sort(rows.begin(), rows.end(),
[&rows,&comparator](ItemRow* ia, ItemRow* ib) [&comparator](ItemRow* ia, ItemRow* ib)
{ {
Row ra(ia); Row ra(ia);
Row rb(ib); Row rb(ib);
......
...@@ -1018,10 +1018,8 @@ bool DictParser::collectItemTypes() ...@@ -1018,10 +1018,8 @@ bool DictParser::collectItemTypes()
for (auto& t: dict["item_type_list"]) for (auto& t: dict["item_type_list"])
{ {
auto ts = t.get("code", "primitive_code", "construct");
string code, primitiveCode, construct; string code, primitiveCode, construct;
cif::tie(code, primitiveCode, construct) = ts; cif::tie(code, primitiveCode, construct) = t.get("code", "primitive_code", "construct");
ba::replace_all(construct, "\\n", "\n"); ba::replace_all(construct, "\\n", "\n");
ba::replace_all(construct, "\\t", "\t"); ba::replace_all(construct, "\\t", "\t");
......
...@@ -153,8 +153,8 @@ tuple<float,float,float,float> CalculateMapStatistics(const clipper::Xmap<FTYPE> ...@@ -153,8 +153,8 @@ tuple<float,float,float,float> CalculateMapStatistics(const clipper::Xmap<FTYPE>
template<typename FTYPE> template<typename FTYPE>
void writeCCP4MapFile(ostream& os, clipper::Xmap<FTYPE>& xmap, clipper::Grid_range range) void writeCCP4MapFile(ostream& os, clipper::Xmap<FTYPE>& xmap, clipper::Grid_range range)
{ {
static_assert(sizeof(CCP4MapFileHeader) == 256 * 4); static_assert(sizeof(CCP4MapFileHeader) == 256 * 4, "Map header is of incorrect size");
static_assert(__BYTE_ORDER == __LITTLE_ENDIAN); static_assert(__BYTE_ORDER == __LITTLE_ENDIAN, "Code for big endian systems is not implemented yet");
auto& spacegroup = xmap.spacegroup(); auto& spacegroup = xmap.spacegroup();
int spaceGroupNumber = spacegroup.descr().spacegroup_number(); int spaceGroupNumber = spacegroup.descr().spacegroup_number();
......
...@@ -141,15 +141,15 @@ enum HelixFlag ...@@ -141,15 +141,15 @@ enum HelixFlag
// -------------------------------------------------------------------- // --------------------------------------------------------------------
const double const double
kSSBridgeDistance = 3.0, // kSSBridgeDistance = 3.0,
kMinimalDistance = 0.5, kMinimalDistance = 0.5,
kMinimalCADistance = 9.0, kMinimalCADistance = 9.0,
kMinHBondEnergy = -9.9, kMinHBondEnergy = -9.9,
kMaxHBondEnergy = -0.5, kMaxHBondEnergy = -0.5,
kCouplingConstant = -27.888, // = -332 * 0.42 * 0.2 kCouplingConstant = -27.888; // = -332 * 0.42 * 0.2
kMaxPeptideBondLength = 2.5; // kMaxPeptideBondLength = 2.5;
const uint32 kHistogramSize = 30; // const uint32 kHistogramSize = 30;
struct Res struct Res
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment