Includes (lost update to coding)

I found this update sitting and waiting to be finished for almost a year!

 

We are now moving to the real code. The main just covered the final product and my style decisions.

Starting with the header code:

/**

Copyright 2018 Michael R. Wild

Permission is hereby granted, free of charge, to any person obtaining a copy of
 this software and associated documentation files (the "Software"), to deal in
 the Software without restriction, including without limitation the rights to
 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 the Software, and to permit persons to whom the Software is furnished to do so,
 subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
 copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE

@file mcp.h
 @author Michael R. Wild
 @date 3/6/2018
 @version 0.0.1

@brief Master Control Classes Header

@section DESCRIPTION

The header file for the MCP.

@section CHANGES

20180306 Creation
 20180307 Added example class

*/
 //=================================
 // include guard
 #ifndef __mcp_H_INCLUDED__
 #define __mcp_H_INCLUDED__

//=================================
 // forward declared dependencies

//=================================
 // included dependencies
 #include 
 #include 
 #include 
 #include <string>
 #include <set>
 #include <exception>
 #include <iostream>

//=================================
 // revisions of namespace

namespace pt = boost::property_tree;
 //=================================
 // the actual classes

/*! \class MyClass
 \brief Example class.

Just an example class.
 */

class MyClass // Example standard
 {
 private:
 int a;

public:
 void set(int n) { a = n;}
 int get() { return a;}
 };

class MCPParms
 {
 private:
 pt::ptree tree;
 std::string parmfile;

public:
 MCPParms(const std::string &filename) ;
 void load() ;
 std::string get(const std::string &mykey) ;
 void put(const std::string &mykey, const std::string &myvalue) ;
 void save() ;

};

#endif // __MCP_H_INCLUDED__

I first had to remember how to control the code to prevent recursive calls for the include. I have to admit that I looked that up as I had no memory about how to control includes in C++. I also left the example in for now. Lastly, I decided that I would not create one set of includes for everything different class. That seemed to me to be a bit over-the-top, but that is the standard for many. Sticking with the minimal standards, I included the minimal to get my work done. I did declare a namespace for the tree as I just did not want to type boost over and over.

I decided to do an example where I would read in an XML file with all of the parameters to run the program. Now there are other ways to do this, but I liked this idea of XML to tree, thus the name MCPParms.

The work is done in the CPP file as you would expect.  Sorry, I am not following my own standards of updating all of the items in the header. It is an example program.

/** 

Copyright 2018 Michael R. Wild

Permission is hereby granted, free of charge, to any person obtaining a copy of 
this software and associated documentation files (the "Software"), to deal in 
the Software without restriction, including without limitation the rights to 
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so, 
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all 
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE


@file mcp.cpp
@author Michael R. Wild
@date 3/6/2018 
@version 0.0.1 

@brief Master Control Classes
 
 @section DESCRIPTION
 
 This is the class definitions for MCP
 
  @section CHANGES
 
 20180306 Creation
 
 */

#include "mcp.h"

MCPParms::MCPParms(const std::string &myfilename)
{
/*
Decided to just save the file name instead of loading it. The process of loading 
could throw so can't do it here without a lot of mystery code to handle an 
exception on declare. So split up load and constructor.
*/

  parmfile = myfilename;
  //std::cout << "File name: " << parmfile << std::endl;
}

void MCPParms::load()
{
/* Must put "load" in a try to handle exceptions. The file and the XML process
can both throw.
*/

  pt::read_xml(parmfile, tree); // exceptions must be handled!
}


std::string MCPParms::get(const std::string &myname)
{ 
  
  return tree.get(myname);

}

void MCPParms::put(const std::string &myname, const std::string &myvalue)
{
  
  //std::cout << "myname: " << myname << std::endl;
  tree.put(myname, myvalue);
  
  return;

}

void MCPParms::save()
{
  //std::cout << "save " << parmfile << std::endl;
  pt::write_xml(parmfile, tree);
}

The code can read an XML file, create a tree of the values, retrieve values from the tree, and write the tree back to a file. This is the start of my blockchain thinking. I plan to create an XML file for each blockchain entry or bitcoin if you like. That the new software will manage files and not use a database. My idea is to have an XML structure that drives the process and that to live in a file. The shared ledger then is just a bunch of XML files.

Hope the code is interesting and the idea is also interesting. That is all the code I have done. So I may take a few days to get another update done. I hope that is OK.

Nearly forgot! Here is the run of the program in Cloud9. So cool to just run this stuff somewhere in the cloud for the cost of one PNW coffee for a whole month of use!

Running /home/ec2-user/environment/mcpmain.cpp
Master Control Program
Local Time: Thu Apr 26 05:51:54 2018

Read in MCP controls
Success: Master Control Program 0.0.1
Success: This is a test
Saved!
 
Process exited with code: 0

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s