ReferenceMap-example.cpp

A map of references to base type scalars, arrays or std::string; changing values in either are equivalent operations. A class designed for objects returned by CastableIterator. Contains a pointer to external object.

/*
Copyright 2001 - 2017 Markus Peura, Finnish Meteorological Institute (First.Last@fmi.fi)
This file is part of Rack for C++.
Rack is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
Rack is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser Public License for more details.
You should have received a copy of the GNU General Public License
along with Rack. If not, see <http://www.gnu.org/licenses/>.
*/
/*
pikamake.sh drain/examples/ReferenceMap-example.cpp
REQUIRE: drain/{Caster,Castable,Log,Reference,RegExp,Sprinter,String,Type,TypeUtils,TextStyle}.cpp
*/
#include <iostream>
#include <stdexcept>
#include <vector>
#include "drain/util/ReferenceMap.h"
#include "drain/util/Rectangle.h"
int main(int argc, char **argv){
int i = 6;
// size_t i = 6;
float f;
std::string s="Hello, world!";
drain::UniTuple<double,3> tuple = {5.0, 123.45, 2.1}; // (5.0, 123.45, 2.1);
// std::pair<int,int> range(12,34);
struct person {
std::string name;
int id;
};
person p;
p.name = "Smith";
p.id = 18267;
p.tuple.set(1.0, 2.0, 3.0);
p.rect.set(20, 60, 30, 70);
p.range.min = 0.1;
p.range.max = 4.7;
// std::cerr << "empty map: " << r << std::endl;
r.link("i", i, "count");
r.link("f", f=4.56f, "float");
r.link("s", s, "name");
r.link("v", tuple, "params").fillArray = true;
//r.link("r", range, "min:max").fillArray = true;
r.link("name", p.name);
r.link("id", p.id);
r.link("vect", p.tuple); // what if resize changes address?
r.link("range",p.range.tuple());
//r.toJSON();
//p.vect.resize(257, 5.12);
p.tuple = {1.1, 2.1, 3.1}; //, 2.1, 2.1, 2.1, 2.1, 2.1, 2.1};
//r.toJSON();
//r["vect"].toJSON();
//std::cerr << drain::Type::getTypeChar(r["i"].getType()) << std::endl;
std::cout << "ReferenceMap rCopy\n";
person pCopy;
//pCopy.vect.resize(3, 333);
pCopy.tuple = {333, 222, 111};
rCopy.copyStruct(r, p, pCopy);
//rCopy.toJSON();
ref.link(p.range.tuple()); //
std::cout << "ReferenceMap rCopy\n";
std::cout << ref << '\n';
if (argc < 2){
std::cout << "ReferenceMap demo\n";
// std::cout << "Creates entries: int i, float f and std::string s. Assign these on the command line.\n";
std::cerr << "map: " << r << '\n';
std::cout << "See also: SmartMap-example\n";
std::cout << "Example: " << argv[0] << " 123,0.25,hello s=hello i=5.67,hello s=1,s=test\n";
// In this example, as struct is used (to illustrate copy const)
struct record {
std::string s = "Hello, world!";
int i = 123;
float f = 4.567f;
bool flag = true;
std::vector<float> fv;
};
record orig;
orig.fv.push_back(1.23);
orig.fv.push_back(4.56);
refOrig.link("i", orig.i = 56);
refOrig.link("f", orig.f = 78.9);
refOrig.link("s", orig.s = "ABC");
refOrig.link("flag", orig.flag = false);
int raimo = 12345;
refOrig.link("outlier", i);
refOrig.link("raymond", raimo);
record copy;
refCopy.copyStruct(refOrig, orig, copy, drain::ReferenceMap::LINK);
refCopy["outlier"] = 129; // shared
std::cout << copy.s << ',' << copy.i << ','<< copy.f << ','<< copy.flag << '\n';
std::cout << refOrig << '\n';
std::cout << refCopy << '\n';
return 1;
}
std::cerr << "map: " << r << '\n';
std::cerr << "keys: " << r.getKeys() << '\n';
std::cerr << "map: " << r << '\n';
std::cerr << "copy: " << r2 << '\n';
r3 = r;
std::cerr << "assign: " << r3 << '\n';
std::cout << "Initial values: " << i << ',' << f << ',' << s << '\n';
//std::cout << r << '\n';
/*
r["i"] = 456.789; // truncates to 456.
std::cout << r << '\n';
std::cout << '\n';
*/
std::cout << '\n';
std::cout << "--- Traversing user parameters --- \n";
std::cout << '\n';
for (int i = 1; i < argc; ++i) {
std::cout << "assign:" << argv[i] << '\n';
try {
const char * v = argv[i];
r.setValues(v);
// r["nonexistent"] = 456.789; // Throws std::exception
}
catch (std::runtime_error & e) {
std::cerr << '\t' << e.what() << std::endl;
//return 0;
std::cout << "\t(leaving remaining values unassigned)\n";
}
std::cout << "result: " << r << '\n';
std::cout << '\n';
}
drain::Logger mout(__FILE__, __FUNCTION__);
drain::getLog().setVerbosity(i);
mout.debug3() << "map: " << r << mout.endl;
mout.debug2() << "map: " << r << mout.endl;
mout.debug() << "map: " << r << mout.endl;
mout.info() << "map: " << r << mout.endl;
mout.note() << "map: " << r << mout.endl;
mout.warn() << "map: " << r << mout.endl;
/*
mout.error() << "map: " << r << mout.endl;
mout.fatal() << "map: " << r << mout.endl;
mout.alert() << "map: " << r << mout.endl;
mout.quit() << "map: " << r << mout.endl;
*/
return 0;
}
/*
//r.setValues(" s = Test , i , f = 0.12 3 ");
r.setValues("s=Test,i,f=0.12 3");
std::cout << "s='" << s << "'" << std::endl;
std::cout << "i=" << i << std::endl;
std::cout << "f=" << f << std::endl;
std::cout << std::endl;
r.setValues("New,2, 0.456");
std::cout << "s=" << s << std::endl;
std::cout << "i=" << i << std::endl;
std::cout << "f=" << f << std::endl;
std::cout << std::endl;
try {
r["nonexistent"] = 456.789; // Throws std::exception
}
catch (std::runtime_error e) {
std::cerr << e.what() << std::endl;
return 0;
}
//std::cout << "finally" << r << std::endl;
std::cout << "Calling an non-existent entry is ok in \em lenient mode...";
ReferenceMap mapFlexible(false);
mapFlexible["nonexistent"] = 456.789; // Does nothing.
std::cout << mapFlexible["nonexistent"] << std::endl; // Prints an empty std::string.
std::cout << (int) mapFlexible["nonexistent"] << std::endl; // Prints zero.
std::cout << mapFlexible << std::endl;
*/
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition: Log.h:308
Logger & note(const TT &... args)
For top-level information.
Definition: Log.h:485
Logger & warn(const TT &... args)
Possible error, but execution can continue.
Definition: Log.h:426
Logger & debug(const TT &... args)
Public, yet typically used "internally", when TIMING=true.
Definition: Log.h:676
Logger & debug2(const TT &... args)
Debug information.
Definition: Log.h:686
Definition: ReferenceMap.h:207
void copyStruct(const ReferenceMap &m, const T &src, T &dst, extLinkPolicy policy=RESERVE)
Experimental. Copies references and values of a structure to another.
Definition: ReferenceMap.h:399
@ LINK
Definition: ReferenceMap.h:384
void setValues(const std::string &entries, char assignmentSymbol='=', char separatorSymbol=0)
Sets values. If strictness==STRICTLY_CLOSED, throws exception if tries to assign a non-existing entry...
Definition: SmartMap.h:311
VariableT is a final class applied through typedefs Variable, Reference and FlexibleVariable.
Definition: VariableT.h:87