VR-Forces Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
updateSenderHelpers.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright (c) 2026 MAK Technologies, Inc.
3  * All rights reserved.
4  ******************************************************************************/
5 
6 #pragma once
7 
8 #include <json/json.h>
9 #include <map>
10 #include <set>
11 #include <string>
12 #include <utility>
13 #include <vector>
14 #include "jsonSerializer.h"
15 
16 template<typename SnapshotType>
18 {
19  SnapshotType snapshot;
20  bool isNew = false;
22 };
23 
33 template<typename SnapshotType, typename FilterFn, typename ApplyModsFn, typename ToJsonFn>
34 std::pair<
35  std::vector<DtChangedItem<SnapshotType>>,
36  std::set<std::string>
37 >
39  const std::vector<SnapshotType>& snapshots,
40  const std::set<std::string>& knownSet,
41  const std::map<std::string, Json::Value>& lastSentAttrs,
42  FilterFn filter,
43  ApplyModsFn applyMods,
44  ToJsonFn toJson)
45 {
46  std::vector<DtChangedItem<SnapshotType>> changed;
47  std::set<std::string> current;
48 
49  for (const auto& snapshot : snapshots)
50  {
51  if (!filter(snapshot))
52  {
53  continue;
54  }
55 
56  current.insert(snapshot.uuid);
57 
58  const SnapshotType modSnapshot = applyMods(snapshot);
59  Json::Value currentAttrs = toJson(modSnapshot);
60 
61  const auto cacheIt = lastSentAttrs.find(snapshot.uuid);
62  const bool isNew = (cacheIt == lastSentAttrs.end());
63  const bool wasKnown = knownSet.count(snapshot.uuid) > 0;
64 
65  bool hasChanges = isNew || !wasKnown;
66  if (!hasChanges && cacheIt != lastSentAttrs.end())
67  {
68  hasChanges = DtSerialization::DtJsonSerializer::hasChanges(currentAttrs, cacheIt->second);
69  }
70 
71  if (hasChanges)
72  {
73  DtChangedItem<SnapshotType> changedItem;
74  changedItem.snapshot = modSnapshot;
75  changedItem.isNew = isNew || !wasKnown;
76  changedItem.cachedJson = std::move(currentAttrs);
77  changed.push_back(std::move(changedItem));
78  }
79  }
80 
81  return std::make_pair(std::move(changed), std::move(current));
82 }
83 
85 inline std::vector<std::string> DtComputeDeletedNames(
86  const std::set<std::string>& known,
87  const std::set<std::string>& current)
88 {
89  std::vector<std::string> deleted;
90  for (const auto& name : known)
91  {
92  if (current.find(name) == current.end())
93  {
94  deleted.push_back(name);
95  }
96  }
97  return deleted;
98 }
Json::Value cachedJson
Definition: updateSenderHelpers.h:21
bool isNew
Definition: updateSenderHelpers.h:20
std::vector< std::string > DtComputeDeletedNames(const std::set< std::string > &known, const std::set< std::string > &current)
Return names present in known but absent from current.
Definition: updateSenderHelpers.h:85
static bool hasChanges(const Json::Value &current, const Json::Value &lastSent)
Check if any attributes have changed between two JSON objects.
IMGUI_API void Value(const char *prefix, bool b)
Definition: updateSenderHelpers.h:17
std::pair< std::vector< DtChangedItem< SnapshotType > >, std::set< std::string >> DtDetectChangedItems(const std::vector< SnapshotType > &snapshots, const std::set< std::string > &knownSet, const std::map< std::string, Json::Value > &lastSentAttrs, FilterFn filter, ApplyModsFn applyMods, ToJsonFn toJson)
Inspect snapshots against knownSet and lastSentAttrs, apply filter and applyMods to each item...
Definition: updateSenderHelpers.h:38
SnapshotType snapshot
Definition: updateSenderHelpers.h:19

Document ID: Generated on Thu Oct 15 21:27:02 EDT 2026 from SVN revision 296421
Copyright © 2005-2026 MAK Technologies. All Rights Reserved (www.mak.com)