00001
00002
00003
00005
00006
00008 #include <iostream>
00009 #include "Config/Param.h"
00010 #include "Config/Config.h"
00011 #include "Config/Observer.h"
00012 #include "Config/Table.h"
00013 #include "Config/Exception.h"
00014 #include "Config/ConfigBuilder.h"
00015 #include "Config/ConfigEditor.h"
00016
00017 class TestBuilder : public cfg::ConfigBuilder
00018 {
00019 public:
00020 void BuildCfg1() {
00021 cfg::Config* cfg1 =new cfg::Config("Cfg1","test",__FILE__);
00022 cfg1->AdoptParam(new cfg::Param("i",86, "an integer"));
00023 cfg1->AdoptParam(new cfg::Param("f",86.6, "a float" ));
00024 cfg1->AdoptParam(new cfg::Param("s",std::string("D.Adams"),"Agent 86" ));
00025 this->Publish(cfg1);
00026 }
00027 void BuildCfg2() {
00028 cfg::Config* cfg2 =new cfg::Config("Cfg2","default",__FILE__);
00029 cfg2->AdoptParam(new cfg::Param("i",99, "an integer"));
00030 cfg2->AdoptParam(new cfg::Param("f",99.9, "a float" ));
00031 cfg2->AdoptParam(new cfg::Param("s",std::string("B.Feldon"),"Agent 99" ));
00032 this->Publish(cfg2);
00033 }
00034 private:
00035 };
00036
00037
00038
00039 class TestEditor : public cfg::ConfigEditor
00040 {
00041 public:
00042 TestEditor(cfg::Config* cfg) : cfg::ConfigEditor(cfg) { }
00043 int EditConfig(cfg::Config& c) {
00044 std::cerr << "| From: "
00045 << c("i") << ","
00046 << c("f") << ","
00047 << c("s")
00048 << std::endl;
00049 c.Par("i").Set(666);
00050 c.Par("f").Set(66.6);
00051 c.Par("s").Set(std::string("KAOS"));
00052 std::cerr << "| To: "
00053 << c("i") << ","
00054 << c("f") << ","
00055 << c("s")
00056 << std::endl;
00057 return 1;
00058 }
00059 private:
00060 };
00061
00062
00063
00064 class TestObserver : public cfg::Observer
00065 {
00066 public:
00067 TestObserver() {
00068 this->SetWatch("Cfg1","test");
00069 this->SetWatch("Cfg2","default");
00070 }
00071
00072 void Update(const cfg::Config& c) {
00073 std::cerr << "% TestObserver:: Update from "
00074 << c.GetName() << "." << c.GetVersion() << std::endl;
00075 std::cerr << "% "
00076 << c("i") << ","
00077 << c("f") << ","
00078 << c("s") << ","
00079 << std::endl;
00080 }
00081 };
00082
00083
00084
00085 int main()
00086 {
00087 TestBuilder b;
00088 b.BuildCfg1();
00089 b.BuildCfg2();
00090
00091 std::cerr << "Should see updates as observer registers itself.\n";
00092 TestObserver obs;
00093
00094 std::cerr << "Should see updates as configurations are edited.\n";
00095
00096 cfg::Config* cfg1 = cfg::Table::Instance().GetConfig("Cfg1","test");
00097 if (cfg1==0) {
00098 std::cerr << "no Cfg1"; return 1;
00099 }
00100 TestEditor e1(cfg1);
00101 e1.Edit();
00102
00103 cfg::Config* cfg2 = cfg::Table::Instance().GetConfig("Cfg2","default");
00104 if (cfg2==0) {
00105 std::cerr << "no Cfg2"; return 1;
00106 }
00107 try {
00108 TestEditor e2(cfg2);
00109 e2.Edit();
00110 }
00111 catch (cfg::Exception& e) {
00112 std::cerr << "*=> Exception caught: Correct behavior.\n";
00113 }
00114 return 0;
00115 }
00116