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
ItemReference& ItemReference::operator=(const string& value);
// 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>
struct getRowResult
{
enum { 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]];
}
static constexpr size_t N = sizeof...(C);
getRowResult(const Row& r, std::array<size_t, N>&& columns)
: mRow(r), mColumns(std::move(columns))
{
}
template<typename... Ts>
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)
const ItemReference operator[](size_t ix) const
{
typedef typename std::remove_reference<T>::type basicType;
const ItemReference v = rr[IX];
basicType tv = v.as<basicType>();
mVal = tv;
return mRow[mColumns[ix]];
}
T& mVal;
};
template<int IX, typename T, typename... Ts>
struct tieWrap<IX, T, Ts...>
template<typename... Ts, std::enable_if_t<N == sizeof...(Ts), int> = 0>
operator std::tuple<Ts...>() const
{
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;
const ItemReference v = rr[IX];
basicType tv = v.as<basicType>();
mVal = tv;
mNext.operator=(rr);
return get<Ts...>(std::index_sequence_for<Ts...>{});
}
T& mVal;
next mNext;
};
// And to convert a getRowResult to a std::tuple
template<int IX, typename... Ts>
struct toTuple;
template<int IX, typename T>
struct toTuple<IX,T>
template<typename... Ts, std::size_t... Is>
std::tuple<Ts...> get(std::index_sequence<Is...>) const
{
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];
std::tuple<T> tv = std::make_tuple(v.as<basicType>());
return tv;
};
const Row& mRow;
std::array<size_t, N> mColumns;
};
template<int IX, typename T, typename... Ts>
struct toTuple<IX,T,Ts...>
// we want to be able to tie some variables to a RowResult, for this we use tiewraps
template<typename... Ts>
struct tieWrap
{
typedef toTuple<IX + 1, Ts...> next;
tieWrap(Ts... args) : mVal(args...) {}
template<typename RR>
std::tuple<T,Ts...> operator()(RR& rr)
void operator=(const RR&& rr)
{
typedef typename std::remove_reference<T>::type basicType;
const ItemReference v = rr[IX];
std::tuple<T> tv = std::make_tuple(v.as<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;
}
next n;
return std::tuple_cat(tv, n(rr));
std::tuple<Ts...> mVal;
};
};
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>
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
......@@ -524,7 +408,7 @@ class Row
friend class Category;
friend class CatIndex;
friend class RowComparator;
friend struct detail::ItemReference;
friend class detail::ItemReference;
Row(ItemRow* data = nullptr, bool cascadeUpdate = true)
: mData(data), mCascadeUpdate(cascadeUpdate) {}
......@@ -543,7 +427,7 @@ class Row
mCascadeUpdate = cascadeUpdate;
}
void setCascadeDelet(bool cascadeDelete)
void setCascadeDelete(bool cascadeDelete)
{
mCascadeDelete = cascadeDelete;
}
......@@ -642,10 +526,10 @@ class Row
std::swap(mData, rhs.mData);
}
private:
friend std::ostream& operator<<(std::ostream& os, const Row& row);
private:
void assign(const string& name, const string& value, bool emplacing);
void assign(size_t column, const string& value, bool emplacing);
void assign(const Item& i, bool emplacing);
......@@ -660,10 +544,6 @@ class Row
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
......@@ -1111,7 +991,7 @@ class Category
public:
friend class Datablock;
friend class Row;
friend struct detail::ItemReference;
friend class detail::ItemReference;
Category(Datablock& db, const string& name, Validator* Validator);
Category(const Category&) = delete;
......
......@@ -1245,7 +1245,7 @@ void Category::sort(std::function<int(const Row&, const Row&)> comparator)
rows.push_back(itemRow);
std::stable_sort(rows.begin(), rows.end(),
[&rows,&comparator](ItemRow* ia, ItemRow* ib)
[&comparator](ItemRow* ia, ItemRow* ib)
{
Row ra(ia);
Row rb(ib);
......
......@@ -1018,10 +1018,8 @@ bool DictParser::collectItemTypes()
for (auto& t: dict["item_type_list"])
{
auto ts = t.get("code", "primitive_code", "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, "\\t", "\t");
......
......@@ -153,8 +153,8 @@ tuple<float,float,float,float> CalculateMapStatistics(const clipper::Xmap<FTYPE>
template<typename FTYPE>
void writeCCP4MapFile(ostream& os, clipper::Xmap<FTYPE>& xmap, clipper::Grid_range range)
{
static_assert(sizeof(CCP4MapFileHeader) == 256 * 4);
static_assert(__BYTE_ORDER == __LITTLE_ENDIAN);
static_assert(sizeof(CCP4MapFileHeader) == 256 * 4, "Map header is of incorrect size");
static_assert(__BYTE_ORDER == __LITTLE_ENDIAN, "Code for big endian systems is not implemented yet");
auto& spacegroup = xmap.spacegroup();
int spaceGroupNumber = spacegroup.descr().spacegroup_number();
......
......@@ -141,15 +141,15 @@ enum HelixFlag
// --------------------------------------------------------------------
const double
kSSBridgeDistance = 3.0,
// kSSBridgeDistance = 3.0,
kMinimalDistance = 0.5,
kMinimalCADistance = 9.0,
kMinHBondEnergy = -9.9,
kMaxHBondEnergy = -0.5,
kCouplingConstant = -27.888, // = -332 * 0.42 * 0.2
kMaxPeptideBondLength = 2.5;
kCouplingConstant = -27.888; // = -332 * 0.42 * 0.2
// kMaxPeptideBondLength = 2.5;
const uint32 kHistogramSize = 30;
// const uint32 kHistogramSize = 30;
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