Stroika Library 3.0d18
 
Loading...
Searching...
No Matches
DocumentDB/Sources/ComputerNetwork.cpp
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#include "Stroika/Frameworks/StroikaPreComp.h"
5
6#include <cstdlib>
7#include <random>
8
10#include "Stroika/Foundation/Containers/Set.h"
11#include "Stroika/Foundation/DataExchange/ObjectVariantMapper.h"
16
17#include "ComputerNetwork.h"
18
19using namespace std;
20
21using namespace Stroika::Foundation;
24using namespace Stroika::Foundation::Database;
25using namespace Stroika::Foundation::Database::Document;
27using namespace Stroika::Foundation::Execution;
28using namespace Stroika::Foundation::Time;
29
30namespace {
31
32 /**
33 * Define the c++ objects which will be persisted.
34 */
35 namespace Model {
36 struct Device {
37 optional<IDType> id; // use DocumentDB::IDType - to generically represent IDs in a way that works across all document DBs (and web service apis)
38 Set<int> openPorts;
39 String name;
40 Set<String> hardwareAddresses;
41
42 static const ObjectVariantMapper kMapper;
43
44 nonvirtual bool operator== (const Device& rhs) const = default;
45 };
46 }
47
48 /**
49 * This defines the mapping from C++ to how we would display the object in our 'model' - like via a webservice
50 * or stored in external files, or for debugging.
51 */
52 const ObjectVariantMapper Model::Device::kMapper = [] () {
54
55 /*
56 * Add dependency mappers
57 */
58 mapper.AddCommonType<Set<int>> ();
59 mapper.AddCommonType<Set<String>> ();
60
61 /*
62 * Add mapper for our user defined type we will be storing.
63 */
64 mapper.AddClass<Device> ({
65 {"id"sv, &Device::id},
66 {"name"sv, &Device::name},
67 {"openPorts"sv, &Device::openPorts},
68 {"hardwareAddresses"sv, &Device::hardwareAddresses},
69 });
70
71 return mapper;
72 }();
73}
74
75void Stroika::Samples::Document::ComputerNetworksModel (const function<Connection::Ptr ()>& connectionFactory)
76{
77 /*
78 * Create a database connection, and then a connection to a particular table (collection).
79 */
80 using Model::Device;
81 auto dbConnection = connectionFactory ();
82 dbConnection.CreateCollection ("Networks");
83 ObjectCollection::Ptr<Device> deviceConnection = ObjectCollection::New<Device> (dbConnection.GetCollection ("Networks"), Device::kMapper);
84
85 if (not deviceConnection.GetAll ().empty ()) {
86 Throw (RuntimeErrorException{"database should start empty"});
87 }
88
89 /*
90 * Create two c++ objects, perist them, delete 1, and check reading back we get the right results.
91 */
92 Device device1_ = Device{.openPorts = {33}, .name = "myLaptop"sv, .hardwareAddresses = {"ff:33:aa:da:ff:33"_k}};
93 Device device2_ = Device{.openPorts = {123, 145}, .name = "some machine"sv, .hardwareAddresses = {"33:aa:dd:ad:af:11"_k}};
94 device1_.id = deviceConnection.Add (device1_);
95 device2_.id = deviceConnection.Add (device2_);
96 {
97 auto devices = deviceConnection.GetAll ();
98 if (devices.size () != 2) {
99 Throw (RuntimeErrorException{"we should have the ones we just added"sv});
100 }
101 if (not devices.Contains (device1_)) {
102 Throw (RuntimeErrorException{"we should have the ones we just added{1}"sv});
103 }
104 if (not devices.Contains (device2_)) {
105 Throw (RuntimeErrorException{"we should have the ones we just added{2}"sv});
106 }
107 }
108 deviceConnection.Remove (Memory::ValueOf (device2_.id));
109 {
110 auto devices = deviceConnection.GetAll ();
111 if (devices.size () != 1) {
112 Throw (RuntimeErrorException{"we should have the ones we just added"});
113 }
114 if (not devices.Contains (device1_)) {
115 Throw (RuntimeErrorException{"we should have kDevice1_"});
116 }
117 }
118}
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201
Set<T> is a container of T, where once an item is added, additionally adds () do nothing.
ObjectVariantMapper can be used to map C++ types to and from variant-union types, which can be transp...
nonvirtual void AddClass(const Traversal::Iterable< StructFieldInfo > &fieldDescriptions, const ClassMapperOptions< CLASS > &mapperOptions={})
nonvirtual void AddCommonType(ARGS &&... args)
void Throw(T &&e2Throw)
identical to builtin C++ 'throw' except that it does helpful, type dependent DbgTrace() messages firs...
Definition Throw.inl:43
STL namespace.