1 : // Copyright 2015 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 : //
15 : // Declares an interface for recording events, which can be played by a
16 : // story teller in an arbitrary order, and during which stats can be
17 : // collected for user analysis.
18 : #ifndef SYZYGY_BARD_EVENT_H_
19 : #define SYZYGY_BARD_EVENT_H_
20 :
21 : #include "base/memory/scoped_ptr.h"
22 : #include "syzygy/core/serialization.h"
23 :
24 : namespace bard {
25 :
26 : // Interface for storing and playing events.
27 : class EventInterface {
28 : public:
29 : // Enum of all non-abstract classes that extend the EventInterface.
30 : // New events should only be added at the end of the Enum (but before
31 : // kMaxEventType), to maintain backwards compatibility for
32 : // serialization/deserialization.
33 : enum EventType {
34 : kLinkedEvent,
35 : // Memory-profiling related events.
36 : kHeapAllocEvent,
37 : kHeapCreateEvent,
38 : kHeapDestroyEvent,
39 : kHeapFreeEvent,
40 : kHeapReAllocEvent,
41 : kHeapSetInformationEvent,
42 : kHeapSizeEvent,
43 : // New events must be added strictly to the end in order for serialization
44 : // to maintain backwards compatibility.
45 : // This must come last.
46 : kMaxEventType
47 : };
48 :
49 E : virtual ~EventInterface() { }
50 :
51 : // This event's EventType.
52 : // @returns the EventType enum representing this event.
53 : virtual EventType type() const = 0;
54 :
55 : // Plays the recorded function call, possibly modifying the current
56 : // backdrop.
57 : // @note The backdrop is a piece of user data, specific to a set of
58 : // events, whose exact type is dictated by convention.
59 : // @param backdrop the backdrop.
60 : // @returns true if Play succeeds without any problems, false otherwise.
61 : virtual bool Play(void* backdrop) = 0;
62 :
63 : // Equality comparator.
64 : virtual bool Equals(const EventInterface* rhs) const = 0;
65 :
66 : // NOTE: Every non-abstract class that extends Event should
67 : // also implement the following functions:
68 : //
69 : // Serialize an Event in an OutArchive.
70 : // @param event a ponter to the event to be serialized.
71 : // @param out_archive where to serialize the event.
72 : // @returns true on success, false otherwise.
73 : // static bool Save(const EventInterface* const event,
74 : // core::OutArchive* out_archive);
75 : //
76 : // Deserialize an event from an InArchive.
77 : // @param in_archive from where to deserialize this event.
78 : // @returns a scoped__ptr to the newly created event on success,
79 : // an nullptr scoped_ptr otherwise.
80 : // static scoped_ptr<DerivedEvent> Load(core::InArchive* in_archive);
81 : //
82 : // NOTE: A DerivedEvent event should NOT save its own type in the Save method.
83 : // That should be done by a root serialization, which will need to read the
84 : // type to call the appropriate static save method from the appropriate class.
85 :
86 : // Serialization of an event. These will automatically dispatch to the
87 : // appropriately typed serialization mechanism.
88 : static bool Save(const EventInterface* event, core::OutArchive* out_archive);
89 : static scoped_ptr<EventInterface> Load(core::InArchive* in_archive);
90 : };
91 :
92 : } // namespace bard
93 :
94 : #endif // SYZYGY_BARD_EVENT_H_
|