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 FunctionBranch : public Brancher {
00046 protected:
00048 class Description : public Choice {
00049 public:
00051 Description(const Brancher& b, unsigned int a);
00053 virtual size_t size(void) const;
00055 virtual void archive(Archive& e) const;
00056 };
00058 SharedData<std::function<void(Space& home)>> f;
00060 bool done;
00062 FunctionBranch(Home home, std::function<void(Space& home)> f0);
00064 FunctionBranch(Space& home, bool share, FunctionBranch& b);
00065 public:
00067 virtual bool status(const Space& home) const;
00069 virtual const Choice* choice(Space& home);
00071 virtual const Choice* choice(const Space& home, Archive& a);
00073 virtual ExecStatus commit(Space& home, const Choice& ch, unsigned int a);
00075 virtual void print(const Space&, const Choice&, unsigned int,
00076 std::ostream& o) const;
00078 virtual Actor* copy(Space& home, bool share);
00080 static void post(Home home, std::function<void(Space& home)> f);
00082 virtual size_t dispose(Space& home);
00083 };
00084
00085 forceinline
00086 FunctionBranch::Description::Description(const Brancher& b, unsigned int a)
00087 : Choice(b,a) {}
00088 size_t
00089 FunctionBranch::Description::size(void) const {
00090 return sizeof(Description);
00091 }
00092 void
00093 FunctionBranch::Description::archive(Archive& e) const {
00094 Choice::archive(e);
00095 }
00096
00097 forceinline
00098 FunctionBranch::FunctionBranch(Home home,
00099 std::function<void(Space& home)> f0)
00100 : Brancher(home), f(f0), done(false) {
00101 if (!f())
00102 throw InvalidFunction("FunctionBranch::FunctionBranch");
00103 home.notice(*this,AP_DISPOSE);
00104 }
00105 forceinline
00106 FunctionBranch::FunctionBranch(Space& home, bool share, FunctionBranch& b)
00107 : Brancher(home,share,b), done(b.done) {
00108 f.update(home,share,b.f);
00109 }
00110 bool
00111 FunctionBranch::status(const Space&) const {
00112 return !done;
00113 }
00114 const Choice*
00115 FunctionBranch::choice(Space&) {
00116 assert(!done);
00117 return new Description(*this,1);
00118 }
00119 const Choice*
00120 FunctionBranch::choice(const Space&, Archive&) {
00121 return new Description(*this,1);
00122 }
00123 ExecStatus
00124 FunctionBranch::commit(Space& home, const Choice&, unsigned int) {
00125 done = true;
00126 GECODE_VALID_FUNCTION(f());
00127 f()(home);
00128 return home.failed() ? ES_FAILED : ES_OK;
00129 }
00130 void
00131 FunctionBranch::print(const Space&, const Choice&, unsigned int,
00132 std::ostream& o) const {
00133 o << "FunctionBranch()";
00134 }
00135 Actor*
00136 FunctionBranch::copy(Space& home, bool share) {
00137 return new (home) FunctionBranch(home,share,*this);
00138 }
00139 forceinline void
00140 FunctionBranch::post(Home home, std::function<void(Space& home)> f) {
00141 if (!f)
00142 throw InvalidFunction("FunctionBranch::post");
00143 (void) new (home) FunctionBranch(home,f);
00144 }
00145 size_t
00146 FunctionBranch::dispose(Space& home) {
00147 home.ignore(*this,AP_DISPOSE);
00148 f.~SharedData<std::function<void(Space& home)>>();
00149 (void) Brancher::dispose(home);
00150 return sizeof(*this);
00151 }
00152
00153 void
00154 branch(Home home, std::function<void(Space& home)> f) {
00155 FunctionBranch::post(home,f);
00156 }
00157
00158 }
00159
00160