]> granicus.if.org Git - icinga2/blob - lib/base/object.cpp
Update copyright headers for 2016
[icinga2] / lib / base / object.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/)  *
4  *                                                                            *
5  * This program is free software; you can redistribute it and/or              *
6  * modify it under the terms of the GNU General Public License                *
7  * as published by the Free Software Foundation; either version 2             *
8  * of the License, or (at your option) any later version.                     *
9  *                                                                            *
10  * This program is distributed in the hope that it will be useful,            *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
13  * GNU General Public License for more details.                               *
14  *                                                                            *
15  * You should have received a copy of the GNU General Public License          *
16  * along with this program; if not, write to the Free Software Foundation     *
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
18  ******************************************************************************/
19
20 #include "base/object.hpp"
21 #include "base/value.hpp"
22 #include "base/dictionary.hpp"
23 #include "base/primitivetype.hpp"
24 #include "base/utility.hpp"
25
26 using namespace icinga;
27
28 DEFINE_TYPE_INSTANCE(Object);
29
30 /**
31  * Default constructor for the Object class.
32  */
33 Object::Object(void)
34         : m_References(0)
35 #ifdef I2_DEBUG
36         , m_LockOwner(0)
37 #endif /* I2_DEBUG */
38 { }
39
40 /**
41  * Destructor for the Object class.
42  */
43 Object::~Object(void)
44 { }
45
46 /**
47  * Returns a string representation for the object.
48  */
49 String Object::ToString(void) const
50 {
51         return "Object of type '" + GetReflectionType()->GetName() + "'";
52 }
53
54 #ifdef I2_DEBUG
55 /**
56  * Checks if the calling thread owns the lock on this object.
57  *
58  * @returns True if the calling thread owns the lock, false otherwise.
59  */
60 bool Object::OwnsLock(void) const
61 {
62 #ifdef _WIN32
63         DWORD tid = InterlockedExchangeAdd(&m_LockOwner, 0);
64
65         return (tid == GetCurrentThreadId());
66 #else /* _WIN32 */
67         pthread_t tid = __sync_fetch_and_add(&m_LockOwner, 0);
68
69         return (tid == pthread_self());
70 #endif /* _WIN32 */
71 }
72 #endif /* I2_DEBUG */
73
74 void Object::SetField(int id, const Value&, bool, const Value&)
75 {
76         if (id == 0)
77                 BOOST_THROW_EXCEPTION(std::runtime_error("Type field cannot be set."));
78         else
79                 BOOST_THROW_EXCEPTION(std::runtime_error("Invalid field ID."));
80 }
81
82 Value Object::GetField(int id) const
83 {
84         if (id == 0)
85                 return GetReflectionType()->GetName();
86         else
87                 BOOST_THROW_EXCEPTION(std::runtime_error("Invalid field ID."));
88 }
89
90 void Object::Validate(int types, const ValidationUtils& utils)
91 {
92         /* Nothing to do here. */
93 }
94
95 void Object::ValidateField(int id, const Value& value, const ValidationUtils& utils)
96 {
97         /* Nothing to do here. */
98 }
99
100 void Object::NotifyField(int id, const Value& cookie)
101 {
102         BOOST_THROW_EXCEPTION(std::runtime_error("Invalid field ID."));
103 }
104
105 Object::Ptr Object::NavigateField(int id) const
106 {
107         BOOST_THROW_EXCEPTION(std::runtime_error("Invalid field ID."));
108 }
109
110 Object::Ptr Object::Clone(void) const
111 {
112         BOOST_THROW_EXCEPTION(std::runtime_error("Object cannot be cloned."));
113 }
114
115 Type::Ptr Object::GetReflectionType(void) const
116 {
117         return Object::TypeInstance;
118 }
119