File: Property.h

package info (click to toggle)
cyphesis-cpp 0.6.2-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 10,752 kB
  • sloc: cpp: 94,194; xml: 40,196; python: 8,717; sh: 4,164; makefile: 1,968; ansic: 753
file content (154 lines) | stat: -rw-r--r-- 5,840 bytes parent folder | download
1
2
3
4
5
6
7
8
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Cyphesis Online RPG Server and AI Engine
// Copyright (C) 2004 Alistair Riddoch
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA


#ifndef COMMON_PROPERTY_H
#define COMMON_PROPERTY_H

#include "OperationRouter.h"

#include <Atlas/Message/Element.h>

class LocatedEntity;

/// \brief Interface for Entity properties
///
/// \ingroup PropertyClasses
class PropertyBase {
  protected:
    /// \brief Flags indicating how this Property should be handled
    unsigned int m_flags;
    explicit PropertyBase(unsigned int flags = 0);
    PropertyBase(const PropertyBase &) = default;
  public:
    virtual ~PropertyBase();

    /// \brief Accessor for Property flags
    unsigned int flags() const { return m_flags; }
    /// \brief Accessor for Property flags
    unsigned int & flags() { return m_flags; }

    void setFlags(unsigned int flags) { m_flags |= flags; }

    void resetFlags(unsigned int flags) { m_flags &= ~flags; }

    /// \brief Install this property on an entity
    ///
    /// Called whenever an Entity gains this property for the first time
    virtual void install(LocatedEntity *, const std::string &);
    /// \brief Apply whatever effect this property has on an Entity
    ///
    /// Called whenever the value of this property should affect an Entity
    virtual void apply(LocatedEntity *);

    /// \brief Copy the value of the property into an Atlas Message
    virtual int get(Atlas::Message::Element & val) const = 0;
    /// \brief Read the value of the property from an Atlas Message
    virtual void set(const Atlas::Message::Element & val) = 0;
    /// \brief Add the value as an attribute to an Atlas map
    virtual void add(const std::string & key, Atlas::Message::MapType & map) const;
    /// \brief Add the value as an attribute to an Atlas entity
    virtual void add(const std::string & key, const Atlas::Objects::Entity::RootEntity & ent) const;
    /// \brief Handle an operation
    virtual HandlerResult operation(LocatedEntity *,
                                    const Operation &,
                                    OpVector &);
    /// \brief Create a copy of this instance
    ///
    /// The copy should have exactly the same type, and the same value
    virtual PropertyBase * copy() const = 0;
};

/// \brief Flag indicating data has been written to permanent store
/// \ingroup PropertyFlags
static const unsigned int per_clean = 1 << 0;
/// \brief Flag indicating data should never be persisted
/// \ingroup PropertyFlags
static const unsigned int per_ephem = 1 << 1;
/// \brief Flag indicating data has been stored initially
/// \ingroup PropertyFlags
static const unsigned int per_seen = 1 << 2;

/// \brief Flag mask indicating data should not be written to store
/// \ingroup PropertyFlags
static const unsigned int per_mask = per_clean | per_ephem;

/// \brief Flag indicating data is not visible
/// \ingroup PropertyFlags
static const unsigned int vis_hidden = 1 << 3;
/// \brief Flag indicating data is server internal
/// \ingroup PropertyFlags
static const unsigned int vis_internal = 1 << 4;

/// \brief Flag mask indicating data should be be perceptable
/// \ingroup PropertyFlags
static const unsigned int vis_mask = vis_hidden | vis_internal;

/// \brief Flag set to indicate this is a class property, and has no instance
/// \ingroup PropertyFlags
static const unsigned int flag_class = 1 << 5;

/// \brief Flag used for boolean properties
/// \ingroup PropertyFlags
static const unsigned int flag_bool = 1 << 6;

/// \brief Flag used to mark properties whose state has not been broadcast
/// \ingroup PropertyFlags
static const unsigned int flag_unsent = 1 << 7;

/// \brief Flag used to mark properties which must be instance properties
/// \ingroup PropertyFlags
/// Typically this will be because they have per-entity state which cannot
/// be handled on a class property.
static const unsigned int flag_instance = 1 << 8;

/// \brief Entity property template for properties with single data values
/// \ingroup PropertyClasses
template <typename T>
class Property : public PropertyBase {
  protected:
    /// \brief Reference to variable holding the value of this Property
    T m_data;
    Property(const Property<T> &) = default;
  public:
    explicit Property(unsigned int flags = 0);

    const T & data() const { return this->m_data; }
    T & data() { return this->m_data; }

    virtual int get(Atlas::Message::Element & val) const;
    virtual void set(const Atlas::Message::Element &);
    virtual void add(const std::string & key, Atlas::Message::MapType & map) const;
    virtual void add(const std::string & key, const Atlas::Objects::Entity::RootEntity & ent) const;
    virtual Property<T> * copy() const;
};

/// \brief Entity property that can store any Atlas value
/// \ingroup PropertyClasses
class SoftProperty : public PropertyBase {
  protected:
    Atlas::Message::Element m_data;
  public:
    SoftProperty();
    explicit SoftProperty(const Atlas::Message::Element & data);

    virtual int get(Atlas::Message::Element & val) const;
    virtual void set(const Atlas::Message::Element & val);
    virtual SoftProperty * copy() const;
};

#endif // COMMON_PROPERTY_H