ScaleOut C++ Native Client API  5.1
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends Pages
named_protobuf_cache.h
1 /*
2  Copyright (c) 2013 by ScaleOut Software, Inc.
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 
17 #pragma once
18 #ifdef HAVE_PROTOBUF
19 
20 #include <stdint.h>
21 #include <vector>
22 #include <boost/shared_ptr.hpp>
23 #include <boost/make_shared.hpp>
24 
25 #ifdef _MSC_VER
26 #pragma warning(push)
27 #pragma warning(disable: 4127 4244 4267)
28 #endif
29 #include <google/protobuf/stubs/common.h>
30 #include <google/protobuf/message.h>
31 #ifdef _MSC_VER
32 #pragma warning(pop)
33 #endif
34 
35 #include "soss_client/typed_named_cache.h"
36 #include "soss_client/filter.h"
37 #include "soss_client/internal/protobuf_index_builder.h"
38 
39 namespace sosscli {
40 namespace pb = ::google::protobuf;
41 
44 template<typename T>
46 {
47 public:
48 
49  /* Constructors */
50 
54  NamedProtobufCache(const std::string &name) { this->init(name); }
55 
58  NamedProtobufCache(const uint32_t app_id) { this->init(app_id); }
59 
61  virtual ~NamedProtobufCache() {}
62 
63 protected:
64 
65  virtual boost::shared_ptr<T> deserialize(const std::vector<uint8_t> &bytes)
66  {
67  boost::shared_ptr<T> obj_p = boost::make_shared<T>();
68  obj_p->ParseFromArray(&bytes[0], static_cast<int>(bytes.size()));
69  return obj_p;
70  }
71 
72  virtual void serialize(T const & message, std::vector<uint8_t> &bytes_out, std::vector<uint8_t> &prop_spec_out)
73  {
74  int message_size = message.ByteSize();
75  bytes_out.resize(message_size);
76  message.SerializeToArray(bytes_out.data(), message_size);
77  sosscli::internal::ProtobufIndexBuilder::generate_prop_spec(message, prop_spec_out);
78  }
79 
80  virtual void serialize_filter(const Filter &filter, std::vector<uint8_t> &bytes_out, size_t bytes_to_reserve_for_header)
81  {
82  sosscli::internal::ProtobufIndexBuilder::generate_query_spec(T::default_instance(), filter, bytes_out, bytes_to_reserve_for_header);
83  }
84 };
85 
86 }
87 
88 #endif