Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Thesis / Thesis.docx
Скачиваний:
3
Добавлен:
20.12.2024
Размер:
998.96 Кб
Скачать
    1. Coordinates structure implementation

As stated earlier Coordinates structure stores two double fields longitude and

latitude. Define default parameter constructor to initialize them with values and parameterless constructor to postpone the initialization. Define getters and the instantiate implementation is finished.

The static implementation includes method to convert degrees to radians because equirectangularApproximation requires cosines values and they take degrees represented as radians. Method to calculate distance between two coordinate’s objects just duplicates the formula and that’s it.

    1. StationInfo structure implementation

StationInfo contains four fields, string name, Clocks time, int platform and Coordinates coords. Initialize default and initialization constructors. Boolean method to check whether the entered coordinates are in the map of coordinates for each city. Check that like this.

{

const auto& it = StaticData::cityCoordinates.find(str);

if (it != StaticData::cityCoordinates.end())

this->coords = Coordinates(it->second);

}

Overload input and output operators with checking for the city’s coordinates to be in the map. We can do that by simple ‘if’ checking because validateCoordinates returns us false if the coordinates were not found in the map and then ask user to enter them.

{

if (!other.validateCoordinates(name))

{

std::cout << "The coordinates of the given city was not found\n" << "Please provide them.\n";

double latitude, longitude;

...

StaticData::cityCoordinates.emplace(name, latitude, longitude);

}

}

Implementation of the class is simple and so does not require any comments. The only comment is why we use structure rather than class is because our data type is very simple and contains a little of functionalitites. Only to show that I use strcture.

    1. IEmissionable, iSerializable and iCloneable implementation

IEmissionable interface does not require any comments everything was shown on the figure 2.16 and no extra functionality is defined.

ISerializible can be mentioned a little only because of its template arguments but still it has very simple and trivial realization of defining pure virtual methods and using template arguments.

ICloneable interface, nevertheless, worth taking a glance at. It takes only pointer types data as template. The question is how can we enforce a class to obligate that. There are four ways to do that: by using std::enable_if, which is not recommendable, requires, static_assert and SFINAE. The closest to me way is static_assert as it’s the most compehensible for everyone. The checking looks like this.

template<typename TypeToClone>

class ICloneable

{

static_assert(std::is_pointer_v<TypeToClone>,

"Type that implements ICloneable must be a pointer");

...

};

We can see that static_assert checks whether TypeToClone is a pointer or not and if it’s not then gives an error message. To explain ‘std::is_pointer_v’ we must first explain ‘std::is_pointer’: under the hood it’s just a template struct that has two overloads. One for pointer template type and the other for non-pointer. The first inherits std::true_type meaning that by default std::is_pointer<T>::value is true, and the second one inherits std::false type. The std::is_pointer_v is a inlined precompiled constant for std::is_pointer<T>::value

Соседние файлы в папке Thesis