branch.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #include <gecode/kernel.hh>
00041
00042 namespace Gecode {
00043
00045 class GECODE_KERNEL_EXPORT FunctionBranch : public Brancher {
00046 protected:
00048 class GECODE_KERNEL_EXPORT Description : public Choice {
00049 public:
00051 Description(const Brancher& b, unsigned int a) : Choice(b,a) {}
00053 virtual size_t size(void) const { return sizeof(Description); }
00055 virtual void archive(Archive& e) const {
00056 Choice::archive(e);
00057 }
00058 };
00060 void (*f)(Space&);
00062 bool done;
00064 FunctionBranch(Home home, void (*f0)(Space&))
00065 : Brancher(home), f(f0), done(false) {}
00067 FunctionBranch(Space& home, bool share, FunctionBranch& b)
00068 : Brancher(home,share,b), f(b.f), done(b.done) {}
00069 public:
00071 virtual bool status(const Space&) const {
00072 return !done;
00073 }
00075 virtual const Choice* choice(Space&) {
00076 assert(!done);
00077 return new Description(*this,1);
00078 }
00080 virtual const Choice* choice(const Space&, Archive&) {
00081 return new Description(*this,1);
00082 }
00084 virtual ExecStatus
00085 commit(Space& home, const Choice&, unsigned int) {
00086 done = true;
00087 f(home);
00088 return home.failed() ? ES_FAILED : ES_OK;
00089 }
00091 virtual Actor* copy(Space& home, bool share) {
00092 return new (home) FunctionBranch(home,share,*this);
00093 }
00095 static void post(Home home, void (*f)(Space&)) {
00096 (void) new (home) FunctionBranch(home,f);
00097 }
00098 };
00099
00100
00101 void
00102 branch(Home home, void (*f)(Space& home)) {
00103 if (home.failed())
00104 return;
00105 FunctionBranch::post(home,f);
00106 }
00107
00108 }
00109
00110