1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
|
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//
#include <BALL/KERNEL/bond.h>
using namespace::std;
namespace BALL
{
using namespace BALL::Exception;
Bond::NotBound::NotBound(const char* file, int line)
: Exception::GeneralException(file, line, "Bond::NotBound", "The bond is not connected to any atom.")
{
}
Bond::Bond()
: Composite(),
PropertyManager(),
first_(BALL_BOND_DEFAULT_FIRST_ATOM),
second_(BALL_BOND_DEFAULT_SECOND_ATOM),
name_(BALL_BOND_DEFAULT_NAME),
bond_order_(BALL_BOND_DEFAULT_ORDER),
bond_type_(BALL_BOND_DEFAULT_TYPE)
{
}
Bond::Bond(const Bond& bond, bool deep)
: Composite(bond, deep),
PropertyManager(bond),
first_(bond.first_),
second_(bond.second_),
name_(bond.name_),
bond_order_(bond.bond_order_),
bond_type_(bond.bond_type_)
{
}
Bond::Bond(const String& name, Atom& first, Atom& second, Bond::Order order, Type type)
: Composite(),
PropertyManager(),
first_(BALL_BOND_DEFAULT_FIRST_ATOM),
second_(BALL_BOND_DEFAULT_SECOND_ATOM),
name_(name),
bond_order_(order),
bond_type_(type)
{
try
{
createBond(*this, first, second);
}
catch (TooManyBonds& e)
{
first_ = 0;
second_ = 0;
throw(e);
}
}
Bond* Bond::createBond(Bond& bond, Atom& first, Atom& second)
{
// first check the cases where no new bond will be created.
if (&first == &second)
{
return 0;
}
Bond* bond_ptr = first.getBond(second);
if (bond_ptr != 0)
{
return bond_ptr;
}
// throw an exception if there is no possibility to create another bond
// for any of the two atoms
if (((Size)first.number_of_bonds_ >= (Size)Atom::MAX_NUMBER_OF_BONDS)
|| ((Size)second.number_of_bonds_ >= (Size)Atom::MAX_NUMBER_OF_BONDS))
{
delete bond_ptr;
bond_ptr = 0;
throw TooManyBonds(__FILE__, __LINE__,
first.getFullName() + String(" and ") + second.getFullName() + ".");
}
// if the bond is already bound, delete it and create
// it anew
if (bond.isBound())
{
bond.clear();
}
first.bond_[first.number_of_bonds_]
= second.bond_[second.number_of_bonds_]
= &bond;
++(first.number_of_bonds_);
++(second.number_of_bonds_);
// keep the order
if (first < second)
{
bond.first_ = &first;
bond.second_ = &second;
}
else
{
bond.first_ = &second;
bond.second_ = &first;
}
return &bond;
}
Bond::~Bond()
{
arrangeBonds_();
}
void Bond::persistentWrite(PersistenceManager& pm, const char* name) const
{
pm.writeObjectHeader(this, name);
Composite::persistentWrite(pm);
pm.writeStorableObject(dynamic_cast<const PropertyManager&>(*this), "PropertyManager");
pm.writeObjectPointer(first_, "first_");
pm.writeObjectPointer(second_, "second_");
pm.writePrimitive(name_, "name_");
pm.writePrimitive((Index)bond_order_, "bond_order_");
pm.writePrimitive((Index)bond_type_, "bond_type_");
pm.writeObjectTrailer(name);
}
void Bond::persistentRead(PersistenceManager& pm)
{
pm.checkObjectHeader(RTTI::getStreamName<Composite>());
Composite::persistentRead(pm);
pm.checkObjectTrailer(0);
pm.readStorableObject(dynamic_cast<PropertyManager&>(*this), "PropertyManager");
pm.readObjectPointer(first_, "first_");
pm.readObjectPointer(second_, "second_");
pm.readPrimitive(name_, "name_");
Index tmp;
pm.readPrimitive(tmp, "bond_order_");
bond_order_ = (BondType)tmp;
pm.readPrimitive(tmp, "bond_type_");
bond_type_ = (Bond::Order)tmp;
}
Bond& Bond::operator = (const Bond& bond)
{
PropertyManager::operator = (bond);
first_ = bond.first_;
second_ = bond.second_;
name_ = bond.name_;
bond_order_ = bond.bond_order_;
bond_type_ = bond.bond_type_;
return *this;
}
void Bond::swap(Bond &bond)
{
PropertyManager::swap(bond);
Atom* temp_atom = first_;
first_ = bond.first_;
bond.first_ = temp_atom;
temp_atom = second_;
second_ = bond.second_;
bond.second_ = temp_atom;
name_.swap(bond.name_);
Order temp_order = bond_order_;
bond_order_ = bond.bond_order_;
bond.bond_order_ = temp_order;
Type temp_type = bond_type_;
bond_type_ = bond.bond_type_;
bond.bond_type_ = temp_type;
}
void Bond::dump(ostream& s, Size depth) const
{
BALL_DUMP_STREAM_PREFIX(s);
Composite::dump(s, depth);
BALL_DUMP_DEPTH(s, depth);
s << " name: " << name_ << endl;
BALL_DUMP_DEPTH(s, depth);
s << " order: " << bond_order_ << endl;
BALL_DUMP_DEPTH(s, depth);
s << " type: " << bond_type_ << endl;
BALL_DUMP_DEPTH(s, depth);
s << " first atom: " << first_ << endl;
BALL_DUMP_DEPTH(s, depth);
s << " second atom: " << second_ << endl;
BALL_DUMP_STREAM_SUFFIX(s);
}
void Bond::arrangeBonds_()
{
if (first_ != 0 && second_ != 0)
{
if (first_->number_of_bonds_ > 0)
{
first_->swapLastBond_(second_);
}
if (second_->number_of_bonds_ > 0)
{
second_->swapLastBond_(first_);
}
}
}
# ifdef BALL_NO_INLINE_FUNCTIONS
# include <BALL/KERNEL/bond.iC>
# endif
} // namespace BALL
|