atscppapi  1.0.9
C++ wrapper for Apache Traffic Server API
 All Classes Files Functions Enumerations Enumerator Macros
InitializableValue.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 LinkedIn Corp. All rights reserved.
3  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
4  * except in compliance with the License. You may obtain a copy of the license at
5  * http://www.apache.org/licenses/LICENSE-2.0
6  *
7  * Unless required by applicable law or agreed to in writing, software distributed under the
8  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
9  * either express or implied.
10  *
11  */
12 
13 /**
14  * @file InitializableValue.h
15  * @author Brian Geffon
16  * @author Manjesh Nilange
17  */
18 
19 #pragma once
20 #ifndef ATSCPPAPI_INITIALIZABLEVALUE_H_
21 #define ATSCPPAPI_INITIALIZABLEVALUE_H_
22 
23 namespace atscppapi {
24 
25 // cannot be static as InitializableValue is a template and a static
26 // member of that would be instantiated once for every type the
27 // template is instantiated
28 extern bool transaction_data_caching_enabled;
29 
30 /**
31  * @private
32  */
33 template <typename Type> class InitializableValue {
34 public:
35  InitializableValue() : initialized_(false) { }
36  explicit InitializableValue(Type value, bool initialized = true) : value_(value), initialized_(initialized) { }
37 
38  inline void setValue(const Type &value) {
39  value_ = value;
40  initialized_ = true;
41  }
42 
43  inline bool isInitialized() const {
44 #ifdef DISABLE_TRANSACTION_DATA_CACHING
45  return false;
46 #endif
47  return transaction_data_caching_enabled && initialized_;
48  }
49 
50  inline Type &getValueRef() {
51  return value_;
52  }
53 
54  inline Type getValue() {
55  return value_;
56  }
57 
58  inline const Type &getValueRef() const {
59  return value_;
60  }
61 
62  inline void setInitialized(bool initialized = true) {
63  initialized_ = initialized;
64  }
65 
66  inline operator Type&() {
67  return value_;
68  }
69 
70  inline operator const Type&() const {
71  return value_;
72  }
73 
74  inline InitializableValue<Type> &operator=(const Type& value) {
75  setValue(value);
76  return *this;
77  }
78 
79 private:
80  Type value_;
81  bool initialized_;
82 };
83 
84 }
85 
86 #endif /* ATSCPPAPI_INITIALIZABLEVALUE_H_ */