TBTK
Need a break? Support the development by playing Polarity Puzzles
TBTK::Resource Class Reference

Read and write string resources from file, URL, etc. More...

#include <Resource.h>

Public Member Functions

 Resource ()
 
 ~Resource ()
 
void setData (const std::string &data)
 
const std::string & getData () const
 
void write (const std::string &uri)
 
void read (const std::string &uri)
 

Detailed Description

Read and write string resources from file, URL, etc.

The Resource provides the means for reading input data from files, URL's etc. It thus provides a unified mechanism for reading data that is agnostic of the underlying storage medium. Files can be accessed from the local disc or through any of the protocols supported by cURL (https://curl.haxx.se/). At the moment, it is only possible to write to a local file.

Resources are particularly useful in combination with serialization for storing and reading in TBTK objects. An empty resource is created using

Resource resource;

Once created, it is possible to read a serialized Model from file

resource.read("Model.json");

or a URL

resource.read("http://www.second-quantization.com/v2/ExampleModel.json");

Finally, we get the content of the resource using 'resource.getData()' and can use it to reconstruct a Model from its serialization

Model model(resource.getData(), Serializable::Mode::JSON);

Example

#include "TBTK/Model.h"
#include "TBTK/Resource.h"
#include "TBTK/Streams.h"
#include "TBTK/TBTK.h"
using namespace TBTK;
int main(){
//Write string to file.
Resource resource;
resource.setData("Hello quantum world!");
resource.write("Message.txt");
//Create a Model.
Model model;
model << HoppingAmplitude(-1, {0}, {1}) + HC;
model.construct();
//Write a serialized version of the Model to file.
resource.setData(model.serialize(Serializable::Mode::JSON));
resource.write("MyModel.json");
//Read string message from file.
resource.read("Message.txt");
Streams::out << "Message:\n";
Streams::out << resource.getData() << "\n\n";
//Read Model from file.
resource.read("MyModel.json");
Model storedModel(resource.getData(), Serializable::Mode::JSON);
Streams::out << "Stored Model:\n";
Streams::out << storedModel << "\n";
return 0;
}

Output

Message:
Hello quantum world!
Stored Model:
Model
Temperature: 0K (0 n.u.)
Chemical potential: 0eV (0 n.u.)
Statistics: Fermi-Dirac
Basis size: 2

Constructor & Destructor Documentation

◆ Resource()

TBTK::Resource::Resource ( )

Constructor.

◆ ~Resource()

TBTK::Resource::~Resource ( )

Destructor.

Member Function Documentation

◆ getData()

const std::string & TBTK::Resource::getData ( ) const
inline

Get the data from the resource.

Returns
The content of the resource.

◆ read()

void TBTK::Resource::read ( const std::string &  uri)

Read a resource from a source.

Parameters
uriThe unique resource identifier (URI) of the source. Should be the name of the file when reading from a local file.

◆ setData()

void TBTK::Resource::setData ( const std::string &  data)
inline

Set the data of the Resource.

Parameters
dataThe data the resource should contain.

◆ write()

void TBTK::Resource::write ( const std::string &  uri)

Write the resource to destination. Currently the destination can only be a local file.

Parameters
uriThe unique resource identifier (URI) of the destination. Should be the name of the file when writing to a local file.

The documentation for this class was generated from the following file:
TBTK::Resource::write
void write(const std::string &uri)
TBTK::Model
Container of Model related information.
Definition: Model.h:57
Model.h
Container of Model related information.
TBTK::Resource
Read and write string resources from file, URL, etc.
Definition: Resource.h:65
Streams.h
Streams for TBTK output.
TBTK::Model::serialize
std::string serialize(Mode mode) const
TBTK::Resource::read
void read(const std::string &uri)
TBTK::Initialize
void Initialize()
TBTK::Resource::Resource
Resource()
TBTK::HoppingAmplitude
Hopping amplitude from state 'from' to state 'to'.
Definition: HoppingAmplitude.h:53
TBTK::Streams::out
static std::ostream out
Definition: Streams.h:70
Resource.h
Read and write string resources from file, URL, etc.
TBTK::Resource::setData
void setData(const std::string &data)
Definition: Resource.h:116
TBTK.h
TBTK initialization.
TBTK::Model::construct
void construct()
TBTK::Resource::getData
const std::string & getData() const
Definition: Resource.h:120