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 void
00092 print(const Space&, const Choice&, unsigned int,
00093 std::ostream& o) const {
00094 o << "FunctionBranch(" << f << ")";
00095 }
00097 virtual Actor* copy(Space& home, bool share) {
00098 return new (home) FunctionBranch(home,share,*this);
00099 }
00101 static BrancherHandle post(Home home, void (*f)(Space&)) {
00102 return *new (home) FunctionBranch(home,f);
00103 }
00104 };
00105
00106
00107 BrancherHandle
00108 branch(Home home, void (*f)(Space& home)) {
00109 if (home.failed())
00110 return BrancherHandle();
00111 return FunctionBranch::post(home,f);
00112 }
00113
00114 }
00115
00116