Skip to content

Variable

Architector edited this page Apr 18, 2019 · 13 revisions

This container is a buffer abstraction represents a regular variable or class object.

Constructors

ecl::var<T>();
ecl::var<T>(const& T value);
ecl::var<T>(ecl::ACCESS);
ecl::var<T>(const T& value, ecl::ACCESS);

Example:

ecl::var<int> a; // not-initialized
ecl::var<int> b = 5; // b = 5
ecl::var<float> c(7.0f, ecl::ACCESS::READ); // c = 7.0f (read-only)

Getters / setters

To get /set value:

const T& getValue() const;
void setValue(const T& value);

Operators

Also you can use overloaded operators!

  1. Increment / dicrement:
var++;
var--;

Example:

ecl::var<int> a = 3;
a++; // 4
a--; // 3
  1. Equality:
var = const T&;
var += const T&;
var -= const T&;
var *= const T&;
var /= const T&;

Example:

ecl::var<int> a = 3;
ecl::var<int> b = 5;

a += 2; // a = 5
a -= b; // a = 0

b = 3;
  1. Friend:
var<T>& = var + const T&;
var<T>& = var - const T&;
var<T>& = var * const T&;
var<T>& = var / const T&;

Example:

ecl::var<int> a = 3;
ecl::var<int> b = 5;

b = a * 3; // b = 9
  1. Casting:
operator T&();

Note: So, if any function takes a reference or const reference, or value of T you may pass var<T> instead!

Example:

void foo(int a){
    std::cout << a << std::endl; // 5
}

ecl::var<int> a = 5;
foo(a);
  1. Out to stream:
ostream& << const var<T>&;

Example:

ecl::var<double> a = 6.0;
std::cout << a; // 6.0

Copy / move

You can use (copy / move) (constructors / operator=):

  1. Copy:
var<T>(const var<T>&);
var = const var<T>&;

Example:

ecl::var<int> a;
ecl::var<int> b = 5;
ecl::var<int> c = b; // copy (create buffer and copy value on host)

a = b; // copy
  1. Move:
var<T>(var<T>&&);
var = std::move(var<T>&&);

Example:

ecl::var<int> a = 2;
ecl::var<int> b = std::move(a); // now 'a' is empty, 'b' grabs all data

ecl::var<int> c = 4;
c = std::move(b); // now 'b' is empty, 'c' grabs all data

Note: All these operation done only on host. To know how to work with devices, look at Computer section.

Clone this wiki locally