baggage.h 2.17 KB
Newer Older
1
2
3
4
5
6
7
#ifndef _BAGGAGE_H_
#define _BAGGAGE_H_

#include <iostream>
#include <cstdint>
#include <cstddef>
#include <vector>
8
#include <string>
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47


/*
This file defines the main interfaces (Atoms and Baggage) of the Brown Tracing Plane

Most interactions will be with Baggage and ThreadLocalBaggage
*/


// An atom is just an array of bytes
struct Atom {

	std::vector<uint8_t> bytes;

	Atom() : bytes(0) {}
	Atom(int size) : bytes(size) {}
	Atom(std::vector<uint8_t> bytes) : bytes(bytes) {}

	// Lexicographic comparison of two atoms
	const int compare(const Atom& other);

	friend std::ostream& operator<< ( std::ostream& os, const Atom& atom );
};


// Baggage is an array of atoms
struct Baggage {

	std::vector<Atom> atoms;

	Baggage() : atoms(0) {}

	// Duplicates this baggage
	Baggage branch();

	// Lexicographic merge of two baggage instances, as defined in the Brown Tracing Plane
	static Baggage merge(Baggage& a, Baggage& b);

	// Serialize this baggage, as defined in the Brown Tracing Plane (length-prefix each atom in order)
48
	std::string str();
49
50
51
	std::vector<uint8_t> serialize();

	// Deserialize a serialized baggage instance, as defined in the Brown Tracing Plane
52
	static Baggage deserialize(std::string bytes);
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
	static Baggage deserialize(std::vector<uint8_t> bytes);

	friend std::ostream& operator<< ( std::ostream& os, const Baggage& baggage );

};


// Helper methods for storing a baggage instance in thread-local storage.  Usage is optional, but very useful
namespace ThreadLocalBaggage {

	Baggage& Get();	                     // Get the current thread's baggage
	Baggage Take();                      // Get the current thread's baggage, and clear the thread-local storage
	Baggage Branch();                    // Get a copy of the current thread's baggage

	void Delete();                       // Delete the current thread's baggage
	void Set(Baggage new_baggage);       // Set the current thread's baggage to the provided baggage, destroying the previous baggage
	void Join(Baggage &otherBaggage);    // Merge the current thread's baggage with the provided baggage
70
	Baggage Swap (Baggage otherBaggage); // Set the current thread's baggage to the provided baggage, and return the previous baggage
71
72
73
74
75

}


#endif