It's possible combinate augeas & template? - templates

It's possible combinate augeas & template?. The objective its a static part of test inside a file an another variable with the augeas.
I'm reading the documentation and examples but I think its not possible.
If its possible, can you put an example?
thanks

If you manage a file with the file type, you need to manage it entirely and you cannot manage parts of it with another type, such as augeas. In order to manage files by parts, you may want to consider using one of the concat modules instead (Puppet Labs' or Onyxpoint's)

Related

Where is the best place to put const in C++?

I have done C# and C, but still, when I come to this C++ project, I am a little stucked for this little code style problem.
I have a couple of const variables(might be strings or numbers) in a C++ program, and I want to gather them in one place and access them from other classes.
For example, in C# and java, we can have a resource file or a config file to store all those data, or maybe just a singleton or static class.
But what is the best way I should do it with C++? should I put the const variables in a header file or make a class like JAVA?
IMO this is one of the shortcoming of C# and Java - everything has to be inside a class.
In C++ you have two three options:
inside a class
in a namespace
free variables (don't)
If your constants are logically related to a class, put them in the class. Otherwise, use a namespace.

how to use types created from persist template?

In my Model.hs I can successfully use the data types that correspond to my data tables that I describe in config/models file.
I would like to use these types in my other files, for example, in Foundation.hs.
Is there a module that I need to import or a function to call from the file where I need to use the types?
I do not know why my first answer was deleted. Again, these types are visible in other files without importing anything. I posted the question too soon.

Flexible application configuration in C++

I am developing a C++ application used to simulate a real world scenario. Based on this simulation our team is going to develop, test and evaluate different algorithms working within such a real world scenrio.
We need the possibility to define several scenarios (they might differ in a few parameters, but a future scenario might also require creating objects of new classes) and the possibility to maintain a set of algorithms (which is, again, a set of parameters but also the definition which classes are to be created). Parameters are passed to the classes in the constructor.
I am wondering which is the best way to manage all the scenario and algorithm configurations. It should be easily possible to have one developer work on one scenario with "his" algorithm and another developer working on another scenario with "his" different algorithm. Still, the parameter sets might be huge and should be "sharable" (if I defined a set of parameters for a certain algorithm in Scenario A, it should be possible to use the algorithm in Scenario B without copy&paste).
It seems like there are two main ways to accomplish my task:
Define a configuration file format that can handle my requirements. This format might be XML based or custom. As there is no C#-like reflection in C++, it seems like I have to update the config-file parser each time a new algorithm class is added to project (in order to convert a string like "MyClass" into a new instance of MyClass). I could create a name for every setup and pass this name as command line argument.
The pros are: no compilation required to change a parameter and re-run, I can easily store the whole config file with the simulation results
contra: seems like a lot of effort, especially hard because I am using a lot of template classes that have to be instantiated with given template arguments. No IDE support for writing the file (at least without creating a whole XSD which I would have to update everytime a parameter/class is added)
Wire everything up in C++ code. I am not completely sure how I would do this to separate all the different creation logic but still be able to reuse parameters across scenarios. I think I'd also try to give every setup a (string) name and use this name to select the setup via command line arg.
pro: type safety, IDE support, no parser needed
con: how can I easily store the setup with the results (maybe some serialization?)?, needs compilation after every parameter change
Now here are my questions:
- What is your opinion? Did I miss
important pros/cons?
- did I miss a third option?
- Is there a simple way to implement the config file approach that gives
me enough flexibility?
- How would you organize all the factory code in the seconde approach? Are there any good C++ examples for something like this out there?
Thanks a lot!
There is a way to do this without templates or reflection.
First, you make sure that all the classes you want to create from the configuration file have a common base class. Let's call this MyBaseClass and assume that MyClass1, MyClass2 and MyClass3 all inherit from it.
Second, you implement a factory function for each of MyClass1, MyClass2 and MyClass3. The signatures of all these factory functions must be identical. An example factory function is as follows.
MyBaseClass * create_MyClass1(Configuration & cfg)
{
// Retrieve config variables and pass as parameters
// to the constructor
int age = cfg->lookupInt("age");
std::string address = cfg->lookupString("address");
return new MyClass1(age, address);
}
Third, you register all the factory functions in a map.
typedef MyBaseClass* (*FactoryFunc)(Configuration *);
std::map<std::string, FactoryFunc> nameToFactoryFunc;
nameToFactoryFunc["MyClass1"] = &create_MyClass1;
nameToFactoryFunc["MyClass2"] = &create_MyClass2;
nameToFactoryFunc["MyClass3"] = &create_MyClass3;
Finally, you parse the configuration file and iterate over it to find all the entries that specify the name of a class. When you find such an entry, you look up its factory function in the nameToFactoryFunc table and invoke the function to create the corresponding object.
If you don't use XML, it's possible that boost::spirit could short-circuit at least some of the problems you are facing. Here's a simple example of how config data could be parsed directly into a class instance.
I found this website with a nice template supporting factory which I think will be used in my code.

C++ - parameter question

I am looking for some simple and efficient parameter container that would act like a in-memory-xml file representation (or ini-file, as another sample).
I mean, basically it could store sections and sets of parameters for each section, have easy accessors like GetValue("ParameterName") and simple return value casting.
It would be great if it is serializable.
I wrote something like that yesterday and, well, it suits my needs, but probably there is something more convenient and flexible available?
Maybe some kind of parameter map in boost?
Thank you
Take a look at boost::program_options. It does what you want and more: INI file parsing, environment variables parsing, commandline options parsing and extensibility.
Have you considered std::map<>?
Dunno if it's overkill or not, but the Message class in MUSCLE does all of the things you listed above. You can use it to serialize any kind of data (structured or not), or use it as an in-memory container for parsed .ini style config files via ParseFile()/UnparseFile().
You can use Boost.PropertyTree.
It reads and writes xml and ini files.
It stores the parameters as a tree and you can use dot notation to access the values:
std::string value = pt.get<std::string>("debug.filename");
You can also insert new values using:
pt.put("debug.filename", fileName);

Parsing huge data with c++

In my job, i need to parse different kind of data files from different data sources.Sometimes i parse them by writing directly c++ code (with the help of qt and boost:D), sometimes manually with a helper program.
I must note that data types are so different from each other it is so hard to create common a interface for all of them. But i want to do this job in a more generic way.I am planning to write a library to convert them and it should be easy to add new parser utility in future.I am also planning to use other helper programs inside my program, not manually.
My question is what kind of an architecture or pattern do you suggest, Basic condition is library must be extendable via new classes or dll's and also configurable.
By the way data can be in text, ascii or something like CSV(comma seperated values) and most of them are specific for a certain data.
Not to blow my own trumpet, but my small Open Source utility CSVfix has an extensible architecture based on deriving new C++ classes with a very simple interface. I did consider using a plugin-architecture with DLLs but it seemed like overkill for such a simple utility . If interested, you can get the binaries & sources here.
I'd suggest a 3-part model, where the common data-format is a String which should be able to contain every value:
Reader: In this layer the values are read from the source (ie. CSV-file) using some sort of file-format-descriptor. The values are then stored in some sort of intermediate data structure.
Connector/Converter: This layer is responsible for mapping the reader-data to the writer-fields.
Writer: This layer is responsible for writing a specific data structure to the target (ie. another file-format or a database).
This way you can write different Readers for different input files.
I think the hardest part would be creating the definition of the intermediate storage format/structure so that it is future-proof and flexible.
One method I used for defining data structure in my datafile read/write classes is to use std::map<std::string, std::vector<std::string>, string_compare> where the key is the variable name and the vector of strings is the data. While this is expensive in memory, it does not lock me down to only numeric data. And, this method allows for different lengths of data within the same file.
I had the base class implement this generic storage, while the derived classes implemented the reader/writer capability. I then used a factory to get to the desired handler, using another class that determined the file format.