Changeset 7a1ce5


Ignore:
Timestamp:
Feb 16, 2010, 4:24:07 PM (15 years ago)
Author:
Tillmann Crueger <crueger@…>
Branches:
Action_Thermostats, Add_AtomRandomPerturbation, Add_FitFragmentPartialChargesAction, Add_RotateAroundBondAction, Add_SelectAtomByNameAction, Added_ParseSaveFragmentResults, AddingActions_SaveParseParticleParameters, Adding_Graph_to_ChangeBondActions, Adding_MD_integration_tests, Adding_ParticleName_to_Atom, Adding_StructOpt_integration_tests, AtomFragments, Automaking_mpqc_open, AutomationFragmentation_failures, Candidate_v1.5.4, Candidate_v1.6.0, Candidate_v1.6.1, ChangeBugEmailaddress, ChangingTestPorts, ChemicalSpaceEvaluator, CombiningParticlePotentialParsing, Combining_Subpackages, Debian_Package_split, Debian_package_split_molecuildergui_only, Disabling_MemDebug, Docu_Python_wait, EmpiricalPotential_contain_HomologyGraph, EmpiricalPotential_contain_HomologyGraph_documentation, Enable_parallel_make_install, Enhance_userguide, Enhanced_StructuralOptimization, Enhanced_StructuralOptimization_continued, Example_ManyWaysToTranslateAtom, Exclude_Hydrogens_annealWithBondGraph, FitPartialCharges_GlobalError, Fix_BoundInBox_CenterInBox_MoleculeActions, Fix_ChargeSampling_PBC, Fix_ChronosMutex, Fix_FitPartialCharges, Fix_FitPotential_needs_atomicnumbers, Fix_ForceAnnealing, Fix_IndependentFragmentGrids, Fix_ParseParticles, Fix_ParseParticles_split_forward_backward_Actions, Fix_PopActions, Fix_QtFragmentList_sorted_selection, Fix_Restrictedkeyset_FragmentMolecule, Fix_StatusMsg, Fix_StepWorldTime_single_argument, Fix_Verbose_Codepatterns, Fix_fitting_potentials, Fixes, ForceAnnealing_goodresults, ForceAnnealing_oldresults, ForceAnnealing_tocheck, ForceAnnealing_with_BondGraph, ForceAnnealing_with_BondGraph_continued, ForceAnnealing_with_BondGraph_continued_betteresults, ForceAnnealing_with_BondGraph_contraction-expansion, FragmentAction_writes_AtomFragments, FragmentMolecule_checks_bonddegrees, GeometryObjects, Gui_Fixes, Gui_displays_atomic_force_velocity, ImplicitCharges, IndependentFragmentGrids, IndependentFragmentGrids_IndividualZeroInstances, IndependentFragmentGrids_IntegrationTest, IndependentFragmentGrids_Sole_NN_Calculation, JobMarket_RobustOnKillsSegFaults, JobMarket_StableWorkerPool, JobMarket_unresolvable_hostname_fix, MoreRobust_FragmentAutomation, ODR_violation_mpqc_open, PartialCharges_OrthogonalSummation, PdbParser_setsAtomName, PythonUI_with_named_parameters, QtGui_reactivate_TimeChanged_changes, Recreated_GuiChecks, Rewrite_FitPartialCharges, RotateToPrincipalAxisSystem_UndoRedo, SaturateAtoms_findBestMatching, SaturateAtoms_singleDegree, StoppableMakroAction, Subpackage_CodePatterns, Subpackage_JobMarket, Subpackage_LinearAlgebra, Subpackage_levmar, Subpackage_mpqc_open, Subpackage_vmg, Switchable_LogView, ThirdParty_MPQC_rebuilt_buildsystem, TrajectoryDependenant_MaxOrder, TremoloParser_IncreasedPrecision, TremoloParser_MultipleTimesteps, TremoloParser_setsAtomName, Ubuntu_1604_changes, stable
Children:
354859
Parents:
d1c5e2
Message:

Rebuilt AtomDescriptors using PIMPL-Idiom and added unittest for descriptors

Location:
src
Files:
4 added
8 edited

Legend:

Unmodified
Added
Removed
  • src/Descriptors/AtomDescriptor.cpp

    rd1c5e2 r7a1ce5  
    66 */
    77
    8 #include "AtomDescriptor.hpp"
     8#include "Descriptors/AtomDescriptor.hpp"
     9#include "Descriptors/AtomDescriptor_impl.hpp"
    910
    1011#include "World.hpp"
    1112
     13#include "atom.hpp"
     14
    1215#include <boost/bind.hpp>
    1316#include <cassert>
     17#include <iostream>
    1418
    1519using namespace std;
     
    1822typedef atoms_t::iterator atoms_iter_t;
    1923
    20 AtomDescriptor::AtomDescriptor()
     24/************************ Forwarding object **************************************/
     25
     26
     27AtomDescriptor::AtomDescriptor(impl_ptr _impl) :
     28    impl(_impl)
     29{}
     30
     31AtomDescriptor::AtomDescriptor(const AtomDescriptor& src) :
     32    impl(src.get_impl())
     33{}
     34
     35AtomDescriptor::~AtomDescriptor()
     36{}
     37
     38AtomDescriptor& AtomDescriptor::operator=(AtomDescriptor &src){
     39  if(&src!=this) {
     40    impl=src.get_impl();
     41  }
     42  return *this;
     43}
     44
     45atom* AtomDescriptor::find(){
     46  return impl->find();
     47}
     48
     49std::vector<atom*> AtomDescriptor::findAll(){
     50  return impl->findAll();
     51}
     52
     53AtomDescriptor::impl_ptr AtomDescriptor::get_impl() const{
     54  return impl;
     55}
     56
     57
     58
     59
     60/**************************** implementation ********************/
     61
     62AtomDescriptor_impl::AtomDescriptor_impl()
    2163{
    2264}
    2365
    24 AtomDescriptor::AtomDescriptor(const AtomDescriptor &desc) {
    25 }
    26 
    27 AtomDescriptor::~AtomDescriptor()
     66AtomDescriptor_impl::~AtomDescriptor_impl()
    2867{
    2968}
    3069
    31 
    32 atoms_t& AtomDescriptor::getAtoms(){
     70atoms_t& AtomDescriptor_impl::getAtoms(){
    3371  return World::get()->atoms;
    3472}
    3573
    36 atom* AtomDescriptor::find() {
     74atom* AtomDescriptor_impl::find() {
    3775  atoms_t atoms = getAtoms();
    38   atoms_iter_t res = find_if(atoms.begin(),atoms.end(),boost::bind(&AtomDescriptor::predicate,this,_1));
     76  atoms_iter_t res = find_if(atoms.begin(),atoms.end(),boost::bind(&AtomDescriptor_impl::predicate,this,_1));
    3977  return (res!=atoms.end())?((*res).second):0;
    4078}
    4179
    42 vector<atom*> AtomDescriptor::findAll() {
     80vector<atom*> AtomDescriptor_impl::findAll() {
    4381  vector<atom*> res;
    4482  atoms_t atoms = getAtoms();
    4583  atoms_iter_t iter;
    4684  for(iter=atoms.begin();iter!=atoms.end();++iter) {
    47     if(predicate(*iter))
     85    if(predicate(*iter)){
    4886      res.push_back((*iter).second);
     87    }
    4988  }
     89  return res;
    5090}
    5191
    52 // stuff for operators
     92/************************** Universe and Emptyset *****************/
     93
     94AtomAllDescriptor_impl::AtomAllDescriptor_impl()
     95{}
     96
     97AtomAllDescriptor_impl::~AtomAllDescriptor_impl()
     98{}
     99
     100bool AtomAllDescriptor_impl::predicate(std::pair<int,atom*>){
     101  return true;
     102}
     103
     104AtomDescriptor AllAtoms(){
     105  return AtomDescriptor(AtomDescriptor::impl_ptr(new AtomAllDescriptor_impl));
     106}
     107
     108AtomNoneDescriptor_impl::AtomNoneDescriptor_impl()
     109{}
     110
     111AtomNoneDescriptor_impl::~AtomNoneDescriptor_impl()
     112{}
     113
     114bool AtomNoneDescriptor_impl::predicate(std::pair<int,atom*>){
     115  return false;
     116}
     117
     118AtomDescriptor NoAtoms(){
     119  return AtomDescriptor(AtomDescriptor::impl_ptr(new AtomNoneDescriptor_impl));
     120}
     121
     122/************************** Operator stuff ************************/
    53123
    54124// AND
    55 AtomAndDescriptor::AtomAndDescriptor(const AtomDescriptor &_lhs,const AtomDescriptor &_rhs){
    56   lhs=_lhs.clone();
    57   rhs=_rhs.clone();
    58 }
     125AtomAndDescriptor_impl::AtomAndDescriptor_impl(AtomDescriptor::impl_ptr _lhs, AtomDescriptor::impl_ptr _rhs) :
     126    lhs(_lhs), rhs(_rhs)
     127{}
    59128
    60 AtomAndDescriptor::AtomAndDescriptor(const AtomAndDescriptor &_desc)
    61 {
    62   // the copy constructor needs it's parameter as const to work,
    63   // however we need to be able to transfer the ownership of the pointers
    64   // to the new method to avoid another cloning.
    65   //
    66   // the only place where the copy constructor is used for these classes,
    67   // is when returning a temporary.
    68   // this is one of the very rare cases when const_cast makes sense
    69   AtomAndDescriptor &desc = const_cast<AtomAndDescriptor&>(_desc);
    70   lhs = desc.lhs;
    71   rhs = desc.rhs;
    72 }
     129AtomAndDescriptor_impl::~AtomAndDescriptor_impl()
     130{}
    73131
    74 AtomAndDescriptor::~AtomAndDescriptor(){
    75 }
    76 
    77 bool AtomAndDescriptor::predicate(std::pair<int,atom*> atom){
     132bool AtomAndDescriptor_impl::predicate(std::pair<int,atom*> atom){
    78133  return lhs->predicate(atom) && rhs->predicate(atom);
    79134}
    80 
    81 AtomAndDescriptor::desc_ptr AtomAndDescriptor::clone() const{
    82   return desc_ptr(new AtomAndDescriptor(*lhs,*rhs));
    83 }
    84 
    85 AtomAndDescriptor operator&&(const AtomDescriptor &lhs, const AtomDescriptor &rhs){
    86   return AtomAndDescriptor(lhs,rhs);
     135AtomDescriptor operator&&(const AtomDescriptor &lhs, const AtomDescriptor &rhs){
     136  AtomDescriptor::impl_ptr newImpl = AtomDescriptor::impl_ptr(new AtomAndDescriptor_impl(lhs.get_impl(),rhs.get_impl()));
     137  return AtomDescriptor(newImpl);
    87138}
    88139
    89140// OR
     141AtomOrDescriptor_impl::AtomOrDescriptor_impl(AtomDescriptor::impl_ptr _lhs ,AtomDescriptor::impl_ptr _rhs) :
     142    lhs(_lhs), rhs(_rhs)
     143{}
    90144
    91 AtomOrDescriptor::AtomOrDescriptor(const AtomDescriptor &_lhs,const AtomDescriptor &_rhs){
    92   lhs=_lhs.clone();
    93   rhs=_rhs.clone();
     145AtomOrDescriptor_impl::~AtomOrDescriptor_impl(){
    94146}
    95147
    96 AtomOrDescriptor::AtomOrDescriptor(const AtomOrDescriptor &_desc)
    97 {
    98   // the copy constructor needs it's parameter as const to work,
    99   // however we need to be able to transfer the ownership of the pointers
    100   // to the new method to avoid another cloning.
    101   //
    102   // the only place where the copy constructor is used for these classes,
    103   // is when returning a temporary.
    104   // this is one of the very rare cases when const_cast makes sense
    105   AtomOrDescriptor &desc = const_cast<AtomOrDescriptor&>(_desc);
    106   lhs = desc.lhs;
    107   rhs = desc.rhs;
    108 }
    109 
    110 AtomOrDescriptor::~AtomOrDescriptor(){
    111 }
    112 
    113 bool AtomOrDescriptor::predicate(std::pair<int,atom*> atom){
     148bool AtomOrDescriptor_impl::predicate(std::pair<int,atom*> atom){
    114149  return lhs->predicate(atom) || rhs->predicate(atom);
    115150}
    116151
    117 AtomAndDescriptor::desc_ptr AtomOrDescriptor::clone() const{
    118   return desc_ptr(new AtomOrDescriptor(*lhs,*rhs));
    119 }
    120 
    121 AtomOrDescriptor  operator||(const AtomDescriptor &lhs, const AtomDescriptor &rhs){
    122   return AtomOrDescriptor(lhs,rhs);
     152AtomDescriptor  operator||(const AtomDescriptor &lhs, const AtomDescriptor &rhs){
     153  AtomDescriptor::impl_ptr newImpl = AtomDescriptor::impl_ptr(new AtomOrDescriptor_impl(lhs.get_impl(),rhs.get_impl()));
     154  return AtomDescriptor(newImpl);
    123155}
    124156
    125157// NOT
    126158
    127 AtomNotDescriptor::AtomNotDescriptor(const AtomDescriptor &_arg){
    128   arg = _arg.clone();
    129 }
     159AtomNotDescriptor_impl::AtomNotDescriptor_impl(AtomDescriptor::impl_ptr _arg) :
     160    arg(_arg)
     161{}
    130162
    131 AtomNotDescriptor::AtomNotDescriptor(const AtomNotDescriptor &_desc) {
    132   // the copy constructor needs it's parameter as const to work,
    133   // however we need to be able to transfer the ownership of the pointers
    134   // to the new method to avoid another cloning.
    135   //
    136   // the only place where the copy constructor is used for these classes,
    137   // is when returning a temporary.
    138   // this is one of the very rare cases when const_cast makes sense
    139   AtomNotDescriptor &desc = const_cast<AtomNotDescriptor&>(_desc);
    140   arg = desc.arg;
    141 }
    142163
    143 AtomNotDescriptor::~AtomNotDescriptor()
     164AtomNotDescriptor_impl::~AtomNotDescriptor_impl()
    144165{
    145166}
    146167
    147 bool AtomNotDescriptor::predicate(std::pair<int,atom*> atom){
     168bool AtomNotDescriptor_impl::predicate(std::pair<int,atom*> atom){
    148169 return !(arg->predicate(atom));
    149170}
    150171
    151 AtomAndDescriptor::desc_ptr AtomNotDescriptor::clone() const{
    152   return desc_ptr(new AtomNotDescriptor(*arg));
     172AtomDescriptor operator!(const AtomDescriptor &arg){
     173  AtomDescriptor::impl_ptr newImpl = AtomDescriptor::impl_ptr(new AtomNotDescriptor_impl(arg.get_impl()));
     174  return AtomDescriptor(newImpl);
    153175}
    154 
    155 AtomNotDescriptor operator!(const AtomDescriptor &arg){
    156   return AtomNotDescriptor(arg);
    157 }
  • src/Descriptors/AtomDescriptor.hpp

    rd1c5e2 r7a1ce5  
    1717class atom;
    1818
    19 class AtomDescriptor
    20 {
     19// internal implementation, allows assignment, copying etc
     20class AtomDescripter_impl;
     21
     22class AtomDescriptor {
     23  friend atom* World::getAtom(AtomDescriptor descriptor);
     24  friend std::vector<atom*> World::getAllAtoms(AtomDescriptor descriptor);
     25
     26  friend AtomDescriptor operator&&(const AtomDescriptor &lhs, const AtomDescriptor &rhs);
     27  friend AtomDescriptor operator||(const AtomDescriptor &lhs, const AtomDescriptor &rhs);
     28  friend AtomDescriptor operator!(const AtomDescriptor &arg);
     29
    2130public:
     31  typedef boost::shared_ptr<AtomDescriptor_impl> impl_ptr;
    2232
    23   typedef boost::shared_ptr<AtomDescriptor> desc_ptr;
     33  AtomDescriptor(impl_ptr);
     34  AtomDescriptor(const AtomDescriptor&);
     35  ~AtomDescriptor();
    2436
    25   friend atom* World::getAtom(AtomDescriptor& descriptor);
    26   friend std::vector<atom*> World::getAllAtoms(AtomDescriptor& descriptor);
    27 
    28   AtomDescriptor();
    29   virtual ~AtomDescriptor();
    30   AtomDescriptor(const AtomDescriptor&);
    31 
    32   virtual bool predicate(std::pair<int,atom*>)=0;
    33   // clone is only used internally to generate operators
    34   virtual desc_ptr clone() const=0;
     37  AtomDescriptor &operator=(AtomDescriptor &);
    3538
    3639protected:
    37   virtual atom* find();
    38   virtual std::vector<atom*> findAll();
    39   std::map<int,atom*>& getAtoms();
     40  atom* find();
     41  std::vector<atom*> findAll();
     42  impl_ptr get_impl() const;
     43
     44private:
     45  impl_ptr impl;
    4046};
    4147
    42 class AtomAndDescriptor : public AtomDescriptor
    43 {
    44   friend AtomAndDescriptor operator&&(const AtomDescriptor &lhs, const AtomDescriptor &rhs);
    45 public:
    46   AtomAndDescriptor(const AtomAndDescriptor&);
    47   ~AtomAndDescriptor();
    48 
    49   virtual desc_ptr clone() const;
    50   virtual bool predicate(std::pair<int,atom*>);
    51 
    52 private:
    53   desc_ptr lhs;
    54   desc_ptr rhs;
    55 
    56   // private constructor to enforce construction through operators
    57   AtomAndDescriptor(const AtomDescriptor &_lhs, const AtomDescriptor &_rhs);
    58 };
    59 
    60 class AtomOrDescriptor : public AtomDescriptor
    61 {
    62   friend AtomOrDescriptor  operator||(const AtomDescriptor &lhs, const AtomDescriptor &rhs);
    63 public:
    64   AtomOrDescriptor(const AtomOrDescriptor &);
    65   ~AtomOrDescriptor();
    66 
    67   virtual desc_ptr clone() const;
    68   virtual bool predicate(std::pair<int,atom*>);
    69 
    70 private:
    71   desc_ptr lhs;
    72   desc_ptr rhs;
    73 
    74   // private constructor to enforce construction through operators
    75   AtomOrDescriptor(const AtomDescriptor &_lhs, const AtomDescriptor &_rhs);
    76 };
    77 
    78 class AtomNotDescriptor : public AtomDescriptor
    79 {
    80   friend AtomNotDescriptor operator!(const AtomDescriptor &arg);
    81 public:
    82   AtomNotDescriptor(const AtomNotDescriptor &);
    83   ~AtomNotDescriptor();
    84 
    85   virtual desc_ptr clone() const;
    86   virtual bool predicate(std::pair<int,atom*>);
    87 
    88 private:
    89   desc_ptr arg;
    90 
    91   // protected constructor to enforce construction through operators
    92   AtomNotDescriptor(const AtomDescriptor &_arg);
    93 };
     48// Functions to construct actual descriptors
     49AtomDescriptor AllAtoms();
     50AtomDescriptor NoAtoms();
    9451
    9552// no true short circuit, but the test of the second descriptor wont be done
    96 AtomAndDescriptor operator&&(const AtomDescriptor &lhs, const AtomDescriptor &rhs);
    97 AtomOrDescriptor operator||(const AtomDescriptor &lhs, const AtomDescriptor &rhs);
    98 AtomNotDescriptor operator!(const AtomDescriptor &arg);
     53AtomDescriptor operator&&(const AtomDescriptor &lhs, const AtomDescriptor &rhs);
     54AtomDescriptor operator||(const AtomDescriptor &lhs, const AtomDescriptor &rhs);
     55AtomDescriptor operator!(const AtomDescriptor &arg);
    9956
    10057#endif /* ATOMDESCRIPTOR_HPP_ */
  • src/Descriptors/AtomIdDescriptor.cpp

    rd1c5e2 r7a1ce5  
    77
    88#include "AtomIdDescriptor.hpp"
     9#include "AtomIdDescriptor_impl.hpp"
    910
    1011#include "atom.hpp"
     
    1213using namespace std;
    1314
    14 AtomIdDescriptor::AtomIdDescriptor(int _id) :
     15
     16AtomIdDescriptor_impl::AtomIdDescriptor_impl(int _id) :
    1517  id(_id)
    1618{}
    1719
    18 AtomIdDescriptor::~AtomIdDescriptor()
     20AtomIdDescriptor_impl::~AtomIdDescriptor_impl()
    1921{}
    2022
    21 bool AtomIdDescriptor::predicate(std::pair<int,atom*> atom) {
     23bool AtomIdDescriptor_impl::predicate(std::pair<int,atom*> atom) {
    2224  return atom.second->getId()==id;
    2325}
    2426
    25 AtomDescriptor::desc_ptr AtomIdDescriptor::clone() const{
    26   return desc_ptr(new AtomIdDescriptor(id));
     27AtomDescriptor AtomById(int id){
     28  return AtomDescriptor(AtomDescriptor::impl_ptr(new AtomIdDescriptor_impl(id)));
    2729}
    2830
  • src/Descriptors/AtomIdDescriptor.hpp

    rd1c5e2 r7a1ce5  
    1111#include "Descriptors/AtomDescriptor.hpp"
    1212
    13 class AtomIdDescriptor : public AtomDescriptor
    14 {
    15 public:
    16   AtomIdDescriptor(int _id);
    17   virtual ~AtomIdDescriptor();
    18 
    19   bool predicate(std::pair<int,atom*> atom);
    20 
    21   virtual desc_ptr clone() const;
    22 
    23 protected:
    24 #if 0
    25   atom *find();
    26   std::vector<atom*> findAll();
    27 #endif
    28 private:
    29   int id;
    30 };
     13AtomDescriptor AtomById(int id);
    3114
    3215#endif /* ATOMIDDESCRIPTOR_HPP_ */
  • src/World.cpp

    rd1c5e2 r7a1ce5  
    1818}
    1919
    20 atom* World::getAtom(AtomDescriptor &descriptor){
     20atom* World::getAtom(AtomDescriptor descriptor){
    2121  return descriptor.find();
    2222}
    2323
    24 vector<atom*> World::getAllAtoms(AtomDescriptor &descriptor){
     24vector<atom*> World::getAllAtoms(AtomDescriptor descriptor){
    2525  return descriptor.findAll();
    2626}
     
    3434
    3535
    36 World::World()
     36World::World() :
     37    dummyId(0)
    3738{}
    3839
     
    8788void World::registerAtom(atom *theAtom){
    8889  OBSERVE;
    89   atoms[theAtom->getId()] = theAtom;
     90  atoms[dummyId++] = theAtom;
    9091}
    9192
  • src/World.hpp

    rd1c5e2 r7a1ce5  
    2121class atom;
    2222class AtomDescriptor;
     23class AtomDescriptor_impl;
    2324
    2425class World : public Observable
    2526{
    26 friend class AtomDescriptor;
     27friend class AtomDescriptor_impl;
    2728public:
    2829
    2930  /***** getter and setter *****/
    3031  periodentafel* getPeriode();
    31   atom* getAtom(AtomDescriptor& descriptor);
    32   std::vector<atom*> getAllAtoms(AtomDescriptor& descriptor);
     32  atom* getAtom(AtomDescriptor descriptor);
     33  std::vector<atom*> getAllAtoms(AtomDescriptor descriptor);
    3334private:
    3435  periodentafel *periode;
     
    6364private:
    6465  MoleculeListClass *molecules;
     66
     67  // this is needed to assign unique IDs to atoms... so far
     68  // IDs are not assigned upon Atom creation, so we cannot query the ID
     69  // during construction. By using the dummy ID we can make sure all atoms
     70  // are actually stored in the map and don't overwrite each other.
     71  int dummyId;
    6572};
    6673
  • src/atom_particleinfo.hpp

    rd1c5e2 r7a1ce5  
    3535  ostream & operator << (ostream &ost) const;
    3636
    37   int getId();
     37  virtual int getId();
    3838
    3939private:
  • src/unittests/Makefile.am

    rd1c5e2 r7a1ce5  
    2929  ObserverTest \
    3030  CacheableTest \
     31  DescriptorUnittest \
    3132  ${MENUTESTS} 
    3233   
     
    107108CacheableTest_LDADD = ${ALLLIBS}
    108109
     110DescriptorUnittest_SOURCES = DescriptorUnittest.cpp DescriptorUnittest.hpp
     111DescriptorUnittest_LDADD = ${ALLLIBS}
     112
    109113#AUTOMAKE_OPTIONS = parallel-tests
    110114
Note: See TracChangeset for help on using the changeset viewer.