CMSDK 2.0.1
Cross-platform C++ base library and SDK for the Psyclone AIOS platform
Loading...
Searching...
No Matches
cmlabs::GenericObservation< T > Class Template Reference

Header + typed payload wrapper for one observation, serializable to/from DataMessage and raw memory. More...

#include <Observations.h>

Collaboration diagram for cmlabs::GenericObservation< T >:
[legend]

Public Member Functions

 GenericObservation ()
 Empty observation (zeroed header and payload).
 GenericObservation (const char *data, uint32 size, uint64 id=0)
 Deserialize from a raw blob; id 0 means "first/any".
 ~GenericObservation ()
std::string print ()
 One-line human-readable rendering (header + payload).
std::string toJSON ()
 JSON object rendering.
bool extractDataFromMessage (DataMessage *msg, std::map< std::string, std::string > &mappings)
 Populate the payload from a message using logical→actual field mappings.
bool writeToMessage (DataMessage *msg, uint64 id)
 Append this observation to msg under entry id id.
bool readFromMessage (DataMessage *msg, uint64 id=0)
 Read the observation with id id (0 = first) from msg.
bool readFromData (const char *data, uint32 size, uint64 id=0)
 Parse from a raw blob.
bool isBetween (GenericObservation< T > *a, GenericObservation< T > *b)
 Range test delegated to the payload's isBetween(); see per-type notes on semantics.

Static Public Member Functions

static std::list< GenericObservation< T > * > ReadAllFromMessage (DataMessage *msg)
 Deserialize every observation of type T carried by msg.
static bool WriteAllToMessage (DataMessage *msg, std::list< GenericObservation< T > * > &list)
 Serialize all observations in list into msg.

Public Attributes

ObsHeader header
 Identity/provenance/confidence header.
data
 Typed payload.

Detailed Description

template<typename T>
class cmlabs::GenericObservation< T >

Header + typed payload wrapper for one observation, serializable to/from DataMessage and raw memory.

Template Parameters
TOne of the Obs* payload structs (ObsLocation, ObsDimension, ObsInteger, ObsFloat, ObsTime, ObsString). Implementations are in Observations.tpl.h.

Definition at line 348 of file Observations.h.

Constructor & Destructor Documentation

◆ GenericObservation() [1/2]

template<typename T>
cmlabs::GenericObservation< T >::GenericObservation ( )

Empty observation (zeroed header and payload).

Construct an empty observation: zeroed header stamped with the current time and the payload's type tag (data.getType()), then T::init() on the payload.

Definition at line 61 of file Observations.tpl.h.

References data, cmlabs::GetTimeNow(), header, and OBSERVATIONID.

Referenced by isBetween(), ReadAllFromMessage(), and WriteAllToMessage().

◆ GenericObservation() [2/2]

template<typename T>
cmlabs::GenericObservation< T >::GenericObservation ( const char * cdata,
uint32 size,
uint64 id = 0 )

Deserialize from a raw blob; id 0 means "first/any".

Construct from a serialized blob (ObsHeader + T payload).

Falls back to the default-constructed state (fresh header, T::init()) when cdata is NULL, the size does not match, or the type tag differs; no error is reported.

Parameters
cdataSerialized bytes (copied, not retained).
sizeByte count.
idOptional id hint (currently unused by readFromData()).

Definition at line 76 of file Observations.tpl.h.

References data, cmlabs::GetTimeNow(), header, OBSERVATIONID, and readFromData().

◆ ~GenericObservation()

template<typename T>
cmlabs::GenericObservation< T >::~GenericObservation ( )

Definition at line 89 of file Observations.tpl.h.

Member Function Documentation

◆ extractDataFromMessage()

template<typename T>
bool cmlabs::GenericObservation< T >::extractDataFromMessage ( DataMessage * msg,
std::map< std::string, std::string > & mappings )

Populate the payload from a message using logical→actual field mappings.

Populate payload and header fields from an arbitrary DataMessage.

Returns
true when all required fields were present.

First lets the payload pull its own fields via T::extractDataFromMessage() using the caller-supplied name mappings (message field name to observation field name); on success, fills the header from the message's well-known entries (SystemID, ObjectID, Confidence, Consensus, creation time).

Returns
true if the payload extraction succeeded.

Definition at line 171 of file Observations.tpl.h.

References data, cmlabs::DataMessage::getCreatedTime(), cmlabs::DataMessage::getFloat(), cmlabs::DataMessage::getInt(), cmlabs::DataMessage::getSystemID(), and header.

Referenced by cmlabs::Observations_UnitTest().

◆ isBetween()

template<typename T>
bool cmlabs::GenericObservation< T >::isBetween ( GenericObservation< T > * a,
GenericObservation< T > * b )

Range test delegated to the payload's isBetween(); see per-type notes on semantics.

Delegates to T::isBetween(): is this observation's payload between a's and b's (payload-defined ordering, e.g.

spatial or temporal interpolation range)?

Definition at line 159 of file Observations.tpl.h.

References data, and GenericObservation().

Referenced by cmlabs::Observations_UnitTest().

◆ print()

template<typename T>
std::string cmlabs::GenericObservation< T >::print ( )

One-line human-readable rendering (header + payload).

Human-readable one-line description of the payload (delegates to T::print()).

Definition at line 186 of file Observations.tpl.h.

References data.

◆ ReadAllFromMessage()

template<typename T>
std::list< GenericObservation< T > * > cmlabs::GenericObservation< T >::ReadAllFromMessage ( DataMessage * msg)
static

Deserialize every observation of type T carried by msg.

Extract every "OBS" data entry of matching size from msg into new observations.

Returns
Heap-allocated observations (caller owns).

Iterates ids 1,2,3,... until getData() finds no more entries; each blob whose size equals sizeof(ObsHeader)+sizeof(T) and whose type tag matches T is deserialized. Entries of the wrong observation type are skipped (readFromData() rejects them), but iteration stops at the first missing/short id, so ids must be contiguous from 1.

Returns
List of heap-allocated observations; the caller owns and must delete each one.
std::list<GenericObservation<MyObs>*> list = GenericObservation<MyObs>::ReadAllFromMessage(msg);
for (GenericObservation<MyObs>* o : list) { use(o); delete o; }
static std::list< GenericObservation< T > * > ReadAllFromMessage(DataMessage *msg)
Deserialize every observation of type T carried by msg.
GenericObservation()
Empty observation (zeroed header and payload).

Definition at line 32 of file Observations.tpl.h.

References data, GenericObservation(), cmlabs::DataMessage::getData(), and readFromData().

Referenced by cmlabs::Observations_UnitTest().

◆ readFromData()

template<typename T>
bool cmlabs::GenericObservation< T >::readFromData ( const char * cdata,
uint32 size,
uint64 id = 0 )

Parse from a raw blob.

Deserialize header and payload from a raw blob.

Returns
true on success.

Strict validation: size must equal sizeof(ObsHeader)+sizeof(T) and the blob's type tag must match this instantiation's type (set at construction), so reading a blob of another observation type fails cleanly rather than mis-interpreting bytes.

Parameters
cdataSerialized bytes (borrowed; copied into this object).
sizeByte count; must equal sizeof(ObsHeader)+sizeof(T).
idUnused.
Returns
true if the blob was accepted.

Definition at line 131 of file Observations.tpl.h.

References data, and header.

Referenced by GenericObservation(), ReadAllFromMessage(), and readFromMessage().

◆ readFromMessage()

template<typename T>
bool cmlabs::GenericObservation< T >::readFromMessage ( DataMessage * msg,
uint64 id = 0 )

Read the observation with id id (0 = first) from msg.

Deserialize the "OBS" data entry stored under id in msg (via readFromData()).

Returns
true on success.
true if the entry exists and matches this observation type.

Definition at line 144 of file Observations.tpl.h.

References cmlabs::DataMessage::getData(), and readFromData().

Referenced by cmlabs::Observations_UnitTest().

◆ toJSON()

template<typename T>
std::string cmlabs::GenericObservation< T >::toJSON ( )

JSON object rendering.

Render header metadata plus the payload's own JSON (T::toJSON()) as one JSON object.

If the payload produces no JSON, a stub object with "datatype": "unknown" is emitted so consumers still see the header fields.

Returns
JSON text ending in a newline.

Definition at line 194 of file Observations.tpl.h.

References data, header, cmlabs::PrintTimeString(), and cmlabs::utils::StringFormat().

◆ WriteAllToMessage()

template<typename T>
bool cmlabs::GenericObservation< T >::WriteAllToMessage ( DataMessage * msg,
std::list< GenericObservation< T > * > & list )
static

Serialize all observations in list into msg.

Bulk serialization counterpart of ReadAllFromMessage().

Returns
true on success.
Warning
Not implemented; always returns false. Serialize observations one by one with writeToMessage() instead.

Definition at line 53 of file Observations.tpl.h.

References GenericObservation().

◆ writeToMessage()

template<typename T>
bool cmlabs::GenericObservation< T >::writeToMessage ( DataMessage * msg,
uint64 id )

Append this observation to msg under entry id id.

Serialize this observation into msg as data entry id of type "OBS".

Returns
true on success.

The blob is header followed by a raw memcpy of the T payload (see the file-level note on portability). Fails if neither id nor header.id is set, or if the message already contains an "OBS" entry under that id (no overwrite).

Parameters
msgTarget message (caller-owned).
idEntry id; 0 means use header.id.
Returns
true on success. Uses a temporary heap buffer; msg copies the bytes.

Definition at line 103 of file Observations.tpl.h.

References data, cmlabs::DataMessage::getData(), header, and cmlabs::DataMessage::setData().

Referenced by cmlabs::Observations_UnitTest().

Member Data Documentation

◆ data

◆ header

template<typename T>
ObsHeader cmlabs::GenericObservation< T >::header

Identity/provenance/confidence header.

Definition at line 359 of file Observations.h.

Referenced by extractDataFromMessage(), GenericObservation(), GenericObservation(), cmlabs::Observations_UnitTest(), readFromData(), toJSON(), and writeToMessage().


The documentation for this class was generated from the following files: